3 #include "AudioTools/CoreAudio/AudioStreams.h"
4 #include "AudioTools/CoreAudio/AudioOutput.h"
5 #include "AudioTools/AudioCodecs/CodecWAV.h"
11 #include "nlohmann/json.hpp"
12 #include "xtl/xbase64.hpp"
22 p_audio_stream = &stream;
24 size_t write(
const uint8_t *data,
size_t len)
override {
25 p_audio_stream->write((
const char*)data,len);
28 int availableForWrite()
override {
32 std::fstream *p_audio_stream=
nullptr;
43 void setup(std::string fName,
int channelCount,
int channelNo) {
45 this->channels = channelCount;
46 if (this->channels==0){
47 LOGE(
"Setting channels to 0");
49 this->channel = channelNo;
53 return this->channels;
65 if (channel<channels){
67 is.open(fname, is.binary);
68 is.seekg(wav_header_size, is.beg);
69 std::list<int16_t> audioList;
71 size_t rec_size = channels*
sizeof(T);
72 while(is.read((
char *)buffer, rec_size)){
73 audioList.push_back(transform(buffer[channel]));
75 string str_size =
"102400";
76 str <<
"<style>div.x-svg {width: "<< str_size <<
"px; }</style>";
77 str <<
"<div class='x-svg'><svg viewBox='0 0 "<< str_size <<
" 100'> <polyline fill='none' stroke='blue' stroke-width='1' points ='";
80 for(int16_t sample: audioList){
81 str << idx++ <<
"," << sample <<
" ";
83 str <<
"'/></svg></div>";
85 str <<
"<p>Channel " << channel <<
" of " << channels <<
" does not exist!</p>";
91 std::stringstream str;
93 const int wav_header_size = 44;
98 int result = x / 1000;
104 using Chart = ChartT<int16_t>;
110 template <
typename T>
115 buffer_count = bufferCount;
116 p_audio_stream = &stream;
118 copier.resize(bufferSize);
128 chrt.setup(fname, cfg.
channels, channel);
133 const std::string &name()
const {
138 const std::string path()
const {
139 std::filesystem::path p = fname;
140 std::string result = std::filesystem::absolute(p);
145 void createWAVFile(){
148 std::fstream fstream(fname, fstream.binary | fstream.trunc | fstream.out);
152 out.setEncoder(&wave_encoder);
154 copier.begin(out, *p_audio_stream);
155 copier.copyN(buffer_count);
158 }
catch(
const std::exception& ex){
159 std::cerr << ex.what();
164 ifstream f(fname.c_str());
173 std::string audio() {
174 std::ifstream fin(fname, std::ios::binary);
175 std::stringstream m_buffer;
176 m_buffer << fin.rdbuf();
177 return xtl::base64encode(m_buffer.str());
193 size_t buffer_count=0;
196 using JupyterAudio = JupyterAudioT<int16_t>;
201 nl::json mime_bundle_repr(Chart &in) {
202 auto bundle = nl::json::object();
203 bundle[
"text/html"] = in.chartData();
208 nl::json mime_bundle_repr(JupyterAudio &in) {
209 auto bundle = nl::json::object();
211 bundle[
"text/html"] =
"<audio controls "
212 "src='data:audio/wav;base64," +
If you want to use the framework w/o Arduino you need to provide the implementation of a couple of cl...