arduino-audio-tools
Loading...
Searching...
No Matches
AudioIO.h
Go to the documentation of this file.
1#pragma once
4
5#ifndef MAX_ZERO_READ_COUNT
6#define MAX_ZERO_READ_COUNT 3
7#endif
8
9#ifndef CHANNEL_SELECT_BUFFER_SIZE
10#define CHANNEL_SELECT_BUFFER_SIZE 256
11#endif
12
13namespace audio_tools {
21template <class T>
23 public:
28 void begin(T* transform, Stream* source) {
29 TRACED();
30 active = true;
31 is_eof = false;
34 p_stream = source;
35 p_transform = transform;
36 if (transform == nullptr) {
37 LOGE("transform is NULL");
38 active = false;
39 }
40 if (p_stream == nullptr) {
41 LOGE("p_stream is NULL");
42 active = false;
43 }
44 }
45
46 size_t readBytes(uint8_t* data, size_t len) {
47 LOGD("TransformationReader::readBytes: %d", (int)len);
48 if (!active) {
49 LOGE("inactive");
50 return 0;
51 }
52 if (p_stream == nullptr) {
53 LOGE("p_stream is NULL");
54 return 0;
55 }
56
57 setupBuffers(len);
58
59 if (!is_eof) {
60 fillResultQueue(len);
61 }
62
63 int result_len = min((int)len, result_queue.available());
65 LOGD("TransformationReader::readBytes: %d -> %d", (int)len, result_len);
67 return result_len;
68 }
69
72 int available() {
73 if (!active || p_stream == nullptr || p_transform == nullptr) return 0;
74 if (is_eof) return result_queue.available();
77 int available_bytes = result_queue.available();
78 LOGD("TransformationReader::available: %d", available_bytes);
79 return available_bytes > max_read_size ? max_read_size : available_bytes;
80 }
81
82 void end() {
84 buffer.resize(0);
86 active = false;
87 }
88
90 void setResultQueueFactor(int factor) { result_queue_factor = factor; }
91
93 void resizeResultQueue(int size) {
96 }
97
98 void setMaxReadSize(int size) { max_read_size = size; }
99
100 size_t getTotalBytesRead() const { return total_bytes_read; }
101
102 protected:
105 Stream* p_stream = nullptr;
106 Vector<uint8_t> buffer{0}; // we allocate memory only when needed
107 T* p_transform = nullptr;
108 bool active = false;
112 bool is_eof = false;
114
116 void resizeReadBuffer(int size) { buffer.resize(size); }
117
118 void setupBuffers(size_t len) {
119 if (len == last_setup_buffer_size) return;
120 LOGD("setupBuffers: %d", (int)len);
121 float byte_factor = p_transform->getByteFactor();
122 if (byte_factor <= 0.0f) {
123 LOGE("Invalid byte factor: %f", byte_factor);
124 byte_factor = 1.0f;
125 }
126
127 // we read half the necessary bytes
128 int size = (0.5f / byte_factor * len);
129 // process full samples/frames
130 size = size / 4 * 4;
131 if (size <= 0) size = 4;
132 if (buffer.size() < size) {
133 LOGI("read size: %d", size);
134 buffer.resize(size);
135 }
136
137 if (result_queue_buffer.size() == 0) {
138 // make sure that the ring buffer is big enough
139 int rb_size = len * result_queue_factor;
140 LOGI("buffer size: %d", rb_size);
143 }
145 }
146
149 void fillResultQueue(size_t len) {
150 if (is_eof) return;
151 if (result_queue.available() >= len) return;
152 LOGD("fillResultQueue: %d", (int)len);
153
154 // Detect misconfigured buffer: if the ring buffer capacity is smaller than
155 // the requested len bytes we can never satisfy the condition and will loop
156 // forever. Issue an error and bail out early.
157 if ((int)len > result_queue_buffer.size()) {
158 LOGE("fillResultQueue: result_queue_buffer too small: %d < %d. "
159 "Increase result_queue_factor or call resizeReadResultQueue().",
160 result_queue_buffer.size(), (int)len);
161 return;
162 }
163
164 Print* tmp = setupOutput();
165 int zero_count = 0;
166 while (result_queue.available() < len) {
167 // Detect buffer-full stall: if we can't write any more data but the
168 // queue is still below len, we must stop to avoid an endless loop.
170 LOGE("fillResultQueue: result_queue full (%d) but target not reached "
171 "(%d/%d). Increase result_queue_factor or call "
172 "resizeReadResultQueue().",
174 break;
175 }
176 int read_size = buffer.size();
177 int read_eff = p_stream->readBytes(buffer.data(), read_size);
178 LOGD("readBytes from source: %d -> %d", read_size,read_eff);
179 if (read_eff > 0) {
180 zero_count = 0; // reset 0 count
181 if (read_eff != buffer.size()) {
182 LOGD("readBytes %d -> %d", buffer.size(), read_eff);
183 }
184 int write_eff = p_transform->write(buffer.data(), read_eff);
185 if (write_eff != read_eff) {
186 LOGE("TransformationReader::write %d -> %d", read_eff, write_eff);
187 }
188 } else {
189 // limit the number of reads which provide 0;
191 is_eof = true;
192 // Flush any buffered/final encoder bytes into result_queue.
193 p_transform->flush();
194 break;
195 }
196 // wait for some more data
197 delay(5);
198 }
199 }
200 LOGD("fillResultQueue available: %d", result_queue.available());
201 restoreOutput(tmp);
202 }
203
208 Print* result = p_transform->getPrint();
209 p_transform->setOutput((Print&)result_queue);
210
211 return result;
212 }
215 void restoreOutput(Print* out) {
216 if (out) p_transform->setOutput(*out);
217 }
218};
219
227 public:
228 virtual void setStream(Stream& stream) override {
229 TRACED();
230 p_stream = &stream;
231 p_print = &stream;
232 }
233
234 virtual void setStream(AudioStream& stream) {
235 TRACED();
236 p_stream = &stream;
237 p_print = &stream;
238 // setNotifyOnOutput(stream);
239 addNotifyAudioChange(stream);
240 }
241
242 virtual void setOutput(AudioOutput& print) {
243 TRACED();
244 p_print = &print;
246 }
247
248 virtual void setOutput(Print& print) override {
249 TRACED();
250 p_print = &print;
251 }
252
253 virtual Print* getPrint() { return p_print; }
254
255 virtual Stream* getStream() { return p_stream; }
256
257 size_t readBytes(uint8_t* data, size_t len) override {
258 LOGD("ReformatBaseStream::readBytes: %d", (int)len);
259 return reader.readBytes(data, len);
260 }
261
262 int available() override {
263 return reader.available();
264 }
265
266 int availableForWrite() override {
267 return DEFAULT_BUFFER_SIZE; // reader.availableForWrite();
268 }
269
270 virtual float getByteFactor() = 0;
271
277 virtual void flush() {}
278
279 void end() override {
280 TRACED();
282 reader.end();
283 }
284
287 void resizeReadResultQueue(int size) { reader.resizeResultQueue(size); }
288
290 void setReadResultQueueSize(int size) { reader.resizeResultQueue(size); }
291
293 void setMaxReadSize(int size) { reader.setMaxReadSize(size); }
294
299
300
301 protected:
303 Stream* p_stream = nullptr;
304 Print* p_print = nullptr;
305
306 void setupReader() {
307 if (getStream() != nullptr) {
308 reader.begin(this, getStream());
309 }
310 }
311};
312
318
324 public:
327 void setStream(Print& out) { p_print = &out; }
328 void setAudioInfo(AudioInfo info) { cfg = info; }
329 size_t write(const uint8_t* data, size_t len) {
330 return p_print->write(data, len);
331 }
333 virtual bool isDeletable() { return true; }
334
335 AudioInfo audioInfo() { return cfg; }
336
337 protected:
338 Print* p_print = nullptr;
340};
341
347 public:
349
351
352 void setStream(AudioStream& stream) { p_stream = &stream; }
353
354 void setAudioInfo(AudioInfo info) override { p_stream->setAudioInfo(info); }
355
356 AudioInfo audioInfo() override { return p_stream->audioInfo(); }
357
358 size_t write(const uint8_t* data, size_t len) override {
359 return p_stream->write(data, len);
360 }
361
362 int availableForWrite() override { return p_stream->availableForWrite(); }
363
364 bool begin() override { return p_stream->begin(); }
365
366 void end() override { p_stream->end(); }
367
369 virtual bool isDeletable() override { return true; }
370
371 operator bool() override { return *p_stream; }
372
373 protected:
375};
376
382 public:
384
386
387 void setOutput(AudioOutput& stream) { p_stream = &stream; }
388
390
391 AudioInfo audioInfo() override { return p_stream->audioInfo(); }
392
393 size_t write(const uint8_t* data, size_t len) override {
394 return p_stream->write(data, len);
395 }
396
397 bool begin() override { return p_stream->begin(); }
398
399 void end() override { p_stream->end(); }
400
402 virtual bool isDeletable() { return true; }
403
404 operator bool() override { return *p_stream; }
405
406 protected:
408};
409
417 public:
419 MultiOutput() = default;
420
421 MultiOutput(Print& out) { add(out); }
422
425
427
433
439
443 add(out1);
444 add(out2);
445 }
446
447 virtual ~MultiOutput() { clear(); }
448
450 void add(AudioOutput& out) { vector.push_back(&out); }
451
453 void add(AudioStream& stream) {
456 vector.push_back(out);
457 }
458
459 void add(Print& print) {
461 vector.push_back(out);
462 }
463
464 void flush() {
465 for (int j = 0; j < vector.size(); j++) {
466 vector[j]->flush();
467 }
468 }
469
471 for (int j = 0; j < vector.size(); j++) {
472 vector[j]->setAudioInfo(info);
473 }
474 }
475
476 size_t write(const uint8_t* data, size_t len) override {
477 for (auto& out : vector) {
478 int open = len;
479 int start = 0;
480 // create copy of data to avoid that one output changes the data for the
481 // other outputs
482 uint8_t copy[len];
483 memcpy(copy, data, len);
484 while (open > 0) {
485 int written = out->write(copy + start, open);
486 open -= written;
487 start += written;
488 }
489 }
490 return len;
491 }
492
493 size_t write(uint8_t ch) override {
494 for (int j = 0; j < vector.size(); j++) {
495 int open = 1;
496 while (open > 0) {
497 open -= vector[j]->write(ch);
498 }
499 }
500 return 1;
501 }
502
504 void clear() {
505 for (auto& tmp : vector) {
506 if (tmp != nullptr && tmp->isDeletable()) {
507 delete tmp;
508 }
509 }
510 vector.clear();
511 }
512
513 protected:
515
517 void setOutput(Print& out) { add(out); }
518};
519
530 public:
531 TimedStream() = default;
532
533 TimedStream(AudioStream& io, long startSeconds = 0, long endSeconds = -1) {
534 p_stream = &io;
535 p_print = &io;
536 p_info = &io;
539 }
540
542 p_print = &o;
543 p_info = &o;
546 }
547
548 TimedStream(Stream& io, long startSeconds = 0, long endSeconds = -1) {
549 p_stream = &io;
550 p_print = &io;
553 }
554
555 TimedStream(Print& o, long startSeconds = 0, long endSeconds = -1) {
556 p_print = &o;
559 }
560
567
570 start_ms = ms;
572 }
573
580
583 end_ms = ms;
585 }
586
588 bool isPlaying() {
589 if (current_bytes < start_bytes) return false;
590 if (end_bytes > 0 && current_bytes > end_bytes) return false;
591 return true;
592 }
593
598
601 return begin();
602 }
603
604 bool begin() override {
606 current_bytes = 0;
607 LOGI("byte range %u - %u", (unsigned)start_bytes, (unsigned)end_bytes);
608 return true;
609 }
610
611 operator bool() override { return isActive(); }
612
616 size_t readBytes(uint8_t* data, size_t len) override {
617 // if reading is not supported we stop
618 if (p_stream == nullptr) return 0;
619 // Positioin to start
622 }
623 // if we are past the end we stop
624 if (!isActive()) return 0;
625 // read the data now
626 size_t result = 0;
627 do {
628 result = p_stream->readBytes(data, len);
629 current_bytes += len;
630 // ignore data before start time
631 } while (result > 0 && current_bytes < start_bytes);
632 return isPlaying() ? result : 0;
633 }
634
636 size_t write(const uint8_t* data, size_t len) override {
637 if (current_bytes >= end_bytes) return 0;
638 current_bytes += len;
639 if (current_bytes < start_bytes) return len;
640 return p_print->write(data, len);
641 }
642
644 int available() override {
645 if (p_stream == nullptr) return 0;
646 return current_bytes < end_bytes ? p_stream->available() : 0;
647 }
648
655
656 int availableForWrite() override {
658 }
659
662 void setCompressionRatio(float ratio) { compression_ratio = ratio; }
663
668
669 void setOutput(Print& out) override { p_print = &out; }
670
671 void setStream(Stream& stream) override {
672 p_print = &stream;
673 p_stream = &stream;
674 }
675
677 p_print = &out;
678 p_info = &out;
679 }
680
682 p_print = &out;
683 p_info = &out;
684 }
685
686 void setStream(AudioStream& stream) {
687 p_print = &stream;
688 p_stream = &stream;
689 p_info = &stream;
690 }
691
692 size_t size() { return end_bytes - start_bytes; }
693
694 protected:
695 Stream* p_stream = nullptr;
696 Print* p_print = nullptr;
703 float compression_ratio = 1.0;
704
706 int open = len;
707 uint8_t buffer[1024];
708 while (open > 0) {
709 int toread = min(1024, open);
710 p_stream->readBytes(buffer, toread);
711 open -= toread;
712 }
713 current_bytes += len;
714 LOGD("consumed %u -> %u", (unsigned)len, (unsigned)current_bytes);
715 }
716
718 float bytes_per_second = bytesPerSecond();
719 if (bytes_per_second > 0) {
720 start_bytes = bytes_per_second * start_ms / compression_ratio / 1000;
721 end_bytes = bytes_per_second * end_ms / compression_ratio / 1000;
722 } else {
723 LOGE("AudioInfo not defined");
724 }
725 }
726};
727
738 public:
740
741 bool begin(AudioInfo info) override {
742 setAudioInfo(info);
743 return begin();
744 }
745
746 bool begin() override {
748 // make sure that selected channels are valid
749 for (auto& out : out_channels) {
750 for (auto& ch : out.channels) {
751 if (ch > cfg.channels - 1) {
752 LOGE("Channel '%d' not valid for max %d channels", ch, cfg.channels);
753 return false;
754 }
755 }
756 }
757 return true;
758 }
759
762 void addOutput(AudioOutput& out, uint16_t channel) {
763 Vector<uint16_t> channels;
764 channels.push_back(channel);
766 def.channels = channels;
767 def.p_out = &out;
768 def.p_audio_info = &out;
769 out_channels.push_back(def);
770 }
771
774 void addOutput(AudioStream& out, uint16_t channel) {
775 Vector<uint16_t> channels;
776 channels.push_back(channel);
778 def.channels = channels;
779 def.p_out = &out;
780 def.p_audio_info = &out;
781 out_channels.push_back(def);
782 }
783
786 void addOutput(Print& out, uint16_t channel) {
787 Vector<uint16_t> channels;
788 channels.push_back(channel);
790 def.channels = channels;
791 def.p_out = &out;
792 out_channels.push_back(def);
793 }
794
797 void addOutput(Print& out, uint16_t left, uint16_t right) {
798 Vector<uint16_t> channels;
799 channels.push_back(left);
800 channels.push_back(right);
802 def.channels = channels;
803 def.p_out = &out;
804 out_channels.push_back(def);
805 }
806
809 void addOutput(AudioOutput& out, uint16_t left, uint16_t right) {
810 Vector<uint16_t> channels;
811 channels.push_back(left);
812 channels.push_back(right);
814 def.channels = channels;
815 def.p_out = &out;
816 def.p_audio_info = &out;
817 out_channels.push_back(def);
818 }
819
822 void addOutput(AudioStream& out, uint16_t left, uint16_t right) {
823 Vector<uint16_t> channels;
824 channels.push_back(left);
825 channels.push_back(right);
827 def.channels = channels;
828 def.p_out = &out;
829 def.p_audio_info = &out;
830 out_channels.push_back(def);
831 }
832
833 size_t write(const uint8_t* data, size_t len) override {
834 if (!is_active) return false;
835 LOGD("write %d", (int)len);
836 switch (cfg.bits_per_sample) {
837 case 16:
838 return writeT<int16_t>(data, len);
839 case 24:
840 return writeT<int24_t>(data, len);
841 case 32:
842 return writeT<int32_t>(data, len);
843 default:
844 return 0;
845 }
846 }
847
848 void setAudioInfo(AudioInfo ai) override {
849 this->cfg = ai;
850 // notifyAudioChange(ai);
851 for (auto& info : out_channels) {
852 auto p_notify = info.p_audio_info;
853 if (p_notify != nullptr) {
854 AudioInfo result{ai};
855 result.channels = info.channels.size();
856 p_notify->setAudioInfo(result);
857 }
858 }
859 }
860
861 protected:
869
870 template <typename T>
871 size_t writeT(const uint8_t* buffer, size_t size) {
872 if (!is_active) return 0;
873 int sample_count = size / sizeof(T);
874 // int result_size = sample_count / cfg.channels;
875 T* data = (T*)buffer;
876
877 for (int i = 0; i < sample_count; i += cfg.channels) {
878 T* frame = data + i;
879 for (auto& out : out_channels) {
880 T out_frame[out.channels.size()];
881 int ch_out = 0;
882 for (auto& ch : out.channels) {
883 // make sure we have a valid channel
884 int channel = (ch < cfg.channels) ? ch : cfg.channels - 1;
885 out_frame[ch_out++] = frame[channel];
886 }
887 // write to buffer
888 size_t written = out.buffer.writeArray((const uint8_t*)&out_frame,
889 sizeof(out_frame));
890 // write buffer to final output
891 if (out.buffer.availableForWrite() < sizeof(out_frame)) {
892 out.p_out->write(out.buffer.data(), out.buffer.available());
893 out.buffer.reset();
894 }
895 // if (written != sizeof(out_frame)) {
896 // LOGW("Could not write all samples %d -> %d", sizeof(out_frame),
897 // written);
898 // }
899 }
900 }
901 return size;
902 }
903
906 for (auto& channels_select : out_channels) {
907 if (channels_select.p_out == out) return channels_select.channels.size();
908 }
909 return defaultChannels;
910 }
911};
912
913} // namespace audio_tools
#define CHANNEL_SELECT_BUFFER_SIZE
Definition AudioIO.h:10
#define MAX_ZERO_READ_COUNT
Definition AudioIO.h:6
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define DEFAULT_BUFFER_SIZE
Definition avr.h:20
Wrapper which converts a AudioStream to a AudioOutput.
Definition AudioIO.h:381
void setOutput(AudioOutput &stream)
Definition AudioIO.h:387
virtual bool isDeletable()
If true we need to release the related memory in the destructor.
Definition AudioIO.h:402
AdapterAudioOutputToAudioStream(AudioOutput &stream)
Definition AudioIO.h:385
void end() override
Definition AudioIO.h:399
size_t write(const uint8_t *data, size_t len) override
Definition AudioIO.h:393
AudioOutput * p_stream
Definition AudioIO.h:407
bool begin() override
Definition AudioIO.h:397
void setAudioInfo(AudioInfo info) override
Defines the input AudioInfo.
Definition AudioIO.h:389
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition AudioIO.h:391
Wrapper which converts a AudioStream to a AudioOutput.
Definition AudioIO.h:346
void setStream(AudioStream &stream)
Definition AudioIO.h:352
virtual bool isDeletable() override
If true we need to release the related memory in the destructor.
Definition AudioIO.h:369
void end() override
Definition AudioIO.h:366
size_t write(const uint8_t *data, size_t len) override
Definition AudioIO.h:358
AdapterAudioStreamToAudioOutput(AudioStream &stream)
Definition AudioIO.h:350
int availableForWrite() override
Definition AudioIO.h:362
AudioStream * p_stream
Definition AudioIO.h:374
bool begin() override
Definition AudioIO.h:364
void setAudioInfo(AudioInfo info) override
Defines the input AudioInfo.
Definition AudioIO.h:354
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition AudioIO.h:356
Wrapper which converts a Print to a AudioOutput.
Definition AudioIO.h:323
AdapterPrintToAudioOutput(Print &print)
Definition AudioIO.h:326
AudioInfo cfg
Definition AudioIO.h:339
virtual bool isDeletable()
If true we need to release the related memory in the destructor.
Definition AudioIO.h:333
void setAudioInfo(AudioInfo info)
Defines the input AudioInfo.
Definition AudioIO.h:328
void setStream(Print &out)
Definition AudioIO.h:327
AudioInfo audioInfo()
provides the actual input AudioInfo
Definition AudioIO.h:335
Print * p_print
Definition AudioIO.h:338
size_t write(const uint8_t *data, size_t len)
Definition AudioIO.h:329
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition AudioTypes.h:153
Supports changes to the sampling rate, bits and channels.
Definition AudioTypes.h:135
virtual void setAudioInfo(AudioInfo info)=0
Defines the input AudioInfo.
Base class for Output Adpapters.
Definition AudioIO.h:317
Abstract Audio Ouptut class.
Definition AudioOutput.h:25
SingleBuffer< uint8_t > tmp
Definition AudioOutput.h:89
AudioInfo cfg
Definition AudioOutput.h:88
bool is_active
Definition AudioOutput.h:90
virtual bool begin(AudioInfo info)
Definition AudioOutput.h:73
virtual bool begin()
Definition AudioOutput.h:78
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition AudioOutput.h:49
virtual void end()
Definition AudioOutput.h:82
virtual size_t write(const uint8_t *data, size_t len) override=0
virtual AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition AudioOutput.h:62
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:123
virtual size_t write(const uint8_t *data, size_t len) override
Definition BaseStream.h:149
AudioInfo info
Definition BaseStream.h:174
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition BaseStream.h:131
virtual AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition BaseStream.h:154
void clear()
same as reset
Definition Buffers.h:95
virtual bool begin()
Definition BaseStream.h:43
virtual int availableForWrite() override
Definition BaseStream.h:60
virtual void end()
Definition BaseStream.h:44
Flexible functionality to extract one or more channels from a multichannel signal....
Definition AudioIO.h:737
void addOutput(Print &out, uint16_t left, uint16_t right)
Definition AudioIO.h:797
void setAudioInfo(AudioInfo ai) override
Defines the input AudioInfo.
Definition AudioIO.h:848
void addOutput(Print &out, uint16_t channel)
Definition AudioIO.h:786
Vector< ChannelSelectionOutputDef > out_channels
Definition AudioIO.h:868
void addOutput(AudioOutput &out, uint16_t channel)
Definition AudioIO.h:762
int getChannels(Print *out, int defaultChannels)
Determine number of channels for destination.
Definition AudioIO.h:905
size_t write(const uint8_t *data, size_t len) override
Definition AudioIO.h:833
size_t writeT(const uint8_t *buffer, size_t size)
Definition AudioIO.h:871
bool begin(AudioInfo info) override
Definition AudioIO.h:741
bool begin() override
Definition AudioIO.h:746
void addOutput(AudioStream &out, uint16_t channel)
Definition AudioIO.h:774
void addOutput(AudioStream &out, uint16_t left, uint16_t right)
Definition AudioIO.h:822
void addOutput(AudioOutput &out, uint16_t left, uint16_t right)
Definition AudioIO.h:809
Abstract class: Objects can be put into a pipleline.
Definition AudioOutput.h:100
Abstract class: Objects can be put into a pipleline.
Definition AudioStreams.h:68
Replicates the output to multiple destinations.
Definition AudioIO.h:416
void add(AudioOutput &out)
Add an additional AudioOutput output.
Definition AudioIO.h:450
MultiOutput(Print &out)
Definition AudioIO.h:421
Vector< AudioOutput * > vector
Definition AudioIO.h:514
size_t write(uint8_t ch) override
Definition AudioIO.h:493
void setAudioInfo(AudioInfo info)
Defines the input AudioInfo.
Definition AudioIO.h:470
void add(AudioStream &stream)
Add an AudioStream to the output.
Definition AudioIO.h:453
MultiOutput(AudioStream &out1, AudioStream &out2)
Defines a MultiOutput with 2 final outputs.
Definition AudioIO.h:435
size_t write(const uint8_t *data, size_t len) override
Definition AudioIO.h:476
void add(Print &print)
Definition AudioIO.h:459
MultiOutput(AudioOutput &out1, AudioOutput &out2)
Defines a MultiOutput with 2 final outputs.
Definition AudioIO.h:429
MultiOutput(AudioOutput &out)
Defines a MultiOutput with a single final outputs,.
Definition AudioIO.h:424
MultiOutput(AudioStream &out)
Definition AudioIO.h:426
virtual ~MultiOutput()
Definition AudioIO.h:447
void clear()
Removes all output components.
Definition AudioIO.h:504
void flush()
Definition AudioIO.h:464
void setOutput(Print &out)
support for Pipleline
Definition AudioIO.h:517
MultiOutput(Print &out1, Print &out2)
Definition AudioIO.h:442
MultiOutput()=default
Defines a MultiOutput with no final output: Define your outputs with add()
Definition NoArduino.h:62
virtual int availableForWrite()
Definition NoArduino.h:134
virtual size_t write(const uint8_t *data, size_t len)
Definition NoArduino.h:126
Stream class which stores the data in a temporary queue buffer. The queue can be consumed e....
Definition BaseStream.h:362
virtual bool begin() override
Activates the output.
Definition BaseStream.h:390
virtual size_t readBytes(uint8_t *data, size_t len) override
Definition BaseStream.h:448
int available() override
Definition BaseStream.h:414
Base class for chained converting streams.
Definition AudioIO.h:226
void setMaxReadSize(int size)
Defines the read buffer size for individual reads: same as transformationReader()....
Definition AudioIO.h:293
virtual void setStream(AudioStream &stream)
Definition AudioIO.h:234
virtual void setStream(Stream &stream) override
Defines/Changes the input & output.
Definition AudioIO.h:228
TransformationReader< ReformatBaseStream > reader
Definition AudioIO.h:302
virtual float getByteFactor()=0
virtual void setOutput(Print &print) override
Defines/Changes the output target.
Definition AudioIO.h:248
size_t readBytes(uint8_t *data, size_t len) override
Definition AudioIO.h:257
void end() override
Definition AudioIO.h:279
int available() override
Definition AudioIO.h:262
virtual Stream * getStream()
Definition AudioIO.h:255
virtual void setOutput(AudioOutput &print)
Definition AudioIO.h:242
Stream * p_stream
Definition AudioIO.h:303
virtual TransformationReader< ReformatBaseStream > & transformationReader()
Provides access to the TransformationReader.
Definition AudioIO.h:296
int availableForWrite() override
Definition AudioIO.h:266
void setReadResultQueueSize(int size)
same as resizeReadResultQueue(size)
Definition AudioIO.h:290
virtual Print * getPrint()
Definition AudioIO.h:253
Print * p_print
Definition AudioIO.h:304
virtual void flush()
Definition AudioIO.h:277
void setupReader()
Definition AudioIO.h:306
void resizeReadResultQueue(int size)
Definition AudioIO.h:287
Implements a typed Ringbuffer.
Definition Buffers.h:341
virtual size_t size() override
Returns the maximum capacity of the buffer.
Definition Buffers.h:428
virtual bool resize(int len)
Resizes the buffer if supported: returns false if not supported.
Definition Buffers.h:418
A simple Buffer implementation which just uses a (dynamically sized) array.
Definition Buffers.h:172
Definition NoArduino.h:142
virtual size_t readBytes(uint8_t *data, size_t len)
Definition NoArduino.h:147
virtual int available()
Definition NoArduino.h:146
AudioStream class that can define a start and (an optional) stop time Usually it is used to wrap an A...
Definition AudioIO.h:529
TimedStream(Print &o, long startSeconds=0, long endSeconds=-1)
Definition AudioIO.h:555
void setOutput(Print &out) override
Defines/Changes the output target.
Definition AudioIO.h:669
size_t size()
Definition AudioIO.h:692
void setStream(Stream &stream) override
Defines/Changes the input & output.
Definition AudioIO.h:671
uint32_t end_ms
Definition AudioIO.h:699
void setEndSec(uint32_t endSeconds)
Definition AudioIO.h:576
uint32_t start_bytes
Definition AudioIO.h:700
void setEndMs(uint32_t ms)
Defines the (optional) end time in milliseconds.
Definition AudioIO.h:582
uint32_t current_bytes
Definition AudioIO.h:702
void setStream(AudioOutput &out)
Definition AudioIO.h:681
void setCompressionRatio(float ratio)
Definition AudioIO.h:662
uint32_t start_ms
Definition AudioIO.h:698
void setOutput(AudioOutput &out)
Definition AudioIO.h:676
size_t readBytes(uint8_t *data, size_t len) override
Definition AudioIO.h:616
void setStartSec(uint32_t startSeconds)
Definition AudioIO.h:563
TimedStream(Stream &io, long startSeconds=0, long endSeconds=-1)
Definition AudioIO.h:548
void setStream(AudioStream &stream)
Definition AudioIO.h:686
TimedStream(AudioStream &io, long startSeconds=0, long endSeconds=-1)
Definition AudioIO.h:533
void consumeBytes(uint32_t len)
Definition AudioIO.h:705
int available() override
Provides the available bytes until the end time has reached.
Definition AudioIO.h:644
size_t write(const uint8_t *data, size_t len) override
Plays only data for the indiated start and end time.
Definition AudioIO.h:636
Stream * p_stream
Definition AudioIO.h:695
void setStartMs(uint32_t ms)
Defines the start time in milliseconds.
Definition AudioIO.h:569
bool isPlaying()
Returns true if we are in a valid time range and are still playing sound.
Definition AudioIO.h:588
int availableForWrite() override
Definition AudioIO.h:656
AudioInfoSupport * p_info
Definition AudioIO.h:697
bool isActive()
Returns true if we are not past the end time;.
Definition AudioIO.h:595
void calculateByteLimits()
Definition AudioIO.h:717
bool begin() override
Definition AudioIO.h:604
Print * p_print
Definition AudioIO.h:696
uint32_t end_bytes
Definition AudioIO.h:701
TimedStream(AudioOutput &o, long startSeconds=0, long endSeconds=-1)
Definition AudioIO.h:541
void setAudioInfo(AudioInfo info) override
Updates the AudioInfo in the current object and in the source or target.
Definition AudioIO.h:650
float compression_ratio
Definition AudioIO.h:703
bool begin(AudioInfo info)
Definition AudioIO.h:599
int bytesPerSecond()
Calculates the bytes per second from the AudioInfo.
Definition AudioIO.h:665
ConverterStream Helper class which implements the readBytes with the help of write.
Definition AudioIO.h:22
bool active
Definition AudioIO.h:108
QueueStream< uint8_t > result_queue
Definition AudioIO.h:104
void setupBuffers(size_t len)
Definition AudioIO.h:118
void setMaxReadSize(int size)
Definition AudioIO.h:98
void begin(T *transform, Stream *source)
setup of the TransformationReader class
Definition AudioIO.h:28
void resizeReadBuffer(int size)
Defines the read buffer size for individual reads.
Definition AudioIO.h:116
int last_setup_buffer_size
Definition AudioIO.h:111
RingBuffer< uint8_t > result_queue_buffer
Definition AudioIO.h:103
T * p_transform
Definition AudioIO.h:107
int available()
Definition AudioIO.h:72
void restoreOutput(Print *out)
restores the original output in the converter class
Definition AudioIO.h:215
bool is_eof
Definition AudioIO.h:112
size_t total_bytes_read
Definition AudioIO.h:113
Stream * p_stream
Definition AudioIO.h:105
void fillResultQueue(size_t len)
Definition AudioIO.h:149
size_t readBytes(uint8_t *data, size_t len)
Definition AudioIO.h:46
Print * setupOutput()
Definition AudioIO.h:207
size_t getTotalBytesRead() const
Definition AudioIO.h:100
void end()
Definition AudioIO.h:82
int max_read_size
Definition AudioIO.h:110
void resizeResultQueue(int size)
Defines the queue size for result.
Definition AudioIO.h:93
void setResultQueueFactor(int factor)
Defines the queue size dependent on the read size.
Definition AudioIO.h:90
int result_queue_factor
Definition AudioIO.h:109
Vector< uint8_t > buffer
Definition AudioIO.h:106
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
void push_back(T &&value)
Definition Vector.h:182
bool resize(int newSize, T value)
Definition Vector.h:266
T * data()
Definition Vector.h:316
int size()
Definition Vector.h:178
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
void delay(unsigned long ms)
Definition Time.h:23
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:55
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:57
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:59
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:61
AudioInfoSupport * p_audio_info
Definition AudioIO.h:864
Vector< uint16_t > channels
Definition AudioIO.h:866
SingleBuffer< uint8_t > buffer
Definition AudioIO.h:865