77 * @Dependent Library:
88 * M5Unified@^0.2.5: https://github.com/m5stack/M5Unified
99 * Module Audio: https://github.com/m5stack/M5Module-Audio
10- *
10+ *
1111 * Please rollback these libraries if you encounter I2S initialization issues.
1212 * Board Manager M5Stack@2.0.7;
1313 * M5Unified@0.1.17;
2020AudioI2c device;
2121
2222#define USE_NEW_I2S_API 1
23- #define RECORD_SIZE (44100 * 2 * 2 * 2 )
23+ #define RECORD_SIZE (44100 * 2 * 2 * 2 )
2424
2525#if USE_NEW_I2S_API
26- #include < ESP_I2S.h>
27- I2SClass I2S ;
26+ #include < ESP_I2S.h>
27+ I2SClass I2S ;
2828#else
29- #include " driver/i2s.h"
29+ #include " driver/i2s.h"
3030#endif
3131
3232#define SYS_I2C_SDA_PIN 12
@@ -46,7 +46,7 @@ static uint8_t *buffer = nullptr;
4646uint16_t rxbuf[1024 ], txbuf[1024 ];
4747size_t readsize = 0 ;
4848byte error, address;
49- const uint32_t color[] = {0xFF0000 , 0xFF0000 , 0xFF0000 , 0x00FF00 , 0xFFFF00 , 0xFFFFFF , 0xFFFFFF , 0xFFFFFF , 0xFFFFFF };
49+ const uint32_t color[] = {0xFF0000 , 0xFF0000 , 0xFF0000 , 0x00FF00 , 0xFFFF00 , 0xFFFFFF , 0xFFFFFF , 0xFFFFFF , 0xFFFFFF };
5050
5151void setup ()
5252{
@@ -93,36 +93,28 @@ void setup()
9393 }
9494#if USE_NEW_I2S_API
9595 uint32_t sample_rate = 44100 ;
96- uint8_t i2s_bck = SYS_I2S_SCLK_PIN ;
97- uint8_t i2s_ws = SYS_I2S_LRCK_PIN ;
98- uint8_t i2s_do = SYS_I2S_DOUT_PIN ;
99- uint8_t i2s_di = SYS_I2S_DIN_PIN ;
100- uint8_t i2s_mc = SYS_I2S_MCLK_PIN ;
101- I2S .setPins (i2s_bck, i2s_ws, i2s_do, i2s_di, i2s_mc); // SCK, WS, SDOUT, SDIN, MCLK
96+ uint8_t i2s_bck = SYS_I2S_SCLK_PIN ;
97+ uint8_t i2s_ws = SYS_I2S_LRCK_PIN ;
98+ uint8_t i2s_do = SYS_I2S_DOUT_PIN ;
99+ uint8_t i2s_di = SYS_I2S_DIN_PIN ;
100+ uint8_t i2s_mc = SYS_I2S_MCLK_PIN ;
101+ I2S .setPins (i2s_bck, i2s_ws, i2s_do, i2s_di, i2s_mc); // SCK, WS, SDOUT, SDIN, MCLK
102102 I2S .begin (I2S_MODE_STD , 44100 , I2S_DATA_BIT_WIDTH_16BIT , I2S_SLOT_MODE_STEREO );
103- I2S .configureRX (
104- 44100 ,
105- I2S_DATA_BIT_WIDTH_16BIT ,
106- I2S_SLOT_MODE_STEREO
107- );
108-
109- I2S .configureTX (
110- 44100 ,
111- I2S_DATA_BIT_WIDTH_16BIT ,
112- I2S_SLOT_MODE_STEREO
113- );
103+ I2S .configureRX (44100 , I2S_DATA_BIT_WIDTH_16BIT , I2S_SLOT_MODE_STEREO );
104+
105+ I2S .configureTX (44100 , I2S_DATA_BIT_WIDTH_16BIT , I2S_SLOT_MODE_STEREO );
114106#else
115107 i2s_config_t i2s_config = {.mode = (i2s_mode_t )(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX ),
116- .sample_rate = 44100 ,
117- .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT ,
118- .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT ,
119- .communication_format = I2S_COMM_FORMAT_STAND_I2S ,
120- .intr_alloc_flags = 0 ,
121- .dma_buf_count = 8 ,
122- .dma_buf_len = 512 ,
123- .use_apll = false ,
124- .tx_desc_auto_clear = true ,
125- .fixed_mclk = 0 };
108+ .sample_rate = 44100 ,
109+ .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT ,
110+ .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT ,
111+ .communication_format = I2S_COMM_FORMAT_STAND_I2S ,
112+ .intr_alloc_flags = 0 ,
113+ .dma_buf_count = 8 ,
114+ .dma_buf_len = 512 ,
115+ .use_apll = false ,
116+ .tx_desc_auto_clear = true ,
117+ .fixed_mclk = 0 };
126118
127119 i2s_pin_config_t pin_config = {
128120 .mck_io_num = SYS_I2S_MCLK_PIN ,
@@ -146,7 +138,7 @@ I2S.configureTX(
146138 Serial.println (" Failed to allocate memory :(" );
147139 delay (1000 );
148140 }
149- }
141+ }
150142}
151143void loop ()
152144{
@@ -156,33 +148,34 @@ void loop()
156148
157149#if USE_NEW_I2S_API
158150 Serial.println (" Start recording" );
159- bytes_read = I2S .readBytes ((char *)buffer, bytes_to_record);
151+ bytes_read = I2S .readBytes ((char *)buffer, bytes_to_record);
160152 if (bytes_read < 0 ) {
161153 ESP_LOGI (TAG , " Recording failed during I2S read" );
162154 }
163-
155+
164156#else
165157 esp_err_t err = i2s_read (I2S_NUM_0 , &rxbuf[0 ], 1024 * 96 , &readsize, 1000 );
166-
158+
167159 if (err != ESP_OK ) {
168160 ESP_LOGI (TAG , " Recording failed during I2S read" );
169- }else ESP_LOGI (TAG , " Recording" );
161+ } else
162+ ESP_LOGI (TAG , " Recording" );
170163#endif
171164 delay (100 );
172165 size_t samples = bytes_read / sizeof (int16_t );
173- int16_t * pcm = (int16_t *)buffer;
166+ int16_t * pcm = (int16_t *)buffer;
174167
175168 int16_t peak = 0 ;
176169
177- for (size_t i = 0 ; i < samples; i++) {
178- int16_t v = pcm[i];
179- if (v < 0 ) v = -v;
180- if (v > peak) peak = v;
181- }
170+ for (size_t i = 0 ; i < samples; i++) {
171+ int16_t v = pcm[i];
172+ if (v < 0 ) v = -v;
173+ if (v > peak) peak = v;
174+ }
182175
183- float level = (float )peak / 32767 .0f ;
176+ float level = (float )peak / 32767 .0f ;
184177
185- Serial.printf (" Level: %.3f\n " , level);
178+ Serial.printf (" Level: %.3f\n " , level);
186179 for (int i = 0 ; i < 1024 ; i++) {
187180 // direct transfer too txbuff
188181 txbuf[i] = rxbuf[i];
@@ -191,14 +184,14 @@ Serial.printf("Level: %.3f\n", level);
191184 // play received buffer
192185 size_t bytes_written = 0 ;
193186#if USE_NEW_I2S_API
194- Serial.println (" Start playing" );
187+ Serial.println (" Start playing" );
195188 bytes_written = I2S .write (buffer, RECORD_SIZE );
196189 if (bytes_written < 0 ) {
197190 ESP_LOGI (TAG , " Playback failed" );
198191 }
199-
192+
200193#else
201- err = i2s_write (I2S_NUM_0 , &txbuf[0 ], 1024 * 96 , &readsize, 1000 );
194+ err = i2s_write (I2S_NUM_0 , &txbuf[0 ], 1024 * 96 , &readsize, 1000 );
202195 if (err != ESP_OK ) {
203196 ESP_LOGI (TAG , " Playback failed during I2S write" );
204197 }
0 commit comments