2#include "AudioToolsConfig.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,
172 virtual size_t write(
const uint8_t *data,
size_t len)
override {
…}
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();
244 HexDumpOutput(
int buffer_size = DEFAULT_BUFFER_SIZE,
bool active =
true) {
245 this->is_active = active;
250 bool active =
true) {
251 this->out_ptr = &out;
252 this->is_active = active;
255 bool begin()
override {
257 this->is_active =
true;
262 void flush()
override {
267 virtual size_t write(
const uint8_t *data,
size_t len)
override {
271 for (
size_t j = 0; j < len; j++) {
272 out_ptr->print(data[j], HEX);
276 out_ptr->print(
" - ");
287 AudioInfo defaultConfig(
RxTxMode mode = TX_MODE) {
293 Print *out_ptr = &Serial;
311 setOutput(finalOutput);
312 setOutputCount(outputStreamCount);
315 void setOutput(
Print &finalOutput) { p_final_output = &finalOutput; }
317 void setOutputCount(
int count) {
318 output_count = count;
319 buffers.resize(count);
320 for (
int i = 0; i < count; i++) {
321 buffers[i] =
nullptr;
323 weights.resize(count);
324 for (
int i = 0; i < count; i++) {
328 update_total_weights();
334 if (channel <
size()) {
335 weights[channel] = weight;
337 LOGE(
"Invalid channel %d - max is %d", channel,
size() - 1);
339 update_total_weights();
343 bool begin(
int copy_buffer_size_bytes = DEFAULT_BUFFER_SIZE,
346 size_bytes = copy_buffer_size_bytes;
348 memory_type = memoryType;
349 allocate_buffers(size_bytes);
343 bool begin(
int copy_buffer_size_bytes = DEFAULT_BUFFER_SIZE, {
…}
362 int size() {
return output_count; }
364 size_t write(uint8_t)
override {
return 0; }
368 size_t write(
const uint8_t *data,
size_t len)
override {
369 size_t result = write(stream_idx, data, len);
373 if (stream_idx >= output_count) {
368 size_t write(
const uint8_t *data,
size_t len)
override {
…}
381 size_t write(
int idx,
const uint8_t *buffer_c,
size_t bytes) {
382 LOGD(
"write idx %d: %d", idx, bytes);
384 RingBuffer<T> *p_buffer = idx < output_count ? buffers[idx] :
nullptr;
385 assert(p_buffer !=
nullptr);
386 size_t samples = bytes /
sizeof(T);
388 result = p_buffer->
writeArray((T *)buffer_c, samples) *
sizeof(T);
390 LOGW(
"Available Buffer %d too small %d: requested: %d -> increase the "
381 size_t write(
int idx,
const uint8_t *buffer_c,
size_t bytes) {
…}
405 if (p_buffer ==
nullptr)
413 if (p_buffer ==
nullptr)
415 return p_buffer->
available() *
sizeof(T);
424 size_t samples = availableSamples();
429 output.resize(samples);
430 memset(output.data(), 0, samples *
sizeof(T));
431 for (
int j = 0; j < output_count; j++) {
432 float weight = weights[j];
434 for (
int i = 0; i < samples; i++) {
436 buffers[j]->read(sample);
437 output[i] += weight * sample / total_weights;
442 LOGD(
"write to final out: %d", samples *
sizeof(T));
443 p_final_output->write((uint8_t *)output.data(), samples *
sizeof(T));
449 int availableSamples() {
451 for (
int j = 0; j < output_count; j++) {
452 int available_samples = buffers[j]->available();
453 if (available_samples > 0){
454 samples = MIN(size_bytes /
sizeof(T), (
size_t)available_samples);
462 if (
size != size_bytes) {
463 allocate_buffers(
size);
469 size_t writeSilence(
size_t bytes) {
470 if (bytes == 0)
return 0;
471 uint8_t silence[bytes];
472 memset(silence, 0, bytes);
473 return write(stream_idx, silence, bytes);
476 size_t writeSilence(
int idx,
size_t bytes){
477 if (bytes == 0)
return 0;
478 uint8_t silence[bytes];
479 memset(silence, 0, bytes);
480 return write(idx, silence, bytes);
485 is_auto_index = flag;
501 Vector<float> weights{0};
502 Print *p_final_output =
nullptr;
503 float total_weights = 0.0;
504 bool is_active =
false;
507 int output_count = 0;
509 void *p_memory =
nullptr;
510 bool is_auto_index =
true;
512 void update_total_weights() {
514 for (
int j = 0; j < weights.size(); j++) {
515 total_weights += weights[j];
519 void allocate_buffers(
int size) {
521 for (
int j = 0; j < output_count; j++) {
522 if (buffers[j] !=
nullptr) {
525#if defined(ESP32) && defined(ARDUINO)
526 if (memory_type == PS_RAM && ESP.getFreePsram() >=
size) {
527 p_memory = ps_malloc(
size);
528 LOGI(
"Buffer %d allocated %d bytes in PS_RAM", j,
size);
530 p_memory = malloc(
size);
531 LOGI(
"Buffer %d allocated %d bytes in RAM", j,
size);
533 if (p_memory !=
nullptr) {
534 buffers[j] =
new (p_memory) RingBuffer<T>(
size /
sizeof(T));
536 LOGE(
"Not enough memory to allocate %d bytes",
size);
539 buffers[j] =
new RingBuffer<T>(
size /
sizeof(T));
544 void free_buffers() {
546 for (
int j = 0; j < output_count; j++) {
547 if (buffers[j] !=
nullptr) {
550 if (p_memory !=
nullptr) {
554 buffers[j] =
nullptr;
572 if (p_next ==
nullptr) {
573 LOGE(
"start must not be null");
577 bool begin()
override {
584 size_t write(
const uint8_t *data,
size_t len)
override {
585 if (p_next ==
nullptr)
587 if (pos + len <= max_size) {
588 memcpy(p_next, data, len);
593 LOGE(
"Buffer too small: pos:%d, size: %d ", pos, (
int)max_size);
598 int availableForWrite()
override {
return max_size - pos; }
600 int size() {
return max_size; }
604 uint8_t *p_start =
nullptr;
605 uint8_t *p_next =
nullptr;
628 def.channel = channel;
630 out_channels.push_back(def);
633 size_t write(
const uint8_t *data,
size_t len)
override {
636 return writeT<int16_t>(data, len);
638 return writeT<int24_t>(data, len);
640 return writeT<int32_t>(data, len);
648 Print *p_out =
nullptr;
653 template <
typename T>
654 size_t writeT(
const uint8_t *buffer,
size_t size) {
655 int sample_count = size /
sizeof(T);
656 int result_size = sample_count / cfg.
channels;
657 T *data = (T *)buffer;
658 T result[result_size];
660 for (
int ch = 0; ch < out_channels.size(); ch++) {
664 for (
int j = def.channel; j < sample_count; j += cfg.
channels) {
665 result[i++] = data[j];
669 def.p_out->write((uint8_t *)result, result_size *
sizeof(T));
670 if (written != result_size *
sizeof(T)) {
671 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