Skip to content

Commit 08bb5e8

Browse files
refactoring
1 parent 50d033a commit 08bb5e8

File tree

3 files changed

+488
-450
lines changed

3 files changed

+488
-450
lines changed

src/audio_module.h

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,22 @@
5353
#include <ml_types.h>
5454

5555

56+
typedef enum
57+
{
58+
AUDIO_PORT_0 = 0,
59+
AUDIO_PORT_1 = 1,
60+
// Add more outputs as needed
61+
} AudioPortId;
62+
63+
5664
void Audio_Setup(void);
5765
void Audio_Output(const float *left, const float *right);
5866
void Audio_OutputMono(const int32_t *samples);
5967
void Audio_Output(const int32_t *samples);
6068
void Audio_Output(const int16_t *samples);
6169
void Audio_Output(const Q1_14 *samples);
6270
void Audio_Output(const int16_t *left, const int16_t *right);
71+
void Audio_Output(AudioPortId audio_port, const int16_t *left, const int16_t *right);
6372
void Audio_Output(const Q1_14 *left, const Q1_14 *right);
6473
void Audio_Input(float *left, float *right);
6574
void Audio_Input(Q1_14 *left, Q1_14 *right);
@@ -156,9 +165,8 @@ static float saw_right[SAMPLE_BUFFER_SIZE];
156165
static int32_t saw_i32[SAMPLE_BUFFER_SIZE];
157166
#endif
158167
#ifdef OUTPUT_SINE_TEST
159-
static float sin_left[SAMPLE_BUFFER_SIZE];
160-
static float sin_right[SAMPLE_BUFFER_SIZE];
161-
static int32_t sine_i32[SAMPLE_BUFFER_SIZE];
168+
static float sin[4][SAMPLE_BUFFER_SIZE];
169+
static int32_t sine_i32[4][SAMPLE_BUFFER_SIZE];
162170
#endif
163171

164172
void Audio_Setup(void)
@@ -192,27 +200,28 @@ void Audio_Setup(void)
192200
w *= 1.0f / ((float)SAMPLE_BUFFER_SIZE);
193201
w *= 2.0f * M_PI;
194202
float sine = sin(w);
195-
sin_left[i] = sine;
196-
sin_right[i] = sin(w * 2.0f);
197-
sine *= 1073741824;
198-
sine_i32[i] = sine;
203+
for (int n = 0; n < 4; n++)
204+
{
205+
sine = sin(n * w * 2.0f);
206+
sin[n][i] = sine;
207+
sine *= 1073741824;
208+
sine_i32[n][i] = sine;
209+
}
199210
}
200211
#endif
201212

202213
#ifdef ESP32_AUDIO_KIT
203214
#ifdef ES8388_ENABLED
204-
ES8388_Setup();
205-
ES8388_SetIn2OoutVOL(0, 0);
215+
ES8388_Setup(ES8388_ID0);
216+
ES8388_SetIn2OoutVOL(ES8388_ID0, 0.0f);
206217
#else
207218
ac101_setup();
208219
#endif
209220
#endif
210221

211222
#if defined(DUAL_CODEC_ENABLED) && defined(ES8388_ENABLED)
212-
ES8388_SelectCodec(1);
213-
ES8388_Setup();
214-
ES8388_SetIn2OoutVOL(0, 0);
215-
ES8388_SelectCodec(0);
223+
ES8388_Setup(ES8388_ID1);
224+
ES8388_SetIn2OoutVOL(ES8388_ID1, 0.0f);
216225
#endif
217226

218227
#ifdef WM8978_ENABLED
@@ -644,7 +653,7 @@ void Audio_Output(const Q1_14 *left, const Q1_14 *right)
644653
}
645654

646655
#ifndef ARDUINO_SEEED_XIAO_M0
647-
void Audio_Output(const int16_t *left, const int16_t *right)
656+
void Audio_Output(AudioPortId audio_port, const int16_t *left, const int16_t *right)
648657
{
649658
#ifdef ESP8266
650659
for (int i = 0; i < SAMPLE_BUFFER_SIZE; i++)
@@ -709,7 +718,7 @@ void Audio_Output(const int16_t *left, const int16_t *right)
709718
}
710719
}
711720
#else
712-
i2s_write_stereo_samples_i16(left, right, SAMPLE_BUFFER_SIZE);
721+
i2s_write_stereo_samples_i16(left, right, SAMPLE_BUFFER_SIZE, audio_port);
713722
#endif
714723
#endif /* ESP32 */
715724

@@ -836,6 +845,11 @@ void Audio_Output(const int16_t *left, const int16_t *right)
836845
#endif
837846
#endif /* PICO_AUDIO_I2S */
838847
}
848+
849+
void Audio_Output(const int16_t *left, const int16_t *right)
850+
{
851+
Audio_Output(AUDIO_PORT_0, left, right);
852+
}
839853
#endif
840854

841855
#if (defined ESP32) || (defined TEENSYDUINO) || (defined ARDUINO_DAISY_SEED) || (defined ARDUINO_GENERIC_F407VGTX) || (defined ARDUINO_DISCO_F407VG) || (defined ARDUINO_BLACK_F407VE) || (defined ARDUINO_ARCH_RP2040) || (((defined ARDUINO_RASPBERRY_PI_PICO) || (defined ARDUINO_GENERIC_RP2040)) && (defined RP2040_AUDIO_PWM))
@@ -868,8 +882,8 @@ void Audio_Output(const float *left, const float *right)
868882
right = saw_right;
869883
#endif
870884
#ifdef OUTPUT_SINE_TEST
871-
left = sin_left;
872-
right = sin_right;
885+
left = sin[0];
886+
right = sin[1];
873887
#endif
874888

875889
#ifdef ESP32

0 commit comments

Comments
 (0)