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");
50 result_queue_buffer.resize(size);
55 size_t readBytes(uint8_t *data,
size_t len) {
56 LOGD(
"TransformationReader::readBytes: %d", (
int)len);
61 if (p_stream ==
nullptr) {
62 LOGE(
"p_stream is NULL");
67 if (buffer.size() == 0) {
68 int size = (0.5f / p_transform->getByteFactor() * len);
71 LOGI(
"read size: %d", size);
75 if (result_queue_buffer.
size() == 0) {
77 int rb_size = len * result_queue_factor;
78 LOGI(
"buffer size: %d", rb_size);
79 result_queue_buffer.resize(rb_size);
83 if (result_queue.available() < len) {
86 while (result_queue.available() < len) {
87 int read_eff = p_stream->readBytes(buffer.data(), buffer.size());
90 if (read_eff != buffer.size())
91 LOGD(
"readBytes %d -> %d", buffer.size(), read_eff);
92 int write_eff = p_transform->write(buffer.data(), read_eff);
93 if (write_eff != read_eff)
94 LOGE(
"write %d -> %d", read_eff, write_eff);
97 if (++zero_count > MAX_ZERO_READ_COUNT){
107 int result_len = min((
int)len, result_queue.available());
108 result_len = result_queue.readBytes(data, result_len);
109 LOGD(
"TransformationReader::readBytes: %d -> %d", (
int)len,
116 result_queue_buffer.resize(0);
125 QueueStream<uint8_t> result_queue{result_queue_buffer};
126 Stream *p_stream =
nullptr;
127 Vector<uint8_t> buffer{0};
128 T *p_transform =
nullptr;
130 int result_queue_factor = 5;
136 Print *result = p_transform->getPrint();
137 p_transform->setOutput((
Print &)result_queue);
144 if (out) p_transform->setOutput(*out);
170 virtual void setOutput(AudioOutput &print) {
181 virtual Print *getPrint() {
return p_print; }
183 virtual Stream *getStream() {
return p_stream; }
185 size_t readBytes(uint8_t *data,
size_t len)
override {
186 LOGD(
"ReformatBaseStream::readBytes: %d", (
int)len);
187 return reader.readBytes(data, len);
190 int available()
override {
191 return DEFAULT_BUFFER_SIZE;
194 int availableForWrite()
override {
195 return DEFAULT_BUFFER_SIZE;
198 virtual float getByteFactor() = 0;
200 void end()
override {
211 Stream *p_stream =
nullptr;
212 Print *p_print =
nullptr;
215 if (getStream() !=
nullptr) {
216 reader.
begin(
this, getStream());
235 size_t write(
const uint8_t *data,
size_t len) {
236 return p_print->write(data, len);
242 Print *p_print =
nullptr;
255 void setStream(
AudioStream &stream) { p_stream = &stream; }
261 size_t write(
const uint8_t *data,
size_t len)
override {
262 return p_stream->write(data, len);
265 int availableForWrite()
override {
return p_stream->availableForWrite(); }
267 bool begin()
override {
return p_stream->begin(); }
269 void end()
override { p_stream->end(); }
274 operator bool()
override {
return *p_stream; }
277 AudioStream *p_stream =
nullptr;
290 void setOutput(
AudioOutput &stream) { p_stream = &stream; }
296 size_t write(
const uint8_t *data,
size_t len)
override {
297 return p_stream->write(data, len);
300 bool begin()
override {
return p_stream->begin(); }
302 void end()
override { p_stream->end(); }
307 operator bool()
override {
return *p_stream; }
310 AudioOutput *p_stream =
nullptr;
351 for (
int j = 0; j < vector.size(); j++) {
365 vector.push_back(out);
370 vector.push_back(out);
374 for (
int j = 0; j < vector.size(); j++) {
380 for (
int j = 0; j < vector.size(); j++) {
381 vector[j]->setAudioInfo(info);
385 size_t write(
const uint8_t *data,
size_t len) {
386 for (
int j = 0; j < vector.size(); j++) {
390 int written = vector[j]->write(data + start, open);
398 size_t write(uint8_t ch) {
399 for (
int j = 0; j < vector.size(); j++) {
402 open -= vector[j]->write(ch);
409 Vector<AudioOutput *> vector;
445 start_ms = startSeconds * 1000;
446 calculateByteLimits();
452 calculateByteLimits();
458 end_ms = endSeconds * 1000;
459 calculateByteLimits();
465 calculateByteLimits();
470 if (current_bytes < start_bytes)
return false;
471 if (end_bytes > 0 && current_bytes > end_bytes)
return false;
477 return (current_bytes < end_bytes && current_bytes >= start_bytes);
485 bool begin()
override {
486 calculateByteLimits();
488 LOGI(
"byte range %u - %u",(
unsigned) start_bytes,(
unsigned) end_bytes);
492 operator bool() {
return isActive(); }
499 if (p_stream ==
nullptr)
return 0;
501 if (start_bytes > current_bytes){
502 consumeBytes(start_bytes - current_bytes);
509 result = p_stream->readBytes(data, len);
510 current_bytes += len;
512 }
while (result > 0 && current_bytes < start_bytes);
517 size_t write(
const uint8_t *data,
size_t len)
override {
518 if (current_bytes >= end_bytes)
return 0;
519 current_bytes += len;
520 if (current_bytes < start_bytes)
return len;
521 return p_print->write(data, len);
526 if (p_stream ==
nullptr)
return 0;
527 return current_bytes < end_bytes ? p_stream->available() : 0;
534 calculateByteLimits();
537 int availableForWrite()
override {
538 return current_bytes < end_bytes ? p_print->availableForWrite() : 0;
574 return end_bytes - start_bytes;
578 Stream *p_stream =
nullptr;
579 Print *p_print =
nullptr;
580 AudioInfoSupport *p_info =
nullptr;
581 uint32_t start_ms = 0;
582 uint32_t end_ms = UINT32_MAX;
583 uint32_t start_bytes = 0;
584 uint32_t end_bytes = UINT32_MAX;
585 uint32_t current_bytes = 0;
586 float compression_ratio = 1.0;
588 void consumeBytes(uint32_t len){
590 uint8_t buffer[1024];
592 int toread = min(1024, open);
593 p_stream->readBytes(buffer, toread);
596 current_bytes += len;
597 LOGD(
"consumed %u -> %u",(
unsigned) len, (
unsigned)current_bytes);
600 void calculateByteLimits() {
602 if (bytes_per_second > 0) {
603 start_bytes = bytes_per_second * start_ms / compression_ratio / 1000;
604 end_bytes = bytes_per_second * end_ms / compression_ratio / 1000;
606 LOGE(
"AudioInfo not defined");
630 AudioOutput::begin();
632 for (
auto &out : out_channels) {
633 for (
auto &ch : out.channels) {
635 LOGE(
"Channel '%d' not valid for max %d channels", ch, cfg.
channels);
647 channels.push_back(channel);
649 def.channels = channels;
651 def.p_audio_info = &out;
652 out_channels.push_back(def);
659 channels.push_back(channel);
661 def.channels = channels;
663 def.p_audio_info = &out;
664 out_channels.push_back(def);
671 channels.push_back(channel);
673 def.channels = channels;
675 out_channels.push_back(def);
682 channels.push_back(left);
683 channels.push_back(right);
685 def.channels = channels;
687 out_channels.push_back(def);
694 channels.push_back(left);
695 channels.push_back(right);
697 def.channels = channels;
699 def.p_audio_info = &out;
700 out_channels.push_back(def);
707 channels.push_back(left);
708 channels.push_back(right);
710 def.channels = channels;
712 def.p_audio_info = &out;
713 out_channels.push_back(def);
716 size_t write(
const uint8_t *data,
size_t len)
override {
717 if (!is_active)
return false;
718 LOGD(
"write %d", (
int)len);
721 return writeT<int16_t>(data, len);
723 return writeT<int24_t>(data, len);
725 return writeT<int32_t>(data, len);
734 for (
auto &info : out_channels) {
735 auto p_notify = info.p_audio_info;
736 if (p_notify !=
nullptr) {
739 p_notify->setAudioInfo(result);
746 Print *p_out =
nullptr;
753 template <
typename T>
754 size_t writeT(
const uint8_t *buffer,
size_t size) {
755 if (!is_active)
return 0;
756 int sample_count = size /
sizeof(T);
758 T *data = (T *)buffer;
760 for (
int i = 0; i < sample_count; i += cfg.
channels) {
762 for (
auto &out : out_channels) {
763 T out_frame[out.channels.size()] = {0};
765 for (
auto &ch : out.channels) {
768 out_frame[ch_out++] = frame[channel];
771 size_t written = out.buffer.writeArray((
const uint8_t *)&out_frame,
sizeof(out_frame));
773 if (out.buffer.availableForWrite()<
sizeof(out_frame)){
774 out.p_out->write(out.buffer.data(), out.buffer.available());
787 for (
auto &channels_select : out_channels) {
788 if (channels_select.p_out == out)
return channels_select.channels.size();
790 return defaultChannels;