@@ -80,19 +80,21 @@ void i2s_reset(void) {
8080 }
8181}
8282
83+ #define I2S_WRITE_DELAY pdMS_TO_TICKS(1)
84+
8385static void i2s_fill_buffer (i2s_t * self ) {
8486 if (self -> instance < 0 || self -> instance >= I2S_NUM_MAX ) {
8587 return ;
8688 }
87- #define STACK_BUFFER_SIZE (512 )
89+ #define STACK_BUFFER_SIZE (4096 )
8890 int16_t signed_samples [STACK_BUFFER_SIZE / sizeof (int16_t )];
8991
9092 if (!self -> playing || self -> paused || !self -> sample || self -> stopping ) {
9193 memset (signed_samples , 0 , sizeof (signed_samples ));
9294
9395 size_t bytes_written = 0 ;
9496 do {
95- CHECK_ESP_RESULT (i2s_write (self -> instance , signed_samples , sizeof (signed_samples ), & bytes_written , 0 ));
97+ CHECK_ESP_RESULT (i2s_write (self -> instance , signed_samples , sizeof (signed_samples ), & bytes_written , I2S_WRITE_DELAY ));
9698 } while (bytes_written != 0 );
9799 return ;
98100 }
@@ -120,9 +122,9 @@ static void i2s_fill_buffer(i2s_t *self) {
120122 size_t bytecount = self -> sample_end - self -> sample_data ;
121123 if (self -> samples_signed && self -> channel_count == 2 ) {
122124 if (self -> bytes_per_sample == 2 ) {
123- CHECK_ESP_RESULT (i2s_write (self -> instance , self -> sample_data , bytecount , & bytes_written , 0 ));
125+ CHECK_ESP_RESULT (i2s_write (self -> instance , self -> sample_data , bytecount , & bytes_written , I2S_WRITE_DELAY ));
124126 } else {
125- CHECK_ESP_RESULT (i2s_write_expand (self -> instance , self -> sample_data , bytecount , 8 , 16 , & bytes_written , 0 ));
127+ CHECK_ESP_RESULT (i2s_write_expand (self -> instance , self -> sample_data , bytecount , 8 , 16 , & bytes_written , I2S_WRITE_DELAY ));
126128 }
127129 } else {
128130 const size_t bytes_per_output_frame = 4 ;
@@ -151,7 +153,7 @@ static void i2s_fill_buffer(i2s_t *self) {
151153 }
152154 }
153155 size_t expanded_bytes_written = 0 ;
154- CHECK_ESP_RESULT (i2s_write (self -> instance , signed_samples , bytes_per_output_frame * framecount , & expanded_bytes_written , 0 ));
156+ CHECK_ESP_RESULT (i2s_write (self -> instance , signed_samples , bytes_per_output_frame * framecount , & expanded_bytes_written , I2S_WRITE_DELAY ));
155157 assert (expanded_bytes_written % 4 == 0 );
156158 bytes_written = expanded_bytes_written / bytes_per_output_frame * bytes_per_input_frame ;
157159 }
@@ -188,8 +190,8 @@ void port_i2s_allocate_init(i2s_t *self, bool left_justified) {
188190 .bits_per_sample = 16 ,
189191 .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT ,
190192 .communication_format = left_justified ? I2S_COMM_FORMAT_STAND_I2S : I2S_COMM_FORMAT_STAND_I2S ,
191- .dma_buf_count = 2 ,
192- .dma_buf_len = 128 , // in _frames_, so 128 is 512 bytes per dma buf
193+ .dma_buf_count = 3 ,
194+ .dma_buf_len = 1024 , // in _frames_, so 1024 is 4096 bytes per dma buf
193195 .use_apll = false,
194196 };
195197 CHECK_ESP_RESULT (i2s_driver_install (self -> instance , & i2s_config , I2S_QUEUE_SIZE , & i2s_queues [self -> instance ]));
@@ -223,7 +225,11 @@ void port_i2s_play(i2s_t *self, mp_obj_t sample, bool loop) {
223225
224226 audiosample_reset_buffer (self -> sample , false, 0 );
225227
226- CHECK_ESP_RESULT (i2s_set_sample_rates (self -> instance , audiosample_sample_rate (sample )));
228+ uint32_t sample_rate = audiosample_sample_rate (sample );
229+ if (sample_rate != self -> i2s_config .sample_rate ) {
230+ CHECK_ESP_RESULT (i2s_set_sample_rates (self -> instance , audiosample_sample_rate (sample )));
231+ self -> i2s_config .sample_rate = sample_rate ;
232+ }
227233
228234 background_callback_add (& self -> callback , i2s_callback_fun , self );
229235}
0 commit comments