Skip to content

Commit 3522264

Browse files
committed
esp32s2: i2s: fix accounting for "stretched" frames
1 parent 010a4e7 commit 3522264

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

ports/esp32s2/i2s_common.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ static void i2s_fill_buffer(i2s_t *self) {
103103
}
104104
} else {
105105
#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);
109109
if (self->samples_signed) {
110110
assert(self->channel_count == 1);
111111
if (self->bytes_per_sample == 1) {
@@ -129,9 +129,9 @@ static void i2s_fill_buffer(i2s_t *self) {
129129
}
130130
}
131131
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));
133133
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;
135135
}
136136
self->sample_data += bytes_written;
137137
// We have filled the DMA buffer
@@ -181,6 +181,10 @@ void port_i2s_allocate_init(i2s_t *self, bool left_justified) {
181181

182182

183183
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+
184188
self->sample = sample;
185189
self->bytes_per_sample = audiosample_bits_per_sample(sample) / 8;
186190
self->channel_count = audiosample_channel_count(sample);

0 commit comments

Comments
 (0)