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;
312 setOutput(finalOutput);
313 setOutputCount(outputStreamCount);
316 void setOutput(
Print &finalOutput) { p_final_output = &finalOutput; }
318 void setOutputCount(
int count) {
319 output_count = count;
320 buffers.resize(count);
321 for (
int i = 0; i < count; i++) {
322 buffers[i] =
nullptr;
324 weights.resize(count);
325 for (
int i = 0; i < count; i++) {
329 update_total_weights();
335 if (channel <
size()) {
336 weights[channel] = weight;
338 LOGE(
"Invalid channel %d - max is %d", channel,
size() - 1);
340 update_total_weights();
344 bool begin(
int copy_buffer_size_bytes = DEFAULT_BUFFER_SIZE) {
346 size_bytes = copy_buffer_size_bytes;
348 allocate_buffers(size_bytes);
344 bool begin(
int copy_buffer_size_bytes = DEFAULT_BUFFER_SIZE) {
…}
361 int size() {
return output_count; }
363 size_t write(uint8_t)
override {
return 0; }
367 size_t write(
const uint8_t *data,
size_t len)
override {
368 size_t result = write(stream_idx, data, len);
372 if (stream_idx >= output_count) {
367 size_t write(
const uint8_t *data,
size_t len)
override {
…}
380 size_t write(
int idx,
const uint8_t *buffer_c,
size_t bytes) {
381 LOGD(
"write idx %d: %d", idx, (
int)bytes);
383 BaseBuffer<T> *p_buffer = idx < output_count ? buffers[idx] :
nullptr;
384 assert(p_buffer !=
nullptr);
385 size_t samples = bytes /
sizeof(T);
387 result = p_buffer->
writeArray((T *)buffer_c, samples) *
sizeof(T);
389 LOGW(
"Available Buffer %d too small %d: requested: %d -> increase the "
390 "buffer size", (
int) idx,
380 size_t write(
int idx,
const uint8_t *buffer_c,
size_t bytes) {
…}
404 if (p_buffer ==
nullptr)
412 if (p_buffer ==
nullptr)
414 return p_buffer->
available() *
sizeof(T);
419 return 100.0 *
available(idx) / size_bytes;
428 size_t samples = availableSamples();
433 output.resize(samples);
434 memset(output.data(), 0, samples *
sizeof(T));
435 for (
int j = 0; j < output_count; j++) {
436 float weight = weights[j];
438 for (
int i = 0; i < samples; i++) {
440 buffers[j]->read(sample);
441 output[i] += weight * sample / total_weights;
446 LOGD(
"write to final out: %d",
static_cast<int>(samples *
sizeof(T)));
447 p_final_output->write((uint8_t *)output.data(), samples *
sizeof(T));
453 int availableSamples() {
455 for (
int j = 0; j < output_count; j++) {
456 int available_samples = buffers[j]->available();
457 if (available_samples > 0){
458 samples = MIN(size_bytes /
sizeof(T), (
size_t)available_samples);
466 if (
size != size_bytes) {
467 allocate_buffers(
size);
472 size_t writeSilence(
size_t bytes) {
473 if (bytes == 0)
return 0;
474 uint8_t silence[bytes];
475 memset(silence, 0, bytes);
476 return write(stream_idx, silence, bytes);
479 size_t writeSilence(
int idx,
size_t bytes){
480 if (bytes == 0)
return 0;
481 uint8_t silence[bytes];
482 memset(silence, 0, bytes);
483 return write(idx, silence, bytes);
488 is_auto_index = flag;
503 create_buffer_cb = cb;
508 return idx < output_count ? buffers[idx] :
nullptr;
514 Vector<float> weights{0};
515 Print *p_final_output =
nullptr;
516 float total_weights = 0.0;
517 bool is_active =
false;
520 int output_count = 0;
521 void *p_memory =
nullptr;
522 bool is_auto_index =
true;
523 BaseBuffer<T>* (*create_buffer_cb)(
int size) = create_buffer;
525 static BaseBuffer<T>* create_buffer(
int size) {
526 return new RingBuffer<T>(
size /
sizeof(T));
529 void update_total_weights() {
531 for (
int j = 0; j < weights.size(); j++) {
532 total_weights += weights[j];
536 void allocate_buffers(
int size) {
538 for (
int j = 0; j < output_count; j++) {
539 if (buffers[j] !=
nullptr) {
542 buffers[j] = create_buffer(
size);
546 void free_buffers() {
548 for (
int j = 0; j < output_count; j++) {
549 if (buffers[j] !=
nullptr) {
551 buffers[j] =
nullptr;
569 if (p_next ==
nullptr) {
570 LOGE(
"start must not be null");
574 bool begin()
override {
581 size_t write(
const uint8_t *data,
size_t len)
override {
582 if (p_next ==
nullptr)
584 if (pos + len <= max_size) {
585 memcpy(p_next, data, len);
590 LOGE(
"Buffer too small: pos:%d, size: %d ", pos, (
int)max_size);
595 int availableForWrite()
override {
return max_size - pos; }
597 int size() {
return max_size; }
601 uint8_t *p_start =
nullptr;
602 uint8_t *p_next =
nullptr;
625 def.channel = channel;
627 out_channels.push_back(def);
630 size_t write(
const uint8_t *data,
size_t len)
override {
633 return writeT<int16_t>(data, len);
635 return writeT<int24_t>(data, len);
637 return writeT<int32_t>(data, len);
645 Print *p_out =
nullptr;
650 template <
typename T>
651 size_t writeT(
const uint8_t *buffer,
size_t size) {
652 int sample_count = size /
sizeof(T);
653 int result_size = sample_count / cfg.
channels;
654 T *data = (T *)buffer;
655 T result[result_size];
657 for (
int ch = 0; ch < out_channels.size(); ch++) {
661 for (
int j = def.channel; j < sample_count; j += cfg.
channels) {
662 result[i++] = data[j];
666 def.p_out->write((uint8_t *)result, result_size *
sizeof(T));
667 if (written != result_size *
sizeof(T)) {
668 LOGW(
"Could not write all samples");
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition AudioTypes.h:28