|
3 | 3 | #include "AudioTools/CoreAudio/BaseConverter.h"
|
4 | 4 | #include "AudioTools/CoreAudio/Buffers.h"
|
5 | 5 | #include "AudioToolsConfig.h"
|
| 6 | +#if defined(USE_ESP32_DSP) |
| 7 | +#include "esp_dsp.h" |
| 8 | +#endif |
6 | 9 |
|
7 | 10 | namespace audio_tools {
|
8 | 11 |
|
@@ -596,18 +599,41 @@ class OutputMixer : public Print {
|
596 | 599 |
|
597 | 600 | /// Mixes the samples from all input streams into the output buffer
|
598 | 601 | inline void mixSamples(size_t samples) {
|
| 602 | +#if defined(USE_ESP32_DSP) |
| 603 | + // Temporary float buffers for mixing |
| 604 | + float mix_out[samples] = {0.0f}; |
| 605 | + float temp[samples] = {0.0f}; |
| 606 | + |
| 607 | + for (uint8_t j = 0; j < output_count; j++) { |
| 608 | + const float factor = weights[j] / total_weights; |
| 609 | + // Read int16_t samples and convert to float |
| 610 | + for (uint16_t i = 0; i < samples; i++) { |
| 611 | + int16_t s = 0; |
| 612 | + buffers[j]->read(s); |
| 613 | + temp[i] = static_cast<float>(s) * factor; |
| 614 | + } |
| 615 | + // Add to output |
| 616 | + dsps_add_f32(mix_out, temp, mix_out, samples, 1, 1, 1); |
| 617 | + } |
| 618 | + // Convert back to int16_t with clamping |
| 619 | + output.resize(samples); |
| 620 | + for (size_t i = 0; i < samples; i++) { |
| 621 | + float v = mix_out[i]; |
| 622 | + output[i] = static_cast<int16_t>(v); |
| 623 | + } |
| 624 | +#else |
| 625 | + // Fallback: original scalar code |
599 | 626 | output.resize(samples);
|
600 | 627 | memset(output.data(), 0, samples * sizeof(T));
|
601 |
| - |
602 | 628 | for (int j = 0; j < output_count; j++) {
|
603 | 629 | float factor = weights[j] / total_weights;
|
604 |
| - // sum up input samples to result samples |
605 | 630 | for (int i = 0; i < samples; i++) {
|
606 |
| - T sample = 0; |
| 631 | + int16_t sample = 0; |
607 | 632 | buffers[j]->read(sample);
|
608 |
| - output[i] += factor * sample; |
| 633 | + output[i] += static_cast<int16_t>(factor * sample); |
609 | 634 | }
|
610 | 635 | }
|
| 636 | +#endif |
611 | 637 | }
|
612 | 638 |
|
613 | 639 | /// Recalculates the total weights for normalization
|
|
0 commit comments