2 #include "AudioTools/CoreAudio/AudioOutput.h"
3 #include "AudioTools/CoreAudio/AudioStreams.h"
5 #ifndef MAX_ZERO_READ_COUNT
6 # define MAX_ZERO_READ_COUNT 3
9 #ifndef CHANNEL_SELECT_BUFFER_SIZE
10 # define CHANNEL_SELECT_BUFFER_SIZE 256
33 p_transform = transform;
34 if (transform ==
nullptr) {
35 LOGE(
"transform is NULL");
38 if (p_stream ==
nullptr) {
39 LOGE(
"p_stream is NULL");
44 size_t readBytes(uint8_t *data,
size_t len) {
45 LOGD(
"TransformationReader::readBytes: %d", (
int)len);
50 if (p_stream ==
nullptr) {
51 LOGE(
"p_stream is NULL");
56 if (buffer.size() == 0) {
57 int size = (0.5 / p_transform->getByteFactor() * len);
60 LOGI(
"read size: %d", size);
66 int rb_size = len * byte_count_factor;
67 LOGI(
"buffer size: %d", rb_size);
72 if (result_queue.available() < len) {
75 while (result_queue.available() < len) {
76 int read_eff = p_stream->readBytes(buffer.data(), buffer.size());
79 if (read_eff != buffer.size())
80 LOGD(
"readBytes %d -> %d", buffer.size(), read_eff);
81 int write_eff = p_transform->write(buffer.data(), read_eff);
82 if (write_eff != read_eff)
83 LOGE(
"write %d -> %d", read_eff, write_eff);
86 if (++zero_count > MAX_ZERO_READ_COUNT){
96 int result_len = min((
int)len, result_queue.available());
97 result_len = result_queue.readBytes(data, result_len);
98 LOGD(
"TransformationReader::readBytes: %d -> %d", (
int)len,
109 void setByteCountFactor(
int f) { byte_count_factor = f; }
112 RingBuffer<uint8_t> rb{0};
113 QueueStream<uint8_t> result_queue{rb};
114 Stream *p_stream =
nullptr;
115 Vector<uint8_t> buffer{0};
116 T *p_transform =
nullptr;
118 int byte_count_factor = 3;
124 Print *result = p_transform->getPrint();
125 p_transform->setOutput((
Print &)result_queue);
132 if (out) p_transform->setOutput(*out);
158 virtual void setOutput(AudioOutput &print) {
169 virtual Print *getPrint() {
return p_print; }
171 virtual Stream *getStream() {
return p_stream; }
173 size_t readBytes(uint8_t *data,
size_t len)
override {
174 LOGD(
"ReformatBaseStream::readBytes: %d", (
int)len);
175 return reader.readBytes(data, len);
178 int available()
override {
179 return DEFAULT_BUFFER_SIZE;
182 int availableForWrite()
override {
183 return DEFAULT_BUFFER_SIZE;
186 virtual float getByteFactor() = 0;
188 void end()
override {
195 TransformationReader<ReformatBaseStream> reader;
196 Stream *p_stream =
nullptr;
197 Print *p_print =
nullptr;
214 if (getStream() !=
nullptr) {
215 reader.begin(
this, getStream());
234 size_t write(
const uint8_t *data,
size_t len) {
235 return p_print->write(data, len);
241 Print *p_print =
nullptr;
254 void setStream(
AudioStream &stream) { p_stream = &stream; }
260 size_t write(
const uint8_t *data,
size_t len)
override {
261 return p_stream->write(data, len);
264 int availableForWrite()
override {
return p_stream->availableForWrite(); }
266 bool begin()
override {
return p_stream->begin(); }
268 void end()
override { p_stream->end(); }
273 operator bool()
override {
return *p_stream; }
276 AudioStream *p_stream =
nullptr;
289 void setOutput(
AudioOutput &stream) { p_stream = &stream; }
295 size_t write(
const uint8_t *data,
size_t len)
override {
296 return p_stream->write(data, len);
299 bool begin()
override {
return p_stream->begin(); }
301 void end()
override { p_stream->end(); }
306 operator bool()
override {
return *p_stream; }
309 AudioOutput *p_stream =
nullptr;
350 for (
int j = 0; j < vector.size(); j++) {
364 vector.push_back(out);
369 vector.push_back(out);
373 for (
int j = 0; j < vector.size(); j++) {
379 for (
int j = 0; j < vector.size(); j++) {
380 vector[j]->setAudioInfo(info);
384 size_t write(
const uint8_t *data,
size_t len) {
385 for (
int j = 0; j < vector.size(); j++) {
389 int written = vector[j]->write(data + start, open);
397 size_t write(uint8_t ch) {
398 for (
int j = 0; j < vector.size(); j++) {
401 open -= vector[j]->write(ch);
408 Vector<AudioOutput *> vector;
444 start_ms = startSeconds * 1000;
445 calculateByteLimits();
451 calculateByteLimits();
457 end_ms = endSeconds * 1000;
458 calculateByteLimits();
464 calculateByteLimits();
469 if (current_bytes < start_bytes)
return false;
470 if (end_bytes > 0 && current_bytes > end_bytes)
return false;
476 return (current_bytes < end_bytes && current_bytes >= start_bytes);
484 bool begin()
override {
485 calculateByteLimits();
487 LOGI(
"byte range %u - %u",(
unsigned) start_bytes,(
unsigned) end_bytes);
491 operator bool() {
return isActive(); }
498 if (p_stream ==
nullptr)
return 0;
500 if (start_bytes > current_bytes){
501 consumeBytes(start_bytes - current_bytes);
508 result = p_stream->readBytes(data, len);
509 current_bytes += len;
511 }
while (result > 0 && current_bytes < start_bytes);
516 size_t write(
const uint8_t *data,
size_t len)
override {
517 if (current_bytes >= end_bytes)
return 0;
518 current_bytes += len;
519 if (current_bytes < start_bytes)
return len;
520 return p_print->write(data, len);
525 if (p_stream ==
nullptr)
return 0;
526 return current_bytes < end_bytes ? p_stream->available() : 0;
533 calculateByteLimits();
536 int availableForWrite()
override {
537 return current_bytes < end_bytes ? p_print->availableForWrite() : 0;
573 return end_bytes - start_bytes;
577 Stream *p_stream =
nullptr;
578 Print *p_print =
nullptr;
579 AudioInfoSupport *p_info =
nullptr;
580 uint32_t start_ms = 0;
581 uint32_t end_ms = UINT32_MAX;
582 uint32_t start_bytes = 0;
583 uint32_t end_bytes = UINT32_MAX;
584 uint32_t current_bytes = 0;
585 float compression_ratio = 1.0;
587 void consumeBytes(uint32_t len){
589 uint8_t buffer[1024];
591 int toread = min(1024, open);
592 p_stream->readBytes(buffer, toread);
595 current_bytes += len;
596 LOGD(
"consumed %u -> %u",(
unsigned) len, (
unsigned)current_bytes);
599 void calculateByteLimits() {
601 if (bytes_per_second > 0) {
602 start_bytes = bytes_per_second * start_ms / compression_ratio / 1000;
603 end_bytes = bytes_per_second * end_ms / compression_ratio / 1000;
605 LOGE(
"AudioInfo not defined");
629 AudioOutput::begin();
631 for (
auto &out : out_channels) {
632 for (
auto &ch : out.channels) {
634 LOGE(
"Channel '%d' not valid for max %d channels", ch, cfg.
channels);
646 channels.push_back(channel);
648 def.channels = channels;
650 def.p_audio_info = &out;
651 out_channels.push_back(def);
658 channels.push_back(channel);
660 def.channels = channels;
662 def.p_audio_info = &out;
663 out_channels.push_back(def);
670 channels.push_back(channel);
672 def.channels = channels;
674 out_channels.push_back(def);
681 channels.push_back(left);
682 channels.push_back(right);
684 def.channels = channels;
686 out_channels.push_back(def);
693 channels.push_back(left);
694 channels.push_back(right);
696 def.channels = channels;
698 def.p_audio_info = &out;
699 out_channels.push_back(def);
706 channels.push_back(left);
707 channels.push_back(right);
709 def.channels = channels;
711 def.p_audio_info = &out;
712 out_channels.push_back(def);
715 size_t write(
const uint8_t *data,
size_t len)
override {
716 if (!is_active)
return false;
717 LOGD(
"write %d", (
int)len);
720 return writeT<int16_t>(data, len);
722 return writeT<int24_t>(data, len);
724 return writeT<int32_t>(data, len);
733 for (
auto &info : out_channels) {
734 auto p_notify = info.p_audio_info;
735 if (p_notify !=
nullptr) {
738 p_notify->setAudioInfo(result);
745 Print *p_out =
nullptr;
752 template <
typename T>
753 size_t writeT(
const uint8_t *buffer,
size_t size) {
754 if (!is_active)
return 0;
755 int sample_count = size /
sizeof(T);
757 T *data = (T *)buffer;
759 for (
int i = 0; i < sample_count; i += cfg.
channels) {
761 for (
auto &out : out_channels) {
762 T out_frame[out.channels.size()] = {0};
764 for (
auto &ch : out.channels) {
767 out_frame[ch_out++] = frame[channel];
770 size_t written = out.buffer.writeArray((
const uint8_t *)&out_frame,
sizeof(out_frame));
772 if (out.buffer.availableForWrite()<
sizeof(out_frame)){
773 out.p_out->write(out.buffer.data(), out.buffer.available());
786 for (
auto &channels_select : out_channels) {
787 if (channels_select.p_out == out)
return channels_select.channels.size();
789 return defaultChannels;