2 #include "AudioConfig.h"
3 #include "AudioTools/AudioTypes.h"
4 #include "AudioTools/BaseConverter.h"
5 #include "AudioTools/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; }
80 operator bool() {
return is_active; }
85 SingleBuffer<uint8_t> tmp{MAX_SINGLE_CHARS};
86 bool is_active =
false;
114 CsvOutput(
int buffer_size = DEFAULT_BUFFER_SIZE,
bool active =
true) {
115 this->is_active = active;
120 bool active =
true) {
121 this->out_ptr = &out;
122 this->is_active = active;
155 this->is_active =
true;
165 this->is_active =
true;
171 virtual size_t write(
const uint8_t *data,
size_t len)
override {
172 LOGD(
"CsvOutput::write: %d", (
int)len);
174 LOGE(
"is not active");
179 LOGW(
"Channels not defined: using 2");
182 size_t lenChannels = len / (
sizeof(T) * cfg.
channels);
183 if (lenChannels > 0) {
184 writeFrames((T *)data, lenChannels);
185 }
else if (len ==
sizeof(T)) {
187 T *data_value = (T *)data;
188 out_ptr->print(data_value[0]);
194 out_ptr->print(delimiter_str);
197 LOGE(
"Unsupported size: %d for channels %d and bits: %d", (
int)len,
206 int availableForWrite()
override {
return 1024; }
210 Print *out_ptr = &Serial;
212 const char *delimiter_str =
",";
214 void writeFrames(T *data_ptr,
int frameCount) {
215 for (
size_t j = 0; j < frameCount; j++) {
216 for (
int ch = 0; ch < cfg.
channels; ch++) {
217 if (out_ptr !=
nullptr && data_ptr !=
nullptr) {
218 int value = *data_ptr;
219 out_ptr->print(value);
223 this->out_ptr->print(delimiter_str);
225 this->out_ptr->println();
231 template <
typename T>
using CsvStream = CsvOutput<T>;
241 HexDumpOutput(
int buffer_size = DEFAULT_BUFFER_SIZE,
bool active =
true) {
242 this->is_active = active;
247 bool active =
true) {
248 this->out_ptr = &out;
249 this->is_active = active;
252 bool begin()
override {
254 this->is_active =
true;
259 void flush()
override {
264 virtual size_t write(
const uint8_t *data,
size_t len)
override {
268 for (
size_t j = 0; j < len; j++) {
269 out_ptr->print(data[j], HEX);
273 out_ptr->print(
" - ");
284 AudioInfo defaultConfig(
RxTxMode mode = TX_MODE) {
290 Print *out_ptr = &Serial;
295 using HexDumpStream = HexDumpOutput;
304 template <
typename T>
310 setOutput(finalOutput);
311 setOutputCount(outputStreamCount);
314 void setOutput(
Print &finalOutput) { p_final_output = &finalOutput; }
316 void setOutputCount(
int count) {
317 output_count = count;
318 buffers.resize(count);
319 for (
int i = 0; i < count; i++) {
320 buffers[i] =
nullptr;
322 weights.resize(count);
323 for (
int i = 0; i < count; i++) {
327 update_total_weights();
333 if (channel <
size()) {
334 weights[channel] = weight;
336 LOGE(
"Invalid channel %d - max is %d", channel,
size() - 1);
338 update_total_weights();
342 bool begin(
int copy_buffer_size_bytes = DEFAULT_BUFFER_SIZE,
345 size_bytes = copy_buffer_size_bytes;
347 memory_type = memoryType;
348 allocate_buffers(size_bytes);
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) {
380 size_t write(
int idx,
const uint8_t *buffer_c,
size_t bytes) {
381 LOGD(
"write idx %d: %d", idx, bytes);
383 RingBuffer<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 "
404 if (p_buffer ==
nullptr)
412 if (p_buffer ==
nullptr)
414 return p_buffer->
available() *
sizeof(T);
423 size_t samples = availableSamples();
428 output.resize(samples);
429 memset(output.data(), 0, samples *
sizeof(T));
430 for (
int j = 0; j < output_count; j++) {
431 float weight = weights[j];
433 for (
int i = 0; i < samples; i++) {
434 output[i] += weight * buffers[j]->read() / total_weights;
439 LOGD(
"write to final out: %d", samples *
sizeof(T));
440 p_final_output->write((uint8_t *)output.data(), samples *
sizeof(T));
446 int availableSamples() {
448 for (
int j = 0; j < output_count; j++) {
449 int available_samples = buffers[j]->available();
450 if (available_samples > 0){
451 samples = MIN(size_bytes /
sizeof(T), (
size_t)available_samples);
459 if (
size != size_bytes) {
460 allocate_buffers(
size);
466 size_t writeSilence(
size_t bytes) {
467 if (bytes == 0)
return 0;
468 uint8_t silence[bytes] = {0};
469 return write(stream_idx, silence, bytes);
472 size_t writeSilence(
int idx,
size_t bytes){
473 if (bytes == 0)
return 0;
474 uint8_t silence[bytes] = {0};
475 return write(idx, silence, bytes);
480 is_auto_index = flag;
496 Vector<float> weights{0};
497 Print *p_final_output =
nullptr;
498 float total_weights = 0.0;
499 bool is_active =
false;
502 int output_count = 0;
504 void *p_memory =
nullptr;
505 bool is_auto_index =
true;
507 void update_total_weights() {
509 for (
int j = 0; j < weights.size(); j++) {
510 total_weights += weights[j];
514 void allocate_buffers(
int size) {
516 for (
int j = 0; j < output_count; j++) {
517 if (buffers[j] !=
nullptr) {
520 #if defined(ESP32) && defined(ARDUINO)
521 if (memory_type == PS_RAM && ESP.getFreePsram() >=
size) {
522 p_memory = ps_malloc(
size);
523 LOGI(
"Buffer %d allocated %d bytes in PS_RAM", j,
size);
525 p_memory = malloc(
size);
526 LOGI(
"Buffer %d allocated %d bytes in RAM", j,
size);
528 if (p_memory !=
nullptr) {
529 buffers[j] =
new (p_memory) RingBuffer<T>(
size /
sizeof(T));
531 LOGE(
"Not enough memory to allocate %d bytes",
size);
534 buffers[j] =
new RingBuffer<T>(
size /
sizeof(T));
539 void free_buffers() {
541 for (
int j = 0; j < output_count; j++) {
542 if (buffers[j] !=
nullptr) {
545 if (p_memory !=
nullptr) {
549 buffers[j] =
nullptr;
567 if (p_next ==
nullptr) {
568 LOGE(
"start must not be null");
579 size_t write(
const uint8_t *data,
size_t len)
override {
580 if (p_next ==
nullptr)
582 if (pos + len <= max_size) {
583 memcpy(p_next, data, len);
588 LOGE(
"Buffer too small: pos:%d, size: %d ", pos, (
int)max_size);
593 int availableForWrite()
override {
return max_size - pos; }
595 int size() {
return max_size; }
599 uint8_t *p_start =
nullptr;
600 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");
MemoryType
Memory types.
Definition: AudioTypes.h:33
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition: AudioTypes.h:26