@@ -80,19 +80,21 @@ void i2s_reset(void) {
80
80
}
81
81
}
82
82
83
+ #define I2S_WRITE_DELAY pdMS_TO_TICKS(1)
84
+
83
85
static void i2s_fill_buffer (i2s_t * self ) {
84
86
if (self -> instance < 0 || self -> instance >= I2S_NUM_MAX ) {
85
87
return ;
86
88
}
87
- #define STACK_BUFFER_SIZE (512 )
89
+ #define STACK_BUFFER_SIZE (4096 )
88
90
int16_t signed_samples [STACK_BUFFER_SIZE / sizeof (int16_t )];
89
91
90
92
if (!self -> playing || self -> paused || !self -> sample || self -> stopping ) {
91
93
memset (signed_samples , 0 , sizeof (signed_samples ));
92
94
93
95
size_t bytes_written = 0 ;
94
96
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 ));
96
98
} while (bytes_written != 0 );
97
99
return ;
98
100
}
@@ -120,9 +122,9 @@ static void i2s_fill_buffer(i2s_t *self) {
120
122
size_t bytecount = self -> sample_end - self -> sample_data ;
121
123
if (self -> samples_signed && self -> channel_count == 2 ) {
122
124
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 ));
124
126
} 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 ));
126
128
}
127
129
} else {
128
130
const size_t bytes_per_output_frame = 4 ;
@@ -151,7 +153,7 @@ static void i2s_fill_buffer(i2s_t *self) {
151
153
}
152
154
}
153
155
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 ));
155
157
assert (expanded_bytes_written % 4 == 0 );
156
158
bytes_written = expanded_bytes_written / bytes_per_output_frame * bytes_per_input_frame ;
157
159
}
@@ -188,8 +190,8 @@ void port_i2s_allocate_init(i2s_t *self, bool left_justified) {
188
190
.bits_per_sample = 16 ,
189
191
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT ,
190
192
.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
193
195
.use_apll = false,
194
196
};
195
197
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) {
223
225
224
226
audiosample_reset_buffer (self -> sample , false, 0 );
225
227
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
+ }
227
233
228
234
background_callback_add (& self -> callback , i2s_callback_fun , self );
229
235
}
0 commit comments