arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
AudioOutput.h
1#pragma once
2#include "AudioToolsConfig.h"
3#include "AudioTools/CoreAudio/AudioTypes.h"
4#include "AudioTools/CoreAudio/BaseConverter.h"
5#include "AudioTools/CoreAudio/Buffers.h"
6
7namespace audio_tools {
8
9#if USE_PRINT_FLUSH
10#define PRINT_FLUSH_OVERRIDE override
11#else
12#define PRINT_FLUSH_OVERRIDE
13#endif
14
20class AudioOutput : public Print,
21 public AudioInfoSupport,
22 public AudioInfoSource {
23public:
24 virtual ~AudioOutput() = default;
25
26 virtual size_t write(const uint8_t *data, size_t len) override = 0;
27
28 virtual size_t write(uint8_t ch) override {
29 if (tmp.isFull()) {
30 flush();
31 }
32 return tmp.write(ch);
33 }
34
35 virtual int availableForWrite() override { return DEFAULT_BUFFER_SIZE; }
36
37 // removed override because some old implementation did not define this method
38 // as virtual
39 virtual void flush() PRINT_FLUSH_OVERRIDE {
40 if (tmp.available() > 0) {
41 write((const uint8_t *)tmp.address(), tmp.available());
42 }
43 }
44
45 // overwrite to do something useful
46 virtual void setAudioInfo(AudioInfo newInfo) override {
47 TRACED();
48 if (cfg != newInfo){
49 cfg = newInfo;
50 cfg.logInfo();
51 }
52 AudioInfo out = audioInfoOut();
53 if (out) notifyAudioChange(out);
54 }
55
57 virtual bool isDeletable() { return false; }
58
59 virtual AudioInfo audioInfo() override { return cfg; }
60
63 virtual void writeSilence(size_t len) {
64 int16_t zero = 0;
65 for (int j = 0; j < len / 2; j++) {
66 write((uint8_t *)&zero, 2);
67 }
68 }
69
70 virtual bool begin(AudioInfo info) {
71 setAudioInfo(info);
72 return begin();
73 }
74
75 virtual bool begin() {
76 is_active = true;
77 return true;
78 }
79 virtual void end() { is_active = false; }
80
81 virtual operator bool() { return is_active; }
82
83protected:
84 int tmpPos = 0;
85 AudioInfo cfg;
86 SingleBuffer<uint8_t> tmp{MAX_SINGLE_CHARS};
87 bool is_active = false;
88};
89
98 public:
100 virtual void setOutput(Print& out) = 0;
101};
102
103
113template <typename T> class CsvOutput : public AudioOutput {
114public:
115 CsvOutput(int buffer_size = DEFAULT_BUFFER_SIZE, bool active = true) {
116 this->is_active = active;
117 }
118
120 CsvOutput(Print &out, int channels = 2, int buffer_size = DEFAULT_BUFFER_SIZE,
121 bool active = true) {
122 this->out_ptr = &out;
123 this->is_active = active;
124 cfg.channels = channels;
125 }
126
128 void setDelimiter(const char *del) { delimiter_str = del; }
129
131 const char *delimiter() { return delimiter_str; }
132
134
137 AudioInfo info;
138 info.channels = 2;
139 info.sample_rate = 44100;
140 info.bits_per_sample = sizeof(T) * 8;
141 return info;
142 }
143
145 bool begin(AudioInfo info) override { return begin(info.channels); }
146
148 bool begin(int channels) {
149 TRACED();
150 cfg.channels = channels;
151 return begin();
152 }
153
155 bool begin() override {
156 this->is_active = true;
157 // if (out_ptr == &Serial){
158 // Serial.setTimeout(60000);
159 // }
160 return true;
161 }
162
164 virtual void setAudioInfo(AudioInfo info) override {
165 TRACEI();
166 this->is_active = true;
167 info.logInfo();
168 cfg = info;
169 };
170
172 virtual size_t write(const uint8_t *data, size_t len) override {
173 LOGD("CsvOutput::write: %d", (int)len);
174 if (!is_active) {
175 LOGE("is not active");
176 return 0;
177 }
178
179 if (len==0){
180 return 0;
181 }
182
183 if (cfg.channels == 0) {
184 LOGW("Channels not defined: using 2");
185 cfg.channels = 2;
186 }
187 size_t lenChannels = len / (sizeof(T) * cfg.channels);
188 if (lenChannels > 0) {
189 writeFrames((T *)data, lenChannels);
190 } else if (len == sizeof(T)) {
191 // if the write contains less then a frame we buffer the data
192 T *data_value = (T *)data;
193 out_ptr->print(data_value[0]);
194 channel++;
195 if (channel == cfg.channels) {
196 out_ptr->println();
197 channel = 0;
198 } else {
199 out_ptr->print(delimiter_str);
200 }
201 } else {
202 LOGE("Unsupported size: %d for channels %d and bits: %d", (int)len,
203 cfg.channels, cfg.bits_per_sample);
204 }
205#if USE_PRINT_FLUSH
206 out_ptr->flush();
207#endif
208 return len;
209 }
210
211 int availableForWrite() override { return 1024; }
212
213protected:
214 T *data_ptr;
215 Print *out_ptr = &Serial;
216 int channel = 0;
217 const char *delimiter_str = ",";
218
219 void writeFrames(T *data_ptr, int frameCount) {
220 for (size_t j = 0; j < frameCount; j++) {
221 for (int ch = 0; ch < cfg.channels; ch++) {
222 if (out_ptr != nullptr && data_ptr != nullptr) {
223 T value = *data_ptr;
224 out_ptr->print(value);
225 }
226 data_ptr++;
227 if (ch < cfg.channels - 1)
228 this->out_ptr->print(delimiter_str);
229 }
230 this->out_ptr->println();
231 }
232 }
233};
234
235
243public:
244 HexDumpOutput(int buffer_size = DEFAULT_BUFFER_SIZE, bool active = true) {
245 this->is_active = active;
246 }
247
249 HexDumpOutput(Print &out, int buffer_size = DEFAULT_BUFFER_SIZE,
250 bool active = true) {
251 this->out_ptr = &out;
252 this->is_active = active;
253 }
254
255 bool begin() override {
256 TRACED();
257 this->is_active = true;
258 pos = 0;
259 return is_active;
260 }
261
262 void flush() override {
263 out_ptr->println();
264 pos = 0;
265 }
266
267 virtual size_t write(const uint8_t *data, size_t len) override {
268 if (!is_active)
269 return 0;
270 TRACED();
271 for (size_t j = 0; j < len; j++) {
272 out_ptr->print(data[j], HEX);
273 out_ptr->print(" ");
274 pos++;
275 if (pos == 8) {
276 out_ptr->print(" - ");
277 }
278 if (pos == 16) {
279 out_ptr->println();
280 pos = 0;
281 }
282 }
283 return len;
284 }
285
286 //
287 AudioInfo defaultConfig(RxTxMode mode = TX_MODE) {
288 AudioInfo info;
289 return info;
290 }
291
292protected:
293 Print *out_ptr = &Serial;
294 int pos = 0;
295};
296
297
305template <typename T>
306class OutputMixer : public Print {
307public:
308 OutputMixer() = default;
309
310 OutputMixer(Print &finalOutput, int outputStreamCount) {
311 setOutput(finalOutput);
312 setOutputCount(outputStreamCount);
313 }
314
315 void setOutput(Print &finalOutput) { p_final_output = &finalOutput; }
316
317 void setOutputCount(int count) {
318 output_count = count;
319 buffers.resize(count);
320 for (int i = 0; i < count; i++) {
321 buffers[i] = nullptr;
322 }
323 weights.resize(count);
324 for (int i = 0; i < count; i++) {
325 weights[i] = 1.0;
326 }
327
328 update_total_weights();
329 }
330
333 void setWeight(int channel, float weight) {
334 if (channel < size()) {
335 weights[channel] = weight;
336 } else {
337 LOGE("Invalid channel %d - max is %d", channel, size() - 1);
338 }
339 update_total_weights();
340 }
341
343 bool begin(int copy_buffer_size_bytes = DEFAULT_BUFFER_SIZE,
344 MemoryType memoryType = PS_RAM) {
345 is_active = true;
346 size_bytes = copy_buffer_size_bytes;
347 stream_idx = 0;
348 memory_type = memoryType;
349 allocate_buffers(size_bytes);
350 return true;
351 }
352
354 void end() {
355 total_weights = 0.0;
356 is_active = false;
357 // release memory for buffers
358 free_buffers();
359 }
360
362 int size() { return output_count; }
363
364 size_t write(uint8_t) override { return 0; }
365
368 size_t write(const uint8_t *data, size_t len) override {
369 size_t result = write(stream_idx, data, len);
370 // after writing the last stream we flush
371 if (is_auto_index) {
372 stream_idx++;
373 if (stream_idx >= output_count) {
374 flushMixer();
375 }
376 }
377 return result;
378 }
379
381 size_t write(int idx, const uint8_t *buffer_c, size_t bytes) {
382 LOGD("write idx %d: %d", idx, bytes);
383 size_t result = 0;
384 RingBuffer<T> *p_buffer = idx < output_count ? buffers[idx] : nullptr;
385 assert(p_buffer != nullptr);
386 size_t samples = bytes / sizeof(T);
387 if (p_buffer->availableForWrite() >= samples) {
388 result = p_buffer->writeArray((T *)buffer_c, samples) * sizeof(T);
389 } else {
390 LOGW("Available Buffer %d too small %d: requested: %d -> increase the "
391 "buffer size", idx,
392 p_buffer->availableForWrite()*sizeof(T), bytes);
393 }
394 return result;
395 }
396
398 int availableForWrite() override {
399 return is_active ? availableForWrite(stream_idx) : 0;
400 }
401
403 int availableForWrite(int idx) {
404 RingBuffer<T> *p_buffer = buffers[idx];
405 if (p_buffer == nullptr)
406 return 0;
407 return p_buffer->availableForWrite() * sizeof(T);
408 }
409
411 int available(int idx){
412 RingBuffer<T> *p_buffer = buffers[idx];
413 if (p_buffer == nullptr)
414 return 0;
415 return p_buffer->available() * sizeof(T);
416 }
417
419 void flushMixer() {
420 LOGD("flush");
421 bool result = false;
422
423 // determine ringbuffer with mininum available data
424 size_t samples = availableSamples();
425 // sum up samples
426 if (samples > 0) {
427 result = true;
428 // mix data from ringbuffers to output
429 output.resize(samples);
430 memset(output.data(), 0, samples * sizeof(T));
431 for (int j = 0; j < output_count; j++) {
432 float weight = weights[j];
433 // sum up input samples to result samples
434 for (int i = 0; i < samples; i++) {
435 T sample = 0;;
436 buffers[j]->read(sample);
437 output[i] += weight * sample / total_weights;
438 }
439 }
440
441 // write output
442 LOGD("write to final out: %d", samples * sizeof(T));
443 p_final_output->write((uint8_t *)output.data(), samples * sizeof(T));
444 }
445 stream_idx = 0;
446 return;
447 }
448
449 int availableSamples() {
450 size_t samples = 0;
451 for (int j = 0; j < output_count; j++) {
452 int available_samples = buffers[j]->available();
453 if (available_samples > 0){
454 samples = MIN(size_bytes / sizeof(T), (size_t)available_samples);
455 }
456 }
457 return samples;
458 }
459
461 void resize(int size) {
462 if (size != size_bytes) {
463 allocate_buffers(size);
464 }
465 size_bytes = size;
466 }
467
468
469 size_t writeSilence(size_t bytes) {
470 if (bytes == 0) return 0;
471 uint8_t silence[bytes];
472 memset(silence, 0, bytes);
473 return write(stream_idx, silence, bytes);
474 }
475
476 size_t writeSilence(int idx, size_t bytes){
477 if (bytes == 0) return 0;
478 uint8_t silence[bytes];
479 memset(silence, 0, bytes);
480 return write(idx, silence, bytes);
481 }
482
484 void setAutoIndex(bool flag){
485 is_auto_index = flag;
486 }
487
489 void setIndex(int idx){
490 stream_idx = idx;
491 }
492
494 void next() {
495 stream_idx++;
496 }
497
498protected:
499 Vector<RingBuffer<T> *> buffers{0};
500 Vector<T> output{0};
501 Vector<float> weights{0};
502 Print *p_final_output = nullptr;
503 float total_weights = 0.0;
504 bool is_active = false;
505 int stream_idx = 0;
506 int size_bytes = 0;
507 int output_count = 0;
508 MemoryType memory_type;
509 void *p_memory = nullptr;
510 bool is_auto_index = true;
511
512 void update_total_weights() {
513 total_weights = 0.0;
514 for (int j = 0; j < weights.size(); j++) {
515 total_weights += weights[j];
516 }
517 }
518
519 void allocate_buffers(int size) {
520 // allocate ringbuffers for each output
521 for (int j = 0; j < output_count; j++) {
522 if (buffers[j] != nullptr) {
523 delete buffers[j];
524 }
525#if defined(ESP32) && defined(ARDUINO)
526 if (memory_type == PS_RAM && ESP.getFreePsram() >= size) {
527 p_memory = ps_malloc(size);
528 LOGI("Buffer %d allocated %d bytes in PS_RAM", j, size);
529 } else {
530 p_memory = malloc(size);
531 LOGI("Buffer %d allocated %d bytes in RAM", j, size);
532 }
533 if (p_memory != nullptr) {
534 buffers[j] = new (p_memory) RingBuffer<T>(size / sizeof(T));
535 } else {
536 LOGE("Not enough memory to allocate %d bytes", size);
537 }
538#else
539 buffers[j] = new RingBuffer<T>(size / sizeof(T));
540#endif
541 }
542 }
543
544 void free_buffers() {
545 // allocate ringbuffers for each output
546 for (int j = 0; j < output_count; j++) {
547 if (buffers[j] != nullptr) {
548 delete buffers[j];
549#ifdef ESP32
550 if (p_memory != nullptr) {
551 free(p_memory);
552 }
553#endif
554 buffers[j] = nullptr;
555 }
556 }
557 }
558};
559
560
565class MemoryOutput : public AudioOutput {
566public:
567 MemoryOutput(uint8_t *start, int len) {
568 p_start = start;
569 p_next = start;
570 max_size = len;
571 is_active = true;
572 if (p_next == nullptr) {
573 LOGE("start must not be null");
574 }
575 }
576
577 bool begin() override {
578 is_active = true;
579 p_next = p_start;
580 pos = 0;
581 return true;
582 }
583
584 size_t write(const uint8_t *data, size_t len) override {
585 if (p_next == nullptr)
586 return 0;
587 if (pos + len <= max_size) {
588 memcpy(p_next, data, len);
589 pos += len;
590 p_next += len;
591 return len;
592 } else {
593 LOGE("Buffer too small: pos:%d, size: %d ", pos, (int)max_size);
594 return 0;
595 }
596 }
597
598 int availableForWrite() override { return max_size - pos; }
599
600 int size() { return max_size; }
601
602protected:
603 int pos = 0;
604 uint8_t *p_start = nullptr;
605 uint8_t *p_next = nullptr;
606 size_t max_size;
607};
608
609
619public:
620 ChannelSplitOutput() = default;
621
622 ChannelSplitOutput(Print &out, int channel) { addOutput(out, channel); }
623
626 void addOutput(Print &out, int channel) {
628 def.channel = channel;
629 def.p_out = &out;
630 out_channels.push_back(def);
631 }
632
633 size_t write(const uint8_t *data, size_t len) override {
634 switch(cfg.bits_per_sample){
635 case 16:
636 return writeT<int16_t>(data, len);
637 case 24:
638 return writeT<int24_t>(data, len);
639 case 32:
640 return writeT<int32_t>(data, len);
641 default:
642 return 0;
643 }
644 }
645
646protected:
648 Print *p_out = nullptr;
649 int channel;
650 };
652
653 template <typename T>
654 size_t writeT(const uint8_t *buffer, size_t size) {
655 int sample_count = size / sizeof(T);
656 int result_size = sample_count / cfg.channels;
657 T *data = (T *)buffer;
658 T result[result_size];
659
660 for (int ch = 0; ch < out_channels.size(); ch++) {
661 ChannelSelectionOutputDef &def = out_channels[ch];
662 // extract mono result
663 int i = 0;
664 for (int j = def.channel; j < sample_count; j += cfg.channels) {
665 result[i++] = data[j];
666 }
667 // write mono result
668 size_t written =
669 def.p_out->write((uint8_t *)result, result_size * sizeof(T));
670 if (written != result_size * sizeof(T)) {
671 LOGW("Could not write all samples");
672 }
673 }
674 return size;
675 }
676
677};
678
679
680} // namespace audio_tools
Supports the subscription to audio change notifications.
Definition AudioTypes.h:148
Supports changes to the sampling rate, bits and channels.
Definition AudioTypes.h:133
virtual AudioInfo audioInfoOut()
Definition AudioTypes.h:141
Abstract Audio Ouptut class.
Definition AudioOutput.h:22
virtual bool isDeletable()
If true we need to release the related memory in the destructor.
Definition AudioOutput.h:57
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition AudioOutput.h:46
virtual void writeSilence(size_t len)
Definition AudioOutput.h:63
virtual AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition AudioOutput.h:59
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition Buffers.h:59
Simple functionality to extract mono streams from a multichannel (e.g. stereo) signal.
Definition AudioOutput.h:618
void addOutput(Print &out, int channel)
Definition AudioOutput.h:626
Stream Wrapper which can be used to print the values as readable ASCII to the screen to be analyzed i...
Definition AudioOutput.h:113
virtual size_t write(const uint8_t *data, size_t len) override
Writes the data - formatted as CSV - to the output stream.
Definition AudioOutput.h:172
AudioInfo defaultConfig()
Provides the default configuration.
Definition AudioOutput.h:136
bool begin(int channels)
Starts the processing with the defined number of channels.
Definition AudioOutput.h:148
CsvOutput(Print &out, int channels=2, int buffer_size=DEFAULT_BUFFER_SIZE, bool active=true)
Constructor.
Definition AudioOutput.h:120
virtual void setAudioInfo(AudioInfo info) override
defines the number of channels
Definition AudioOutput.h:164
bool begin(AudioInfo info) override
Starts the processing with the defined number of channels.
Definition AudioOutput.h:145
bool begin() override
(Re)start (e.g. if channels is set in constructor)
Definition AudioOutput.h:155
void setDelimiter(const char *del)
Defines an alternative (column) delimiter. The default is ,.
Definition AudioOutput.h:128
const char * delimiter()
Provides the current column delimiter.
Definition AudioOutput.h:131
Creates a Hex Dump.
Definition AudioOutput.h:242
HexDumpOutput(Print &out, int buffer_size=DEFAULT_BUFFER_SIZE, bool active=true)
Constructor.
Definition AudioOutput.h:249
Writes to a preallocated memory.
Definition AudioOutput.h:565
Abstract class: Objects can be put into a pipleline.
Definition AudioOutput.h:97
virtual void setOutput(Print &out)=0
Defines/Changes the output target.
Mixing of multiple outputs to one final output.
Definition AudioOutput.h:306
void next()
Moves to the next mixing index.
Definition AudioOutput.h:494
void setIndex(int idx)
Sets the Output Stream index.
Definition AudioOutput.h:489
size_t write(int idx, const uint8_t *buffer_c, size_t bytes)
Write the data for an individual stream idx which will be mixed together.
Definition AudioOutput.h:381
size_t write(const uint8_t *data, size_t len) override
Definition AudioOutput.h:368
int available(int idx)
Provides the available bytes in the buffer.
Definition AudioOutput.h:411
bool begin(int copy_buffer_size_bytes=DEFAULT_BUFFER_SIZE, MemoryType memoryType=PS_RAM)
Starts the processing.
Definition AudioOutput.h:343
int availableForWrite() override
Provides the bytes available to write for the current stream buffer.
Definition AudioOutput.h:398
void flushMixer()
Force output to final destination.
Definition AudioOutput.h:419
void end()
Remove all input streams.
Definition AudioOutput.h:354
void setWeight(int channel, float weight)
Definition AudioOutput.h:333
int availableForWrite(int idx)
Provides the bytes available to write for the indicated stream index.
Definition AudioOutput.h:403
void resize(int size)
Resizes the buffer to the indicated number of bytes.
Definition AudioOutput.h:461
void setAutoIndex(bool flag)
Automatically increment mixing index after each write.
Definition AudioOutput.h:484
int size()
Number of stremams to which are mixed together.
Definition AudioOutput.h:362
Definition NoArduino.h:62
Implements a typed Ringbuffer.
Definition Buffers.h:308
virtual int availableForWrite() override
provides the number of entries that are available to write
Definition Buffers.h:380
virtual int available() override
provides the number of entries that are available to read
Definition Buffers.h:377
bool write(T sample) override
write add an entry to the buffer
Definition Buffers.h:200
int available() override
provides the number of entries that are available to read
Definition Buffers.h:227
T * address() override
Provides address to beginning of the buffer.
Definition Buffers.h:258
bool isFull() override
checks if the buffer is full
Definition Buffers.h:234
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
MemoryType
Memory types.
Definition AudioTypes.h:35
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition AudioTypes.h:28
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:53
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:55
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:57
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:59