2 #include "AudioTools/AudioOutput.h"
3 #include "AudioTools/AudioStreams.h"
24 p_transform = transform;
25 if (transform ==
nullptr) {
26 LOGE(
"transform is NULL");
29 if (p_stream ==
nullptr) {
30 LOGE(
"p_stream is NULL");
35 size_t readBytes(uint8_t *data,
size_t byteCount) {
36 LOGD(
"TransformationReader::readBytes: %d", (
int)byteCount);
41 if (p_stream ==
nullptr) {
42 LOGE(
"p_stream is NULL");
47 if (buffer.size() == 0) {
48 int size = (0.5 / p_transform->getByteFactor() * byteCount);
51 LOGI(
"read size: %d", size);
57 int rb_size = byteCount * byte_count_factor;
58 LOGI(
"buffer size: %d", rb_size);
63 if (result_queue.available() < byteCount) {
65 while (result_queue.available() < byteCount) {
66 int read_eff = p_stream->readBytes(buffer.data(), buffer.size());
68 if(read_eff != buffer.size()) LOGD(
"readBytes %d -> %d", buffer.size(), read_eff);
69 int write_eff = p_transform->write(buffer.data(), read_eff);
70 if (write_eff != read_eff) LOGE(
"write %d -> %d", read_eff, write_eff);
78 int result_len = min((
int)byteCount, result_queue.available());
79 result_len = result_queue.readBytes(data, result_len);
80 LOGD(
"TransformationReader::readBytes: %d -> %d", (
int)byteCount,
91 void setByteCountFactor(
int f){
92 byte_count_factor = f;
97 RingBuffer<uint8_t> rb{0};
98 QueueStream<uint8_t> result_queue{rb};
99 Stream *p_stream =
nullptr;
100 Vector<uint8_t> buffer{0};
101 T *p_transform =
nullptr;
103 int byte_count_factor = 3;
109 Print *result = p_transform->getPrint();
110 p_transform->setOutput((
Print&)result_queue);
117 if (out) p_transform->setOutput(*out);
142 virtual void setOutput(AudioOutput &print) {
153 virtual Print *getPrint() {
return p_print; }
155 virtual Stream *getStream() {
return p_stream; }
157 size_t readBytes(uint8_t *data,
size_t size)
override {
158 LOGD(
"ReformatBaseStream::readBytes: %d", (
int)size);
159 return reader.readBytes(data, size);
162 int available()
override {
163 return DEFAULT_BUFFER_SIZE;
166 int availableForWrite()
override {
167 return DEFAULT_BUFFER_SIZE;
170 virtual float getByteFactor() = 0;
172 void end()
override {
179 TransformationReader<ReformatBaseStream> reader;
180 Stream *p_stream =
nullptr;
181 Print *p_print =
nullptr;
184 if(getStream() !=
nullptr) {
185 reader.begin(
this, getStream());
204 size_t write(
const uint8_t *buffer,
size_t size) {
205 return p_print->write(buffer, size);
211 Print *p_print =
nullptr;
224 void setStream(
AudioStream &stream) { p_stream = &stream; }
228 size_t write(
const uint8_t *buffer,
size_t size)
override {
229 return p_stream->write(buffer, size);
232 int availableForWrite()
override {
233 return p_stream->availableForWrite();
236 bool begin()
override {
237 return p_stream->begin();
240 void end()
override {
261 void setOutput(
AudioOutput &stream) { p_stream = &stream; }
264 size_t write(
const uint8_t *buffer,
size_t size) {
265 return p_stream->write(buffer, size);
313 for (
int j = 0; j < vector.size(); j++) {
327 vector.push_back(out);
332 vector.push_back(out);
336 for (
int j = 0; j < vector.size(); j++) {
342 for (
int j = 0; j < vector.size(); j++) {
343 vector[j]->setAudioInfo(info);
347 size_t write(
const uint8_t *buffer,
size_t size) {
348 for (
int j = 0; j < vector.size(); j++) {
352 int written = vector[j]->write(buffer + start, open);
360 size_t write(uint8_t ch) {
361 for (
int j = 0; j < vector.size(); j++) {
364 open -= vector[j]->write(ch);
371 Vector<AudioOutput *> vector;
409 void setStartSec(uint32_t startSeconds) { start_ms = startSeconds*1000; calculateByteLimits(); }
412 void setStartMs(uint32_t ms) { start_ms = ms; calculateByteLimits(); }
416 void setEndSec(uint32_t endSeconds) { end_ms = endSeconds*1000; calculateByteLimits(); }
419 void setEndMs(uint32_t ms) { end_ms = ms; calculateByteLimits(); }
423 if (current_bytes < start_bytes)
425 if (end_bytes > 0 && current_bytes > end_bytes)
432 return (current_bytes < end_bytes && current_bytes > start_bytes);
440 bool begin()
override {
441 calculateByteLimits();
446 operator bool() {
return isActive(); }
451 size_t readBytes(uint8_t *buffer,
size_t length)
override {
453 if (p_stream ==
nullptr)
461 result = p_stream->readBytes(buffer, length);
462 current_bytes += length;
464 }
while (result > 0 && current_bytes < start_bytes);
469 size_t write(
const uint8_t *buffer,
size_t length)
override {
470 current_bytes += length;
471 return isPlaying() ? p_print->write(buffer, length) : length;
476 if (p_stream ==
nullptr)
478 return isActive() ? p_stream->available() : 0;
485 calculateByteLimits();
488 int availableForWrite()
override {
return p_print->availableForWrite(); }
525 Stream *p_stream =
nullptr;
526 Print *p_print =
nullptr;
527 AudioInfoSupport *p_info =
nullptr;
528 uint32_t start_ms = 0;
529 uint32_t end_ms = UINT32_MAX;
530 uint32_t start_bytes = 0;
531 uint32_t end_bytes = UINT32_MAX;
532 uint32_t current_bytes = 0;
533 float compression_ratio = 1.0;
535 void calculateByteLimits() {
537 if (bytes_per_second > 0) {
538 start_bytes = bytes_per_second * start_ms / compression_ratio / 1000;
539 end_bytes = bytes_per_second * end_ms / compression_ratio / 1000;
541 LOGE(
"AudioInfo not defined");
565 AudioOutput::begin();
567 for (
auto &out : out_channels) {
568 for (
auto &ch : out.channels) {
570 LOGE(
"Channel '%d' not valid for max %d channels", ch, cfg.
channels);
582 channels.push_back(channel);
584 def.channels = channels;
586 def.p_audio_info = &out;
587 out_channels.push_back(def);
594 channels.push_back(channel);
596 def.channels = channels;
598 def.p_audio_info = &out;
599 out_channels.push_back(def);
606 channels.push_back(channel);
608 def.channels = channels;
610 out_channels.push_back(def);
617 channels.push_back(left);
618 channels.push_back(right);
620 def.channels = channels;
622 out_channels.push_back(def);
629 channels.push_back(left);
630 channels.push_back(right);
632 def.channels = channels;
634 def.p_audio_info = &out;
635 out_channels.push_back(def);
642 channels.push_back(left);
643 channels.push_back(right);
645 def.channels = channels;
647 def.p_audio_info = &out;
648 out_channels.push_back(def);
652 size_t write(
const uint8_t *buffer,
size_t size)
override {
655 return writeT<int16_t>(buffer, size);
657 return writeT<int24_t>(buffer, size);
659 return writeT<int32_t>(buffer, size);
666 notifyAudioChange(ai);
667 for (
auto &info : out_channels){
668 auto p_notify = info.p_audio_info;
669 if (p_notify !=
nullptr){
672 p_notify->setAudioInfo(result);
680 Print *p_out =
nullptr;
686 template <
typename T>
687 size_t writeT(
const uint8_t *buffer,
size_t size) {
690 int sample_count = size /
sizeof(T);
691 int result_size = sample_count / cfg.
channels;
692 T *data = (T *)buffer;
694 for (
int i = 0; i < sample_count; i += cfg.
channels) {
696 for (
auto &out : out_channels) {
697 T out_frame[out.channels.size()];
699 for (
auto &ch : out.channels) {
702 out_frame[ch_out++] = frame[channel];
705 size_t written = out.p_out->write((uint8_t *)&out_frame,
sizeof(out_frame));
706 if (written !=
sizeof(out_frame)) {
707 LOGW(
"Could not write all samples");
716 for (
auto& channels_select : out_channels){
717 if (channels_select.p_out == out)
return channels_select.channels.size();
719 return defaultChannels;