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
306template <typename T>
307class OutputMixer : public Print {
308public:
309 OutputMixer() = default;
310
311 OutputMixer(Print &finalOutput, int outputStreamCount) {
312 setOutput(finalOutput);
313 setOutputCount(outputStreamCount);
314 }
315
316 void setOutput(Print &finalOutput) { p_final_output = &finalOutput; }
317
318 void setOutputCount(int count) {
319 output_count = count;
320 buffers.resize(count);
321 for (int i = 0; i < count; i++) {
322 buffers[i] = nullptr;
323 }
324 weights.resize(count);
325 for (int i = 0; i < count; i++) {
326 weights[i] = 1.0;
327 }
328
329 update_total_weights();
330 }
331
334 void setWeight(int channel, float weight) {
335 if (channel < size()) {
336 weights[channel] = weight;
337 } else {
338 LOGE("Invalid channel %d - max is %d", channel, size() - 1);
339 }
340 update_total_weights();
341 }
342
344 bool begin(int copy_buffer_size_bytes = DEFAULT_BUFFER_SIZE) {
345 is_active = true;
346 size_bytes = copy_buffer_size_bytes;
347 stream_idx = 0;
348 allocate_buffers(size_bytes);
349 return true;
350 }
351
353 void end() {
354 total_weights = 0.0;
355 is_active = false;
356 // release memory for buffers
357 free_buffers();
358 }
359
361 int size() { return output_count; }
362
363 size_t write(uint8_t) override { return 0; }
364
367 size_t write(const uint8_t *data, size_t len) override {
368 size_t result = write(stream_idx, data, len);
369 // after writing the last stream we flush
370 if (is_auto_index) {
371 stream_idx++;
372 if (stream_idx >= output_count) {
373 flushMixer();
374 }
375 }
376 return result;
377 }
378
380 size_t write(int idx, const uint8_t *buffer_c, size_t bytes) {
381 LOGD("write idx %d: %d", idx, (int)bytes);
382 size_t result = 0;
383 BaseBuffer<T> *p_buffer = idx < output_count ? buffers[idx] : nullptr;
384 assert(p_buffer != nullptr);
385 size_t samples = bytes / sizeof(T);
386 if (p_buffer->availableForWrite() >= samples) {
387 result = p_buffer->writeArray((T *)buffer_c, samples) * sizeof(T);
388 } else {
389 LOGW("Available Buffer %d too small %d: requested: %d -> increase the "
390 "buffer size", (int) idx,
391 static_cast<int>(p_buffer->availableForWrite()*sizeof(T)), (int)bytes);
392 }
393 return result;
394 }
395
397 int availableForWrite() override {
398 return is_active ? availableForWrite(stream_idx) : 0;
399 }
400
402 int availableForWrite(int idx) {
403 BaseBuffer<T> *p_buffer = buffers[idx];
404 if (p_buffer == nullptr)
405 return 0;
406 return p_buffer->availableForWrite() * sizeof(T);
407 }
408
410 int available(int idx){
411 BaseBuffer<T> *p_buffer = buffers[idx];
412 if (p_buffer == nullptr)
413 return 0;
414 return p_buffer->available() * sizeof(T);
415 }
416
418 int availablePercent(int idx){
419 return 100.0 * available(idx) / size_bytes;
420 }
421
423 void flushMixer() {
424 LOGD("flush");
425 bool result = false;
426
427 // determine ringbuffer with mininum available data
428 size_t samples = availableSamples();
429 // sum up samples
430 if (samples > 0) {
431 result = true;
432 // mix data from ringbuffers to output
433 output.resize(samples);
434 memset(output.data(), 0, samples * sizeof(T));
435 for (int j = 0; j < output_count; j++) {
436 float weight = weights[j];
437 // sum up input samples to result samples
438 for (int i = 0; i < samples; i++) {
439 T sample = 0;;
440 buffers[j]->read(sample);
441 output[i] += weight * sample / total_weights;
442 }
443 }
444
445 // write output
446 LOGD("write to final out: %d", static_cast<int>(samples * sizeof(T)));
447 p_final_output->write((uint8_t *)output.data(), samples * sizeof(T));
448 }
449 stream_idx = 0;
450 return;
451 }
452
453 int availableSamples() {
454 size_t samples = 0;
455 for (int j = 0; j < output_count; j++) {
456 int available_samples = buffers[j]->available();
457 if (available_samples > 0){
458 samples = MIN(size_bytes / sizeof(T), (size_t)available_samples);
459 }
460 }
461 return samples;
462 }
463
465 void resize(int size) {
466 if (size != size_bytes) {
467 allocate_buffers(size);
468 }
469 size_bytes = size;
470 }
471
472 size_t writeSilence(size_t bytes) {
473 if (bytes == 0) return 0;
474 uint8_t silence[bytes];
475 memset(silence, 0, bytes);
476 return write(stream_idx, silence, bytes);
477 }
478
479 size_t writeSilence(int idx, size_t bytes){
480 if (bytes == 0) return 0;
481 uint8_t silence[bytes];
482 memset(silence, 0, bytes);
483 return write(idx, silence, bytes);
484 }
485
487 void setAutoIndex(bool flag){
488 is_auto_index = flag;
489 }
490
492 void setIndex(int idx){
493 stream_idx = idx;
494 }
495
497 void next() {
498 stream_idx++;
499 }
500
503 create_buffer_cb = cb;
504 }
505
508 return idx < output_count ? buffers[idx] : nullptr;
509 }
510
511protected:
512 Vector<BaseBuffer<T> *> buffers{0};
513 Vector<T> output{0};
514 Vector<float> weights{0};
515 Print *p_final_output = nullptr;
516 float total_weights = 0.0;
517 bool is_active = false;
518 int stream_idx = 0;
519 int size_bytes = 0;
520 int output_count = 0;
521 void *p_memory = nullptr;
522 bool is_auto_index = true;
523 BaseBuffer<T>* (*create_buffer_cb)(int size) = create_buffer;
524
525 static BaseBuffer<T>* create_buffer(int size) {
526 return new RingBuffer<T>(size / sizeof(T));
527 }
528
529 void update_total_weights() {
530 total_weights = 0.0;
531 for (int j = 0; j < weights.size(); j++) {
532 total_weights += weights[j];
533 }
534 }
535
536 void allocate_buffers(int size) {
537 // allocate ringbuffers for each output
538 for (int j = 0; j < output_count; j++) {
539 if (buffers[j] != nullptr) {
540 delete buffers[j];
541 }
542 buffers[j] = create_buffer(size);
543 }
544 }
545
546 void free_buffers() {
547 // allocate ringbuffers for each output
548 for (int j = 0; j < output_count; j++) {
549 if (buffers[j] != nullptr) {
550 delete buffers[j];
551 buffers[j] = nullptr;
552 }
553 }
554 }
555};
556
557
562class MemoryOutput : public AudioOutput {
563public:
564 MemoryOutput(uint8_t *start, int len) {
565 p_start = start;
566 p_next = start;
567 max_size = len;
568 is_active = true;
569 if (p_next == nullptr) {
570 LOGE("start must not be null");
571 }
572 }
573
574 bool begin() override {
575 is_active = true;
576 p_next = p_start;
577 pos = 0;
578 return true;
579 }
580
581 size_t write(const uint8_t *data, size_t len) override {
582 if (p_next == nullptr)
583 return 0;
584 if (pos + len <= max_size) {
585 memcpy(p_next, data, len);
586 pos += len;
587 p_next += len;
588 return len;
589 } else {
590 LOGE("Buffer too small: pos:%d, size: %d ", pos, (int)max_size);
591 return 0;
592 }
593 }
594
595 int availableForWrite() override { return max_size - pos; }
596
597 int size() { return max_size; }
598
599protected:
600 int pos = 0;
601 uint8_t *p_start = nullptr;
602 uint8_t *p_next = nullptr;
603 size_t max_size;
604};
605
606
616public:
617 ChannelSplitOutput() = default;
618
619 ChannelSplitOutput(Print &out, int channel) { addOutput(out, channel); }
620
623 void addOutput(Print &out, int channel) {
625 def.channel = channel;
626 def.p_out = &out;
627 out_channels.push_back(def);
628 }
629
630 size_t write(const uint8_t *data, size_t len) override {
631 switch(cfg.bits_per_sample){
632 case 16:
633 return writeT<int16_t>(data, len);
634 case 24:
635 return writeT<int24_t>(data, len);
636 case 32:
637 return writeT<int32_t>(data, len);
638 default:
639 return 0;
640 }
641 }
642
643protected:
645 Print *p_out = nullptr;
646 int channel;
647 };
649
650 template <typename T>
651 size_t writeT(const uint8_t *buffer, size_t size) {
652 int sample_count = size / sizeof(T);
653 int result_size = sample_count / cfg.channels;
654 T *data = (T *)buffer;
655 T result[result_size];
656
657 for (int ch = 0; ch < out_channels.size(); ch++) {
658 ChannelSelectionOutputDef &def = out_channels[ch];
659 // extract mono result
660 int i = 0;
661 for (int j = def.channel; j < sample_count; j += cfg.channels) {
662 result[i++] = data[j];
663 }
664 // write mono result
665 size_t written =
666 def.p_out->write((uint8_t *)result, result_size * sizeof(T));
667 if (written != result_size * sizeof(T)) {
668 LOGW("Could not write all samples");
669 }
670 }
671 return size;
672 }
673
674};
675
676
677} // 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
Shared functionality of all buffers.
Definition Buffers.h:22
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition Buffers.h:55
virtual int availableForWrite()=0
provides the number of entries that are available to write
virtual int available()=0
provides the number of entries that are available to read
Simple functionality to extract mono streams from a multichannel (e.g. stereo) signal.
Definition AudioOutput.h:615
void addOutput(Print &out, int channel)
Definition AudioOutput.h:623
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:562
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. By default a RingBuffer is used as buffer type.
Definition AudioOutput.h:307
void next()
Moves to the next mixing index.
Definition AudioOutput.h:497
void setIndex(int idx)
Sets the Output Stream index.
Definition AudioOutput.h:492
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:380
size_t write(const uint8_t *data, size_t len) override
Definition AudioOutput.h:367
int available(int idx)
Provides the available bytes in the buffer.
Definition AudioOutput.h:410
BaseBuffer< T > * getBuffer(int idx)
Provides the write buffer for the indicated index.
Definition AudioOutput.h:507
void setCreateBufferCallback(BaseBuffer< T > *(*cb)(int size))
Define callback to allocate custum buffer types.
Definition AudioOutput.h:502
int availableForWrite() override
Provides the bytes available to write for the current stream buffer.
Definition AudioOutput.h:397
int availablePercent(int idx)
Provides the % fill level of the buffer for the indicated index.
Definition AudioOutput.h:418
void flushMixer()
Force output to final destination.
Definition AudioOutput.h:423
void end()
Remove all input streams.
Definition AudioOutput.h:353
void setWeight(int channel, float weight)
Definition AudioOutput.h:334
int availableForWrite(int idx)
Provides the bytes available to write for the indicated stream index.
Definition AudioOutput.h:402
void resize(int size)
Resizes the buffer to the indicated number of bytes.
Definition AudioOutput.h:465
void setAutoIndex(bool flag)
Automatically increment mixing index after each write.
Definition AudioOutput.h:487
bool begin(int copy_buffer_size_bytes=DEFAULT_BUFFER_SIZE)
Starts the processing.
Definition AudioOutput.h:344
int size()
Number of stremams to which are mixed together.
Definition AudioOutput.h:361
Definition NoArduino.h:62
bool write(T sample) override
write add an entry to the buffer
Definition Buffers.h:196
int available() override
provides the number of entries that are available to read
Definition Buffers.h:223
T * address() override
Provides address to beginning of the buffer.
Definition Buffers.h:254
bool isFull() override
checks if the buffer is full
Definition Buffers.h:230
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
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