@@ -103,9 +103,9 @@ static void i2s_fill_buffer(i2s_t *self) {
103
103
}
104
104
} else {
105
105
#define STACK_BUFFER_SIZE (64)
106
- size_t bytes_per_frame = self -> channel_count * self -> bytes_per_sample ;
107
- size_t framecount = MIN ( STACK_BUFFER_SIZE / bytes_per_frame , bytecount ) ;
108
- int16_t signed_samples [ STACK_BUFFER_SIZE / sizeof ( int16_t )] ;
106
+ const size_t bytes_per_output_frame = 4 ;
107
+ size_t bytes_per_input_frame = self -> channel_count * self -> bytes_per_sample ;
108
+ size_t framecount = MIN ( STACK_BUFFER_SIZE / bytes_per_output_frame , bytecount / bytes_per_input_frame ) ;
109
109
if (self -> samples_signed ) {
110
110
assert (self -> channel_count == 1 );
111
111
if (self -> bytes_per_sample == 1 ) {
@@ -129,9 +129,9 @@ static void i2s_fill_buffer(i2s_t *self) {
129
129
}
130
130
}
131
131
size_t expanded_bytes_written = 0 ;
132
- ESP_CALL_RAISE (i2s_write (self -> instance , signed_samples , 4 * framecount , & expanded_bytes_written , 0 ));
132
+ ESP_CALL_RAISE (i2s_write (self -> instance , signed_samples , bytes_per_output_frame * framecount , & expanded_bytes_written , 0 ));
133
133
assert (expanded_bytes_written % 4 == 0 );
134
- bytes_written = expanded_bytes_written / 4 * bytes_per_frame ;
134
+ bytes_written = expanded_bytes_written / bytes_per_output_frame * bytes_per_input_frame ;
135
135
}
136
136
self -> sample_data += bytes_written ;
137
137
// We have filled the DMA buffer
@@ -181,6 +181,10 @@ void port_i2s_allocate_init(i2s_t *self, bool left_justified) {
181
181
182
182
183
183
void port_i2s_play (i2s_t * self , mp_obj_t sample , bool loop ) {
184
+ if (common_hal_audiobusio_i2sout_get_playing (self )) {
185
+ common_hal_audiobusio_i2sout_stop (self );
186
+ }
187
+
184
188
self -> sample = sample ;
185
189
self -> bytes_per_sample = audiosample_bits_per_sample (sample ) / 8 ;
186
190
self -> channel_count = audiosample_channel_count (sample );
0 commit comments