Skip to content

Commit a2a5b33

Browse files
fixed i2s dual output
1 parent 551927e commit a2a5b33

File tree

3 files changed

+56
-70
lines changed

3 files changed

+56
-70
lines changed

src/audio_module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ void Audio_Output(AudioPortId audio_port, const int16_t *left, const int16_t *ri
718718
}
719719
}
720720
#else
721-
i2s_write_stereo_samples_i16(left, right, SAMPLE_BUFFER_SIZE, audio_port);
721+
i2s_write_stereo_samples_i16((uint8_t)audio_port, left, right, SAMPLE_BUFFER_SIZE);
722722
#endif
723723
#endif /* ESP32 */
724724

src/es8388.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
#define ES8388_ID0 (uint8_t)0
5656
#define ES8388_ID1 (uint8_t)1
5757

58-
void ES8388_Setup(void);
59-
void ES8388_Setup(int codec_id);
58+
bool ES8388_Setup(void);
59+
bool ES8388_Setup(int codec_id);
6060
void ES8388_SetIn2OoutVOL(uint8_t codec_id, float vol);
6161
void ES8388_SetDACVOL(float vol);
6262
void ES8388_SetDACVOL(uint8_t codec_id, float vol);
@@ -185,7 +185,15 @@ bool ES8388_WriteReg(uint8_t reg, uint8_t val)
185185

186186
bool ES8388_begin(uint8_t codec_id, int sda, int scl, uint32_t frequency)
187187
{
188-
bool ok = Wire.begin(sda, scl, frequency);
188+
static bool wireInitReq = true;
189+
190+
bool ok = true;
191+
192+
if (wireInitReq)
193+
{
194+
ok = Wire.begin(sda, scl, frequency);
195+
wireInitReq = false;
196+
}
189197

190198
/* Reset all registers, readback default as sanity check */
191199
delay(100);
@@ -475,7 +483,7 @@ void ES8388_SetOUT2VOL(float vol)
475483
ES8388_SetOUT2VOL(ES8388_ID0, vol);
476484
}
477485

478-
void ES8388_Setup(int codec_id)
486+
bool ES8388_Setup(int codec_id)
479487
{
480488
const uint32_t i2c_freq = 400000;
481489

@@ -484,12 +492,20 @@ void ES8388_Setup(int codec_id)
484492
Serial.printf(" SCL: %d\n", ES8388_PIN_SCL);
485493
Serial.printf(" freq: %" PRIu32 "\n", i2c_freq);
486494

487-
while (not ES8388_begin(codec_id, ES8388_PIN_SDA, ES8388_PIN_SCL, i2c_freq))
495+
uint8_t retries = 10;
496+
497+
while (not ES8388_begin(codec_id, ES8388_PIN_SDA, ES8388_PIN_SCL, i2c_freq) && (retries > 10))
488498
{
489499
Serial.printf("Failed!\n");
490500
Serial.printf("It may be possible that SCL and SDA are incorrect\n");
491501
Serial.printf("In boards/board_audio_kit_es8388.h you can change the define ES8388_CFG_I2C to use another pin setting\n");
492502
delay(1000);
503+
retries--;
504+
if (retries == 0)
505+
{
506+
Serial.printf("give up!!!!\n");
507+
return false;
508+
}
493509
}
494510

495511
ES8388_WriteReg(codec_id, ES8388_CHIPPOWER, 0xFF); //reset and stop es8388
@@ -592,11 +608,13 @@ void ES8388_Setup(int codec_id)
592608

593609
Serial.printf("ES8388 setup finished!\n");
594610
es8388_read_all(codec_id);
611+
612+
return true;
595613
}
596614

597-
void ES8388_Setup(void)
615+
bool ES8388_Setup(void)
598616
{
599-
ES8388_Setup(ES8388_ID0);
617+
return ES8388_Setup(ES8388_ID0);
600618
}
601619

602620
#endif

src/i2s_interface.h

Lines changed: 30 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@
5656
#ifdef ML_SYNTH_INLINE_DECLARATION
5757

5858
void setup_i2s(void);
59-
bool i2s_write_stereo_samples_i16(const int16_t *fl_sample, const int16_t *fr_sample, const int buffLen);
60-
bool i2s_write_stereo_samples_i16(const int16_t *fl_sample, const int16_t *fr_sample, const int buffLen, uint8_t stream_id);
6159
bool i2s_write_stereo_samples_buff(const float *fl_sample, const float *fr_sample, const int buffLen);
62-
bool i2s_write_stereo_samples_buff(const float *fl_sample, const float *fr_sample, const int buffLen, uint8_t stream_id);
60+
bool i2s_write_stereo_samples_buff(uint8_t stream_id, const float *fl_sample, const float *fr_sample, const int buffLen, uint8_t codec_num);
61+
bool i2s_write_stereo_samples_i16(const int16_t *fl_sample, const int16_t *fr_sample, const int buffLen);
62+
bool i2s_write_stereo_samples_i16(uint8_t stream_id, const int16_t *fl_sample, const int16_t *fr_sample, const int buffLen);
6363
void i2s_read_stereo_samples_buff(float *fl_sample, float *fr_sample, const int buffLen);
6464
void i2s_read_stereo_samples_buff(int16_t *fl_sample, int16_t *fr_sample, const int buffLen);
65-
void i2s_read_stereo_samples_buff(int16_t *fl_sample, int16_t *fr_sample, const int buffLen, uint8_t stream_id);
65+
void i2s_read_stereo_samples_buff(uint8_t stream_id, float *fl_sample, float *fr_sample, const int buffLen);
66+
void i2s_read_stereo_samples_i16(uint8_t stream_id, int16_t *fl_sample, int16_t *fr_sample, const int buffLen);
6667

6768
#endif /* ML_SYNTH_INLINE_DECLARATION */
6869

@@ -146,11 +147,13 @@ union sampleTUNT
146147
//#define I2S_NODAC
147148

148149

149-
#ifndef DUAL_CODEC_ENABLED
150-
const i2s_port_t i2s_port_number[] = {I2S_NUM_0};
151-
#else
152-
const i2s_port_t i2s_port_number[] = {I2S_NUM_0, I2S_NUM_1};
150+
const i2s_port_t i2s_port_number[] =
151+
{
152+
I2S_NUM_0,
153+
#ifdef DUAL_CODEC_ENABLED
154+
I2S_NUM_1,
153155
#endif
156+
};
154157

155158
/*
156159
* please refer to https://www.hackster.io/janost/audio-hacking-on-the-esp8266-fa9464#toc-a-simple-909-drum-synth-0
@@ -195,7 +198,7 @@ bool i2s_write_sample_24ch2(uint8_t *sample)
195198

196199
#endif
197200

198-
bool i2s_write_stereo_samples(const float *fl_sample, const float *fr_sample, uint8_t stream_id)
201+
bool i2s_write_stereo_samples(uint8_t stream_id, const float *fl_sample, const float *fr_sample)
199202
{
200203
static union sampleTUNT sampleDataU;
201204

@@ -221,11 +224,11 @@ bool i2s_write_stereo_samples(const float *fl_sample, const float *fr_sample, ui
221224

222225
bool i2s_write_stereo_samples(const float *fl_sample, const float *fr_sample)
223226
{
224-
return i2s_write_stereo_samples(fl_sample, fr_sample, 0);
227+
return i2s_write_stereo_samples(0, fl_sample, fr_sample);
225228
};
226229

227230
#ifdef SAMPLE_SIZE_16BIT
228-
bool i2s_write_stereo_samples_i16(const int16_t *fl_sample, const int16_t *fr_sample, uint8_t stream_id)
231+
bool i2s_write_stereo_samples_i16(uint8_t stream_id, const int16_t *fl_sample, const int16_t *fr_sample)
229232
{
230233
size_t bytes_written = 0;
231234

@@ -252,15 +255,15 @@ bool i2s_write_stereo_samples_i16(const int16_t *fl_sample, const int16_t *fr_sa
252255

253256
bool i2s_write_stereo_samples_i16(const int16_t *fl_sample, const int16_t *fr_sample)
254257
{
255-
return i2s_write_stereo_samples_i16(fl_sample, fr_sample, 0);
258+
return i2s_write_stereo_samples_i16(0, fl_sample, fr_sample);
256259
}
257260
#endif
258261

259262
#ifdef SAMPLE_SIZE_16BIT
260-
bool i2s_write_stereo_samples_i16(const int16_t *fl_sample, const int16_t *fr_sample, const int buffLen, uint8_t stream_id)
263+
bool i2s_write_stereo_samples_i16(uint8_t stream_id, const int16_t *fl_sample, const int16_t *fr_sample, const int buffLen)
261264
{
262265
size_t bytes_written = 0;
263-
266+
i2s_port_t target_port = i2s_port_number[stream_id];
264267
static union sampleTUNT sampleDataU[SAMPLE_BUFFER_SIZE];
265268

266269
#ifdef OUTPUT_SAW_TEST
@@ -280,7 +283,7 @@ bool i2s_write_stereo_samples_i16(const int16_t *fl_sample, const int16_t *fr_sa
280283
#ifdef CYCLE_MODULE_ENABLED
281284
calcCycleCountPre();
282285
#endif
283-
i2s_write(i2s_port_number[stream_id], (const char *)&sampleDataU[0].sample, 2 * BYTES_PER_SAMPLE * buffLen, &bytes_written, portMAX_DELAY);
286+
i2s_write(target_port, (const char *)&sampleDataU[0].sample, 2 * BYTES_PER_SAMPLE * buffLen, &bytes_written, portMAX_DELAY);
284287
#ifdef CYCLE_MODULE_ENABLED
285288
calcCycleCount();
286289
#endif
@@ -297,12 +300,12 @@ bool i2s_write_stereo_samples_i16(const int16_t *fl_sample, const int16_t *fr_sa
297300

298301
bool i2s_write_stereo_samples_i16(const int16_t *fl_sample, const int16_t *fr_sample, const int buffLen)
299302
{
300-
return i2s_write_stereo_samples_i16(fl_sample, fr_sample, buffLen, 0);
303+
return i2s_write_stereo_samples_i16(0, fl_sample, fr_sample, buffLen);
301304
}
302305
#endif
303306

304307
#ifdef SAMPLE_BUFFER_SIZE
305-
bool i2s_write_stereo_samples_buff(const float *fl_sample, const float *fr_sample, const int buffLen, uint8_t stream_id)
308+
bool i2s_write_stereo_samples_buff(uint8_t stream_id, const float *fl_sample, const float *fr_sample, const int buffLen)
306309
{
307310
static union sampleTUNT sampleDataU[SAMPLE_BUFFER_SIZE];
308311

@@ -353,8 +356,8 @@ bool i2s_write_stereo_samples_buff(const float *fl_sample, const float *fr_sampl
353356
#ifdef OUTPUT_SAW_TEST
354357
for (int n = 0; n < buffLen; n++)
355358
{
356-
sampleDataU[n].ch[1] = sampleDataI16SawTest[0 + stream_id * 2][n];
357-
sampleDataU[n].ch[0] = sampleDataI16SawTest[1 + stream_id * 2][n];
359+
sampleDataU[n].ch[1] = sampleDataI16SawTest[n];
360+
sampleDataU[n].ch[0] = sampleDataI16SawTest[n];
358361
}
359362
#endif
360363

@@ -380,11 +383,11 @@ bool i2s_write_stereo_samples_buff(const float *fl_sample, const float *fr_sampl
380383

381384
bool i2s_write_stereo_samples_buff(const float *fl_sample, const float *fr_sample, const int buffLen)
382385
{
383-
return i2s_write_stereo_samples_buff(fl_sample, fr_sample, buffLen, 0);
386+
return i2s_write_stereo_samples_buff(0, fl_sample, fr_sample, buffLen);
384387
}
385388
#endif /* #ifdef SAMPLE_BUFFER_SIZE */
386389

387-
void i2s_read_stereo_samples(float *fl_sample, float *fr_sample, uint8_t stream_id)
390+
void i2s_read_stereo_samples(uint8_t stream_id, float *fl_sample, float *fr_sample)
388391
{
389392
static size_t bytes_read = 0;
390393

@@ -402,11 +405,11 @@ void i2s_read_stereo_samples(float *fl_sample, float *fr_sample, uint8_t stream_
402405

403406
void i2s_read_stereo_samples(float *fl_sample, float *fr_sample)
404407
{
405-
i2s_read_stereo_samples(fl_sample, fr_sample, 0);
408+
i2s_read_stereo_samples(0, fl_sample, fr_sample);
406409
}
407410

408411
#ifdef SAMPLE_BUFFER_SIZE
409-
void i2s_read_stereo_samples_buff(int16_t *fl_sample, int16_t *fr_sample, const int buffLen, uint8_t stream_id)
412+
void i2s_read_stereo_samples_buff(uint8_t stream_id, int16_t *fl_sample, int16_t *fr_sample, const int buffLen)
410413
{
411414
#ifdef I2S_DIN_PIN
412415
static size_t bytes_read = 0;
@@ -429,10 +432,10 @@ void i2s_read_stereo_samples_buff(int16_t *fl_sample, int16_t *fr_sample, const
429432

430433
void i2s_read_stereo_samples_buff(int16_t *fl_sample, int16_t *fr_sample, const int buffLen)
431434
{
432-
i2s_read_stereo_samples_buff(fl_sample, fr_sample, buffLen, 0);
435+
i2s_read_stereo_samples_buff(0, fl_sample, fr_sample, buffLen);
433436
}
434437

435-
void i2s_read_stereo_samples_buff(float *fl_sample, float *fr_sample, const int buffLen, uint8_t stream_id)
438+
void i2s_read_stereo_samples_buff(uint8_t stream_id, float *fl_sample, float *fr_sample, const int buffLen)
436439
{
437440
#ifdef I2S_DIN_PIN
438441
static size_t bytes_read = 0;
@@ -457,7 +460,7 @@ void i2s_read_stereo_samples_buff(float *fl_sample, float *fr_sample, const int
457460

458461
void i2s_read_stereo_samples_buff(float *fl_sample, float *fr_sample, const int buffLen)
459462
{
460-
i2s_read_stereo_samples_buff(fl_sample, fr_sample, buffLen, 0);
463+
i2s_read_stereo_samples_buff(0, fl_sample, fr_sample, buffLen);
461464
}
462465

463466
#endif /* #ifdef SAMPLE_BUFFER_SIZE */
@@ -684,43 +687,8 @@ i2s_pin_config_t pins[] =
684687
},
685688
#endif /* (defined I2S_BCLK_PIN_SECONDARY) && (defined I2S_WCLK_PIN_SECONDARY) && (defined I2S_DOUT_PIN_SECONDARY) */
686689
#endif /* DUAL_CODEC_ENABLED */
687-
688-
#endif /* (defined I2S_BCLK_PIN) && (defined I2S_WCLK_PIN) && (defined I2S_DOUT_PIN) */
689-
690-
#ifdef DUAL_CODEC_ENABLED
691-
#if (defined I2S_DOUT_PIN_SECONDARY) || (defined I2S_DIN_PIN_SECONDARY)
692-
/* Secondary I2S pin configuration for dual codec setup */
693-
694-
{
695-
#ifdef I2S_MCLK_PIN_SECONDARY
696-
.mck_io_num = I2S_MCLK_PIN_SECONDARY,
697-
#else
698-
.mck_io_num = I2S_PIN_NO_CHANGE,
699-
#endif
700-
#ifdef I2S_BCLK_PIN_SECONDARY
701-
.bck_io_num = I2S_BCLK_PIN_SECONDARY,
702-
#else
703-
.bck_io_num = I2S_PIN_NO_CHANGE,
704-
#endif
705-
#ifdef I2S_WCLK_PIN_SECONDARY
706-
.ws_io_num = I2S_WCLK_PIN_SECONDARY,
707-
#else
708-
.ws_io_num = I2S_PIN_NO_CHANGE,
709-
#endif
710-
#ifdef I2S_DOUT_PIN_SECONDARY
711-
.data_out_num = I2S_DOUT_PIN_SECONDARY,
712-
#else
713-
.data_out_num = I2S_PIN_NO_CHANGE,
714-
#endif
715-
#ifdef I2S_DIN_PIN_SECONDARY
716-
.data_in_num = I2S_DIN_PIN_SECONDARY,
717-
#else
718-
.data_in_num = I2S_PIN_NO_CHANGE,
719-
#endif
720-
},
721-
#endif /* (defined I2S_BCLK_PIN_SECONDARY) && (defined I2S_WCLK_PIN_SECONDARY) && (defined I2S_DOUT_PIN_SECONDARY) */
722-
#endif /* DUAL_CODEC_ENABLED */
723690
};
691+
#endif /* (defined I2S_BCLK_PIN) && (defined I2S_WCLK_PIN) && (defined I2S_DOUT_PIN) */
724692

725693
#endif
726694

0 commit comments

Comments
 (0)