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
111
112 protected:
115 Stream* p_stream = nullptr;
116 Vector<uint8_t> buffer{0}; // we allocate memory only when needed
117 T* p_transform = nullptr;
118 bool active = false;
122 float last_byte_factor = 0.0f;
123 bool is_eof = false;
124 bool eof_on_zero_reads = true;
126
128 void resizeReadBuffer(int size) { buffer.resize(size); }
129
130 void setupBuffers(size_t len) {
131 float byte_factor = p_transform->getByteFactor();
132 if (byte_factor <= 0.0f) {
133 LOGE("Invalid byte factor: %f", byte_factor);
134 byte_factor = 1.0f;
135 }
136 // Recompute if the requested length changed, or if the transform's byte
137 // factor drifted meaningfully since the read chunk size was last sized
138 // (e.g. a live-adjusted resampling step size). For a transform with a
139 // constant byte factor (the common decoder/encoder case) this check
140 // never re-triggers after the first call, so behavior there is
141 // unchanged; it only matters for transforms whose byte factor changes
142 // at runtime, where a stale chunk size would otherwise silently distort
143 // the consumption/production ratio.
145 fabsf(byte_factor - last_byte_factor) > 0.01f * last_byte_factor;
146 if (len == last_setup_buffer_size && !byte_factor_changed) return;
147 LOGD("setupBuffers: %d", (int)len);
148 last_byte_factor = byte_factor;
149
150 // we read half the necessary bytes
151 int size = (0.5f / byte_factor * len);
152 // process full samples/frames
153 size = size / 4 * 4;
154 if (size <= 0) size = 4;
155 if (buffer.size() < size) {
156 LOGI("read size: %d", size);
157 buffer.resize(size);
158 }
159
160 if (result_queue_buffer.size() == 0) {
161 // make sure that the ring buffer is big enough
162 int rb_size = len * result_queue_factor;
163 LOGI("buffer size: %d", rb_size);
166 }
168 }
169
172 void fillResultQueue(size_t len) {
173 if (is_eof) return;
174 if (result_queue.available() >= len) return;
175 LOGD("fillResultQueue: %d", (int)len);
176
177 // Detect misconfigured buffer: if the ring buffer capacity is smaller than
178 // the requested len bytes we can never satisfy the condition and will loop
179 // forever. Issue an error and bail out early.
180 if ((int)len > result_queue_buffer.size()) {
181 LOGE("fillResultQueue: result_queue_buffer too small: %d < %d. "
182 "Increase result_queue_factor or call resizeReadResultQueue().",
183 result_queue_buffer.size(), (int)len);
184 return;
185 }
186
187 Print* tmp = setupOutput();
188 int zero_count = 0;
189 while (result_queue.available() < len) {
190 // Detect buffer-full stall: if we can't write any more data but the
191 // queue is still below len, we must stop to avoid an endless loop.
193 LOGE("fillResultQueue: result_queue full (%d) but target not reached "
194 "(%d/%d). Increase result_queue_factor or call "
195 "resizeReadResultQueue().",
197 break;
198 }
199 int read_size = buffer.size();
200 int read_eff = p_stream->readBytes(buffer.data(), read_size);
201 LOGD("readBytes from source: %d -> %d", read_size,read_eff);
202 if (read_eff > 0) {
203 zero_count = 0; // reset 0 count
204 if (read_eff != buffer.size()) {
205 LOGD("readBytes %d -> %d", buffer.size(), read_eff);
206 }
207 int write_eff = p_transform->write(buffer.data(), read_eff);
208 if (write_eff != read_eff) {
209 LOGE("TransformationReader::write %d -> %d", read_eff, write_eff);
210 }
211 } else {
212 // limit the number of reads which provide 0;
214 if (eof_on_zero_reads) {
215 is_eof = true;
216 // Flush any buffered/final encoder bytes into result_queue.
217 p_transform->flush();
218 }
219 // Otherwise the source is just a momentarily empty live
220 // producer (buffer underflow, not end of stream): stop trying
221 // for this call, but leave is_eof false so the next call
222 // retries once more data has arrived.
223 break;
224 }
225 // wait for some more data
226 delay(5);
227 }
228 }
229 LOGD("fillResultQueue available: %d", result_queue.available());
230 restoreOutput(tmp);
231 }
232
237 Print* result = p_transform->getPrint();
238 p_transform->setOutput((Print&)result_queue);
239
240 return result;
241 }
244 void restoreOutput(Print* out) {
245 if (out) p_transform->setOutput(*out);
246 }
247};
248
256 public:
257 virtual void setStream(Stream& stream) override {
258 TRACED();
259 p_stream = &stream;
260 p_print = &stream;
261 }
262
263 virtual void setStream(AudioStream& stream) {
264 TRACED();
265 p_stream = &stream;
266 p_print = &stream;
267 // setNotifyOnOutput(stream);
268 addNotifyAudioChange(stream);
269 }
270
271 virtual void setOutput(AudioOutput& print) {
272 TRACED();
273 p_print = &print;
275 }
276
277 virtual void setOutput(Print& print) override {
278 TRACED();
279 p_print = &print;
280 }
281
282 virtual Print* getPrint() { return p_print; }
283
284 virtual Stream* getStream() { return p_stream; }
285
286 size_t readBytes(uint8_t* data, size_t len) override {
287 LOGD("ReformatBaseStream::readBytes: %d", (int)len);
288 return reader.readBytes(data, len);
289 }
290
291 int available() override {
292 return reader.available();
293 }
294
295 int availableForWrite() override {
296 return DEFAULT_BUFFER_SIZE; // reader.availableForWrite();
297 }
298
299 virtual float getByteFactor() = 0;
300
306 virtual void flush() {}
307
308 void end() override {
309 TRACED();
311 reader.end();
312 }
313
316 void resizeReadResultQueue(int size) { reader.resizeResultQueue(size); }
317
319 void setReadResultQueueSize(int size) { reader.resizeResultQueue(size); }
320
322 void setMaxReadSize(int size) { reader.setMaxReadSize(size); }
323
328
329
330 protected:
332 Stream* p_stream = nullptr;
333 Print* p_print = nullptr;
334
335 void setupReader() {
336 if (getStream() != nullptr) {
337 reader.begin(this, getStream());
338 }
339 }
340};
341
347
353 public:
356 void setStream(Print& out) { p_print = &out; }
357 void setAudioInfo(AudioInfo info) { cfg = info; }
358 size_t write(const uint8_t* data, size_t len) {
359 return p_print->write(data, len);
360 }
362 virtual bool isDeletable() { return true; }
363
364 AudioInfo audioInfo() { return cfg; }
365
366 protected:
367 Print* p_print = nullptr;
369};
370
376 public:
378
380
381 void setStream(AudioStream& stream) { p_stream = &stream; }
382
383 void setAudioInfo(AudioInfo info) override { p_stream->setAudioInfo(info); }
384
385 AudioInfo audioInfo() override { return p_stream->audioInfo(); }
386
387 size_t write(const uint8_t* data, size_t len) override {
388 return p_stream->write(data, len);
389 }
390
391 int availableForWrite() override { return p_stream->availableForWrite(); }
392
393 bool begin() override { return p_stream->begin(); }
394
395 void end() override { p_stream->end(); }
396
398 virtual bool isDeletable() override { return true; }
399
400 operator bool() override { return *p_stream; }
401
402 protected:
404};
405
411 public:
413
415
416 void setOutput(AudioOutput& stream) { p_stream = &stream; }
417
419
420 AudioInfo audioInfo() override { return p_stream->audioInfo(); }
421
422 size_t write(const uint8_t* data, size_t len) override {
423 return p_stream->write(data, len);
424 }
425
426 bool begin() override { return p_stream->begin(); }
427
428 void end() override { p_stream->end(); }
429
431 virtual bool isDeletable() { return true; }
432
433 operator bool() override { return *p_stream; }
434
435 protected:
437};
438
446 public:
448 MultiOutput() = default;
449
450 MultiOutput(Print& out) { add(out); }
451
454
456
462
468
472 add(out1);
473 add(out2);
474 }
475
476 virtual ~MultiOutput() { clear(); }
477
479 void add(AudioOutput& out) { vector.push_back(&out); }
480
482 void add(AudioStream& stream) {
485 vector.push_back(out);
486 }
487
488 void add(Print& print) {
490 vector.push_back(out);
491 }
492
493 void flush() {
494 for (int j = 0; j < vector.size(); j++) {
495 vector[j]->flush();
496 }
497 }
498
500 for (int j = 0; j < vector.size(); j++) {
501 vector[j]->setAudioInfo(info);
502 }
503 }
504
505 size_t write(const uint8_t* data, size_t len) override {
506 for (auto& out : vector) {
507 int open = len;
508 int start = 0;
509 // create copy of data to avoid that one output changes the data for the
510 // other outputs
511 uint8_t copy[len];
512 memcpy(copy, data, len);
513 while (open > 0) {
514 int written = out->write(copy + start, open);
515 open -= written;
516 start += written;
517 }
518 }
519 return len;
520 }
521
522 size_t write(uint8_t ch) override {
523 for (int j = 0; j < vector.size(); j++) {
524 int open = 1;
525 while (open > 0) {
526 open -= vector[j]->write(ch);
527 }
528 }
529 return 1;
530 }
531
533 void clear() {
534 for (auto& tmp : vector) {
535 if (tmp != nullptr && tmp->isDeletable()) {
536 delete tmp;
537 }
538 }
539 vector.clear();
540 }
541
542 protected:
544
546 void setOutput(Print& out) { add(out); }
547};
548
559 public:
560 TimedStream() = default;
561
562 TimedStream(AudioStream& io, long startSeconds = 0, long endSeconds = -1) {
563 p_stream = &io;
564 p_print = &io;
565 p_info = &io;
568 }
569
571 p_print = &o;
572 p_info = &o;
575 }
576
577 TimedStream(Stream& io, long startSeconds = 0, long endSeconds = -1) {
578 p_stream = &io;
579 p_print = &io;
582 }
583
584 TimedStream(Print& o, long startSeconds = 0, long endSeconds = -1) {
585 p_print = &o;
588 }
589
596
599 start_ms = ms;
601 }
602
609
612 end_ms = ms;
614 }
615
617 bool isPlaying() {
618 if (current_bytes < start_bytes) return false;
619 if (end_bytes > 0 && current_bytes > end_bytes) return false;
620 return true;
621 }
622
627
630 return begin();
631 }
632
633 bool begin() override {
635 current_bytes = 0;
636 LOGI("byte range %u - %u", (unsigned)start_bytes, (unsigned)end_bytes);
637 return true;
638 }
639
640 operator bool() override { return isActive(); }
641
645 size_t readBytes(uint8_t* data, size_t len) override {
646 // if reading is not supported we stop
647 if (p_stream == nullptr) return 0;
648 // Positioin to start
651 }
652 // if we are past the end we stop
653 if (!isActive()) return 0;
654 // read the data now
655 size_t result = 0;
656 do {
657 result = p_stream->readBytes(data, len);
658 current_bytes += len;
659 // ignore data before start time
660 } while (result > 0 && current_bytes < start_bytes);
661 return isPlaying() ? result : 0;
662 }
663
665 size_t write(const uint8_t* data, size_t len) override {
666 if (current_bytes >= end_bytes) return 0;
667 current_bytes += len;
668 if (current_bytes < start_bytes) return len;
669 return p_print->write(data, len);
670 }
671
673 int available() override {
674 if (p_stream == nullptr) return 0;
675 return current_bytes < end_bytes ? p_stream->available() : 0;
676 }
677
684
685 int availableForWrite() override {
687 }
688
691 void setCompressionRatio(float ratio) { compression_ratio = ratio; }
692
697
698 void setOutput(Print& out) override { p_print = &out; }
699
700 void setStream(Stream& stream) override {
701 p_print = &stream;
702 p_stream = &stream;
703 }
704
706 p_print = &out;
707 p_info = &out;
708 }
709
711 p_print = &out;
712 p_info = &out;
713 }
714
715 void setStream(AudioStream& stream) {
716 p_print = &stream;
717 p_stream = &stream;
718 p_info = &stream;
719 }
720
721 size_t size() { return end_bytes - start_bytes; }
722
723 protected:
724 Stream* p_stream = nullptr;
725 Print* p_print = nullptr;
732 float compression_ratio = 1.0;
733
735 int open = len;
736 uint8_t buffer[1024];
737 while (open > 0) {
738 int toread = min(1024, open);
739 p_stream->readBytes(buffer, toread);
740 open -= toread;
741 }
742 current_bytes += len;
743 LOGD("consumed %u -> %u", (unsigned)len, (unsigned)current_bytes);
744 }
745
747 float bytes_per_second = bytesPerSecond();
748 if (bytes_per_second > 0) {
749 start_bytes = bytes_per_second * start_ms / compression_ratio / 1000;
750 end_bytes = bytes_per_second * end_ms / compression_ratio / 1000;
751 } else {
752 LOGE("AudioInfo not defined");
753 }
754 }
755};
756
767 public:
769
770 bool begin(AudioInfo info) override {
771 setAudioInfo(info);
772 return begin();
773 }
774
775 bool begin() override {
777 // make sure that selected channels are valid
778 for (auto& out : out_channels) {
779 for (auto& ch : out.channels) {
780 if (ch > cfg.channels - 1) {
781 LOGE("Channel '%d' not valid for max %d channels", ch, cfg.channels);
782 return false;
783 }
784 }
785 }
786 return true;
787 }
788
791 void addOutput(AudioOutput& out, uint16_t channel) {
792 Vector<uint16_t> channels;
793 channels.push_back(channel);
795 def.channels = channels;
796 def.p_out = &out;
797 def.p_audio_info = &out;
798 out_channels.push_back(def);
799 }
800
803 void addOutput(AudioStream& out, uint16_t channel) {
804 Vector<uint16_t> channels;
805 channels.push_back(channel);
807 def.channels = channels;
808 def.p_out = &out;
809 def.p_audio_info = &out;
810 out_channels.push_back(def);
811 }
812
815 void addOutput(Print& out, uint16_t channel) {
816 Vector<uint16_t> channels;
817 channels.push_back(channel);
819 def.channels = channels;
820 def.p_out = &out;
821 out_channels.push_back(def);
822 }
823
826 void addOutput(Print& out, uint16_t left, uint16_t right) {
827 Vector<uint16_t> channels;
828 channels.push_back(left);
829 channels.push_back(right);
831 def.channels = channels;
832 def.p_out = &out;
833 out_channels.push_back(def);
834 }
835
838 void addOutput(AudioOutput& out, uint16_t left, uint16_t right) {
839 Vector<uint16_t> channels;
840 channels.push_back(left);
841 channels.push_back(right);
843 def.channels = channels;
844 def.p_out = &out;
845 def.p_audio_info = &out;
846 out_channels.push_back(def);
847 }
848
851 void addOutput(AudioStream& out, uint16_t left, uint16_t right) {
852 Vector<uint16_t> channels;
853 channels.push_back(left);
854 channels.push_back(right);
856 def.channels = channels;
857 def.p_out = &out;
858 def.p_audio_info = &out;
859 out_channels.push_back(def);
860 }
861
862 size_t write(const uint8_t* data, size_t len) override {
863 if (!is_active) return false;
864 LOGD("write %d", (int)len);
865 switch (cfg.bits_per_sample) {
866 case 16:
867 return writeT<int16_t>(data, len);
868 case 24:
869 return writeT<int24_t>(data, len);
870 case 32:
871 return writeT<int32_t>(data, len);
872 default:
873 return 0;
874 }
875 }
876
877 void setAudioInfo(AudioInfo ai) override {
878 this->cfg = ai;
879 // notifyAudioChange(ai);
880 for (auto& info : out_channels) {
881 auto p_notify = info.p_audio_info;
882 if (p_notify != nullptr) {
883 AudioInfo result{ai};
884 result.channels = info.channels.size();
885 p_notify->setAudioInfo(result);
886 }
887 }
888 }
889
890 protected:
898
899 template <typename T>
900 size_t writeT(const uint8_t* buffer, size_t size) {
901 if (!is_active) return 0;
902 int sample_count = size / sizeof(T);
903 // int result_size = sample_count / cfg.channels;
904 T* data = (T*)buffer;
905
906 for (int i = 0; i < sample_count; i += cfg.channels) {
907 T* frame = data + i;
908 for (auto& out : out_channels) {
909 T out_frame[out.channels.size()];
910 int ch_out = 0;
911 for (auto& ch : out.channels) {
912 // make sure we have a valid channel
913 int channel = (ch < cfg.channels) ? ch : cfg.channels - 1;
914 out_frame[ch_out++] = frame[channel];
915 }
916 // write to buffer
917 size_t written = out.buffer.writeArray((const uint8_t*)&out_frame,
918 sizeof(out_frame));
919 // write buffer to final output
920 if (out.buffer.availableForWrite() < sizeof(out_frame)) {
921 out.p_out->write(out.buffer.data(), out.buffer.available());
922 out.buffer.reset();
923 }
924 // if (written != sizeof(out_frame)) {
925 // LOGW("Could not write all samples %d -> %d", sizeof(out_frame),
926 // written);
927 // }
928 }
929 }
930 return size;
931 }
932
935 for (auto& channels_select : out_channels) {
936 if (channels_select.p_out == out) return channels_select.channels.size();
937 }
938 return defaultChannels;
939 }
940};
941
942} // 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
Definition Arduino.h:56
virtual int availableForWrite()
Definition Arduino.h:128
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
Definition Arduino.h:136
virtual size_t readBytes(uint8_t *data, size_t len)
Definition Arduino.h:140
virtual int available()
Definition Arduino.h:139
Wrapper which converts a AudioStream to a AudioOutput.
Definition AudioIO.h:410
void setOutput(AudioOutput &stream)
Definition AudioIO.h:416
virtual bool isDeletable()
If true we need to release the related memory in the destructor.
Definition AudioIO.h:431
AdapterAudioOutputToAudioStream(AudioOutput &stream)
Definition AudioIO.h:414
void end() override
Definition AudioIO.h:428
size_t write(const uint8_t *data, size_t len) override
Definition AudioIO.h:422
AudioOutput * p_stream
Definition AudioIO.h:436
bool begin() override
Definition AudioIO.h:426
void setAudioInfo(AudioInfo info) override
Defines the input AudioInfo.
Definition AudioIO.h:418
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition AudioIO.h:420
Wrapper which converts a AudioStream to a AudioOutput.
Definition AudioIO.h:375
void setStream(AudioStream &stream)
Definition AudioIO.h:381
virtual bool isDeletable() override
If true we need to release the related memory in the destructor.
Definition AudioIO.h:398
void end() override
Definition AudioIO.h:395
size_t write(const uint8_t *data, size_t len) override
Definition AudioIO.h:387
AdapterAudioStreamToAudioOutput(AudioStream &stream)
Definition AudioIO.h:379
int availableForWrite() override
Definition AudioIO.h:391
AudioStream * p_stream
Definition AudioIO.h:403
bool begin() override
Definition AudioIO.h:393
void setAudioInfo(AudioInfo info) override
Defines the input AudioInfo.
Definition AudioIO.h:383
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition AudioIO.h:385
Wrapper which converts a Print to a AudioOutput.
Definition AudioIO.h:352
AdapterPrintToAudioOutput(Print &print)
Definition AudioIO.h:355
AudioInfo cfg
Definition AudioIO.h:368
virtual bool isDeletable()
If true we need to release the related memory in the destructor.
Definition AudioIO.h:362
void setAudioInfo(AudioInfo info)
Defines the input AudioInfo.
Definition AudioIO.h:357
void setStream(Print &out)
Definition AudioIO.h:356
AudioInfo audioInfo()
provides the actual input AudioInfo
Definition AudioIO.h:364
Print * p_print
Definition AudioIO.h:367
size_t write(const uint8_t *data, size_t len)
Definition AudioIO.h:358
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition AudioTypes.h:149
Supports changes to the sampling rate, bits and channels.
Definition AudioTypes.h:131
virtual void setAudioInfo(AudioInfo info)=0
Defines the input AudioInfo.
Base class for Output Adpapters.
Definition AudioIO.h:346
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:120
virtual size_t write(const uint8_t *data, size_t len) override
Definition BaseStream.h:146
AudioInfo info
Definition BaseStream.h:171
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition BaseStream.h:128
virtual AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition BaseStream.h:151
void clear()
same as reset
Definition Buffers.h:96
virtual bool begin()
Definition BaseStream.h:40
virtual int availableForWrite() override
Definition BaseStream.h:57
virtual void end()
Definition BaseStream.h:41
Flexible functionality to extract one or more channels from a multichannel signal....
Definition AudioIO.h:766
void addOutput(Print &out, uint16_t left, uint16_t right)
Definition AudioIO.h:826
void setAudioInfo(AudioInfo ai) override
Defines the input AudioInfo.
Definition AudioIO.h:877
void addOutput(Print &out, uint16_t channel)
Definition AudioIO.h:815
Vector< ChannelSelectionOutputDef > out_channels
Definition AudioIO.h:897
void addOutput(AudioOutput &out, uint16_t channel)
Definition AudioIO.h:791
int getChannels(Print *out, int defaultChannels)
Determine number of channels for destination.
Definition AudioIO.h:934
size_t write(const uint8_t *data, size_t len) override
Definition AudioIO.h:862
size_t writeT(const uint8_t *buffer, size_t size)
Definition AudioIO.h:900
bool begin(AudioInfo info) override
Definition AudioIO.h:770
bool begin() override
Definition AudioIO.h:775
void addOutput(AudioStream &out, uint16_t channel)
Definition AudioIO.h:803
void addOutput(AudioStream &out, uint16_t left, uint16_t right)
Definition AudioIO.h:851
void addOutput(AudioOutput &out, uint16_t left, uint16_t right)
Definition AudioIO.h:838
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:445
void add(AudioOutput &out)
Add an additional AudioOutput output.
Definition AudioIO.h:479
MultiOutput(Print &out)
Definition AudioIO.h:450
Vector< AudioOutput * > vector
Definition AudioIO.h:543
size_t write(uint8_t ch) override
Definition AudioIO.h:522
void setAudioInfo(AudioInfo info)
Defines the input AudioInfo.
Definition AudioIO.h:499
void add(AudioStream &stream)
Add an AudioStream to the output.
Definition AudioIO.h:482
MultiOutput(AudioStream &out1, AudioStream &out2)
Defines a MultiOutput with 2 final outputs.
Definition AudioIO.h:464
size_t write(const uint8_t *data, size_t len) override
Definition AudioIO.h:505
void add(Print &print)
Definition AudioIO.h:488
MultiOutput(AudioOutput &out1, AudioOutput &out2)
Defines a MultiOutput with 2 final outputs.
Definition AudioIO.h:458
MultiOutput(AudioOutput &out)
Defines a MultiOutput with a single final outputs,.
Definition AudioIO.h:453
MultiOutput(AudioStream &out)
Definition AudioIO.h:455
virtual ~MultiOutput()
Definition AudioIO.h:476
void clear()
Removes all output components.
Definition AudioIO.h:533
void flush()
Definition AudioIO.h:493
void setOutput(Print &out)
support for Pipleline
Definition AudioIO.h:546
MultiOutput(Print &out1, Print &out2)
Definition AudioIO.h:471
MultiOutput()=default
Defines a MultiOutput with no final output: Define your outputs with add()
Stream class which stores the data in a temporary queue buffer. The queue can be consumed e....
Definition BaseStream.h:359
virtual bool begin() override
Activates the output.
Definition BaseStream.h:387
virtual size_t readBytes(uint8_t *data, size_t len) override
Definition BaseStream.h:445
int available() override
Definition BaseStream.h:411
Base class for chained converting streams.
Definition AudioIO.h:255
void setMaxReadSize(int size)
Defines the read buffer size for individual reads: same as transformationReader()....
Definition AudioIO.h:322
virtual void setStream(AudioStream &stream)
Defines/Changes the input & output and registers for audio change notifications.
Definition AudioIO.h:263
virtual void setStream(Stream &stream) override
Defines/Changes the input & output.
Definition AudioIO.h:257
TransformationReader< ReformatBaseStream > reader
Definition AudioIO.h:331
virtual float getByteFactor()=0
virtual void setOutput(Print &print) override
Defines/Changes the output target.
Definition AudioIO.h:277
size_t readBytes(uint8_t *data, size_t len) override
Definition AudioIO.h:286
void end() override
Definition AudioIO.h:308
int available() override
Definition AudioIO.h:291
virtual Stream * getStream()
Definition AudioIO.h:284
virtual void setOutput(AudioOutput &print)
Defines/Changes the output target and registers for audio change notifications.
Definition AudioIO.h:271
Stream * p_stream
Definition AudioIO.h:332
virtual TransformationReader< ReformatBaseStream > & transformationReader()
Provides access to the TransformationReader.
Definition AudioIO.h:325
int availableForWrite() override
Definition AudioIO.h:295
void setReadResultQueueSize(int size)
same as resizeReadResultQueue(size)
Definition AudioIO.h:319
virtual Print * getPrint()
Definition AudioIO.h:282
Print * p_print
Definition AudioIO.h:333
virtual void flush()
Definition AudioIO.h:306
void setupReader()
Definition AudioIO.h:335
void resizeReadResultQueue(int size)
Definition AudioIO.h:316
Implements a typed Ringbuffer.
Definition Buffers.h:353
virtual size_t size() override
Returns the maximum capacity of the buffer.
Definition Buffers.h:440
virtual bool resize(size_t len)
Resizes the buffer if supported: returns false if not supported.
Definition Buffers.h:430
A simple Buffer implementation which just uses a (dynamically sized) array.
Definition Buffers.h:184
AudioStream class that can define a start and (an optional) stop time Usually it is used to wrap an A...
Definition AudioIO.h:558
TimedStream(Print &o, long startSeconds=0, long endSeconds=-1)
Definition AudioIO.h:584
void setOutput(Print &out) override
Defines/Changes the output target.
Definition AudioIO.h:698
size_t size()
Definition AudioIO.h:721
void setStream(Stream &stream) override
Defines/Changes the input & output.
Definition AudioIO.h:700
uint32_t end_ms
Definition AudioIO.h:728
void setEndSec(uint32_t endSeconds)
Definition AudioIO.h:605
uint32_t start_bytes
Definition AudioIO.h:729
void setEndMs(uint32_t ms)
Defines the (optional) end time in milliseconds.
Definition AudioIO.h:611
uint32_t current_bytes
Definition AudioIO.h:731
void setStream(AudioOutput &out)
Definition AudioIO.h:710
void setCompressionRatio(float ratio)
Definition AudioIO.h:691
uint32_t start_ms
Definition AudioIO.h:727
void setOutput(AudioOutput &out)
Defines/Changes the output target and registers for audio change notifications.
Definition AudioIO.h:705
size_t readBytes(uint8_t *data, size_t len) override
Definition AudioIO.h:645
void setStartSec(uint32_t startSeconds)
Definition AudioIO.h:592
TimedStream(Stream &io, long startSeconds=0, long endSeconds=-1)
Definition AudioIO.h:577
void setStream(AudioStream &stream)
Defines/Changes the input & output and registers for audio change notifications.
Definition AudioIO.h:715
TimedStream(AudioStream &io, long startSeconds=0, long endSeconds=-1)
Definition AudioIO.h:562
void consumeBytes(uint32_t len)
Definition AudioIO.h:734
int available() override
Provides the available bytes until the end time has reached.
Definition AudioIO.h:673
size_t write(const uint8_t *data, size_t len) override
Plays only data for the indiated start and end time.
Definition AudioIO.h:665
Stream * p_stream
Definition AudioIO.h:724
void setStartMs(uint32_t ms)
Defines the start time in milliseconds.
Definition AudioIO.h:598
bool isPlaying()
Returns true if we are in a valid time range and are still playing sound.
Definition AudioIO.h:617
int availableForWrite() override
Definition AudioIO.h:685
AudioInfoSupport * p_info
Definition AudioIO.h:726
bool isActive()
Returns true if we are not past the end time;.
Definition AudioIO.h:624
void calculateByteLimits()
Definition AudioIO.h:746
bool begin() override
Definition AudioIO.h:633
Print * p_print
Definition AudioIO.h:725
uint32_t end_bytes
Definition AudioIO.h:730
TimedStream(AudioOutput &o, long startSeconds=0, long endSeconds=-1)
Definition AudioIO.h:570
void setAudioInfo(AudioInfo info) override
Updates the AudioInfo in the current object and in the source or target.
Definition AudioIO.h:679
float compression_ratio
Definition AudioIO.h:732
bool begin(AudioInfo info)
Definition AudioIO.h:628
int bytesPerSecond()
Calculates the bytes per second from the AudioInfo.
Definition AudioIO.h:694
ConverterStream Helper class which implements the readBytes with the help of write.
Definition AudioIO.h:22
bool active
Definition AudioIO.h:118
QueueStream< uint8_t > result_queue
Definition AudioIO.h:114
void setupBuffers(size_t len)
Definition AudioIO.h:130
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:128
int last_setup_buffer_size
Definition AudioIO.h:121
float last_byte_factor
Definition AudioIO.h:122
void setEofOnZeroReads(bool flag)
Definition AudioIO.h:110
RingBuffer< uint8_t > result_queue_buffer
Definition AudioIO.h:113
T * p_transform
Definition AudioIO.h:117
int available()
Definition AudioIO.h:72
void restoreOutput(Print *out)
restores the original output in the converter class
Definition AudioIO.h:244
bool is_eof
Definition AudioIO.h:123
size_t total_bytes_read
Definition AudioIO.h:125
Stream * p_stream
Definition AudioIO.h:115
void fillResultQueue(size_t len)
Definition AudioIO.h:172
size_t readBytes(uint8_t *data, size_t len)
Definition AudioIO.h:46
Print * setupOutput()
Definition AudioIO.h:236
size_t getTotalBytesRead() const
Definition AudioIO.h:100
void end()
Definition AudioIO.h:82
bool eof_on_zero_reads
Definition AudioIO.h:124
int max_read_size
Definition AudioIO.h:120
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:119
Vector< uint8_t > buffer
Definition AudioIO.h:116
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(size_t 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(uint32_t ms)
Definition Arduino.h:255
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:51
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57
AudioInfoSupport * p_audio_info
Definition AudioIO.h:893
Vector< uint16_t > channels
Definition AudioIO.h:895
SingleBuffer< uint8_t > buffer
Definition AudioIO.h:894