2 #include "AudioConfig.h"
3 #include "AudioTools/CoreAudio/AudioTypes.h"
4 #include "AudioTools/CoreAudio/BaseConverter.h"
5 #include "AudioTools/CoreAudio/Buffers.h"
10 #define PRINT_FLUSH_OVERRIDE override
12 #define PRINT_FLUSH_OVERRIDE
26 virtual size_t write(
const uint8_t *data,
size_t len)
override = 0;
28 virtual size_t write(uint8_t ch)
override {
35 virtual int availableForWrite()
override {
return DEFAULT_BUFFER_SIZE; }
39 virtual void flush() PRINT_FLUSH_OVERRIDE {
53 if (out) notifyAudioChange(out);
65 for (
int j = 0; j < len / 2; j++) {
66 write((uint8_t *)&zero, 2);
75 virtual bool begin() {
79 virtual void end() { is_active =
false; }
81 virtual operator bool() {
return is_active; }
86 SingleBuffer<uint8_t> tmp{MAX_SINGLE_CHARS};
87 bool is_active =
false;
115 CsvOutput(
int buffer_size = DEFAULT_BUFFER_SIZE,
bool active =
true) {
116 this->is_active = active;
121 bool active =
true) {
122 this->out_ptr = &out;
123 this->is_active = active;
156 this->is_active =
true;
166 this->is_active =
true;
172 virtual size_t write(
const uint8_t *data,
size_t len)
override {
173 LOGD(
"CsvOutput::write: %d", (
int)len);
175 LOGE(
"is not active");
184 LOGW(
"Channels not defined: using 2");
187 size_t lenChannels = len / (
sizeof(T) * cfg.
channels);
188 if (lenChannels > 0) {
189 writeFrames((T *)data, lenChannels);
190 }
else if (len ==
sizeof(T)) {
192 T *data_value = (T *)data;
193 out_ptr->print(data_value[0]);
199 out_ptr->print(delimiter_str);
202 LOGE(
"Unsupported size: %d for channels %d and bits: %d", (
int)len,
211 int availableForWrite()
override {
return 1024; }
215 Print *out_ptr = &Serial;
217 const char *delimiter_str =
",";
219 void writeFrames(T *data_ptr,
int frameCount) {
220 for (
size_t j = 0; j < frameCount; j++) {
221 for (
int ch = 0; ch < cfg.
channels; ch++) {
222 if (out_ptr !=
nullptr && data_ptr !=
nullptr) {
224 out_ptr->print(value);
228 this->out_ptr->print(delimiter_str);
230 this->out_ptr->println();
237 template <
typename T>
using CsvStream = CsvOutput<T>;
248 HexDumpOutput(
int buffer_size = DEFAULT_BUFFER_SIZE,
bool active =
true) {
249 this->is_active = active;
254 bool active =
true) {
255 this->out_ptr = &out;
256 this->is_active = active;
259 bool begin()
override {
261 this->is_active =
true;
266 void flush()
override {
271 virtual size_t write(
const uint8_t *data,
size_t len)
override {
275 for (
size_t j = 0; j < len; j++) {
276 out_ptr->print(data[j], HEX);
280 out_ptr->print(
" - ");
291 AudioInfo defaultConfig(
RxTxMode mode = TX_MODE) {
297 Print *out_ptr = &Serial;
303 using HexDumpOutput = HexDumpOutput;
313 template <
typename T>
319 setOutput(finalOutput);
320 setOutputCount(outputStreamCount);
323 void setOutput(
Print &finalOutput) { p_final_output = &finalOutput; }
325 void setOutputCount(
int count) {
326 output_count = count;
327 buffers.resize(count);
328 for (
int i = 0; i < count; i++) {
329 buffers[i] =
nullptr;
331 weights.resize(count);
332 for (
int i = 0; i < count; i++) {
336 update_total_weights();
342 if (channel <
size()) {
343 weights[channel] = weight;
345 LOGE(
"Invalid channel %d - max is %d", channel,
size() - 1);
347 update_total_weights();
351 bool begin(
int copy_buffer_size_bytes = DEFAULT_BUFFER_SIZE,
354 size_bytes = copy_buffer_size_bytes;
356 memory_type = memoryType;
357 allocate_buffers(size_bytes);
370 int size() {
return output_count; }
372 size_t write(uint8_t)
override {
return 0; }
376 size_t write(
const uint8_t *data,
size_t len)
override {
377 size_t result = write(stream_idx, data, len);
381 if (stream_idx >= output_count) {
389 size_t write(
int idx,
const uint8_t *buffer_c,
size_t bytes) {
390 LOGD(
"write idx %d: %d", idx, bytes);
392 RingBuffer<T> *p_buffer = idx < output_count ? buffers[idx] :
nullptr;
393 assert(p_buffer !=
nullptr);
394 size_t samples = bytes /
sizeof(T);
396 result = p_buffer->
writeArray((T *)buffer_c, samples) *
sizeof(T);
398 LOGW(
"Available Buffer %d too small %d: requested: %d -> increase the "
413 if (p_buffer ==
nullptr)
421 if (p_buffer ==
nullptr)
423 return p_buffer->
available() *
sizeof(T);
432 size_t samples = availableSamples();
437 output.resize(samples);
438 memset(output.data(), 0, samples *
sizeof(T));
439 for (
int j = 0; j < output_count; j++) {
440 float weight = weights[j];
442 for (
int i = 0; i < samples; i++) {
443 output[i] += weight * buffers[j]->read() / total_weights;
448 LOGD(
"write to final out: %d", samples *
sizeof(T));
449 p_final_output->write((uint8_t *)output.data(), samples *
sizeof(T));
455 int availableSamples() {
457 for (
int j = 0; j < output_count; j++) {
458 int available_samples = buffers[j]->available();
459 if (available_samples > 0){
460 samples = MIN(size_bytes /
sizeof(T), (
size_t)available_samples);
468 if (
size != size_bytes) {
469 allocate_buffers(
size);
475 size_t writeSilence(
size_t bytes) {
476 if (bytes == 0)
return 0;
477 uint8_t silence[bytes] = {0};
478 return write(stream_idx, silence, bytes);
481 size_t writeSilence(
int idx,
size_t bytes){
482 if (bytes == 0)
return 0;
483 uint8_t silence[bytes] = {0};
484 return write(idx, silence, bytes);
489 is_auto_index = flag;
505 Vector<float> weights{0};
506 Print *p_final_output =
nullptr;
507 float total_weights = 0.0;
508 bool is_active =
false;
511 int output_count = 0;
513 void *p_memory =
nullptr;
514 bool is_auto_index =
true;
516 void update_total_weights() {
518 for (
int j = 0; j < weights.size(); j++) {
519 total_weights += weights[j];
523 void allocate_buffers(
int size) {
525 for (
int j = 0; j < output_count; j++) {
526 if (buffers[j] !=
nullptr) {
529 #if defined(ESP32) && defined(ARDUINO)
530 if (memory_type == PS_RAM && ESP.getFreePsram() >=
size) {
531 p_memory = ps_malloc(
size);
532 LOGI(
"Buffer %d allocated %d bytes in PS_RAM", j,
size);
534 p_memory = malloc(
size);
535 LOGI(
"Buffer %d allocated %d bytes in RAM", j,
size);
537 if (p_memory !=
nullptr) {
538 buffers[j] =
new (p_memory) RingBuffer<T>(
size /
sizeof(T));
540 LOGE(
"Not enough memory to allocate %d bytes",
size);
543 buffers[j] =
new RingBuffer<T>(
size /
sizeof(T));
548 void free_buffers() {
550 for (
int j = 0; j < output_count; j++) {
551 if (buffers[j] !=
nullptr) {
554 if (p_memory !=
nullptr) {
558 buffers[j] =
nullptr;
576 if (p_next ==
nullptr) {
577 LOGE(
"start must not be null");
588 size_t write(
const uint8_t *data,
size_t len)
override {
589 if (p_next ==
nullptr)
591 if (pos + len <= max_size) {
592 memcpy(p_next, data, len);
597 LOGE(
"Buffer too small: pos:%d, size: %d ", pos, (
int)max_size);
602 int availableForWrite()
override {
return max_size - pos; }
604 int size() {
return max_size; }
608 uint8_t *p_start =
nullptr;
609 uint8_t *p_next =
nullptr;
636 def.channel = channel;
638 out_channels.push_back(def);
641 size_t write(
const uint8_t *data,
size_t len)
override {
644 return writeT<int16_t>(data, len);
646 return writeT<int24_t>(data, len);
648 return writeT<int32_t>(data, len);
656 Print *p_out =
nullptr;
661 template <
typename T>
662 size_t writeT(
const uint8_t *buffer,
size_t size) {
663 int sample_count = size /
sizeof(T);
664 int result_size = sample_count / cfg.
channels;
665 T *data = (T *)buffer;
666 T result[result_size];
668 for (
int ch = 0; ch < out_channels.size(); ch++) {
672 for (
int j = def.channel; j < sample_count; j += cfg.
channels) {
673 result[i++] = data[j];
677 def.p_out->write((uint8_t *)result, result_size *
sizeof(T));
678 if (written != result_size *
sizeof(T)) {
679 LOGW(
"Could not write all samples");
MemoryType
Memory types.
Definition: AudioTypes.h:35
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition: AudioTypes.h:28