Skip to content

Commit 61aaf2a

Browse files
committed
codal_port/modaudio: Allow AudioFrame to be arbitrary length.
No longer needs to be a multiple of 32. Signed-off-by: Damien George <damien@micropython.org>
1 parent 80cfac7 commit 61aaf2a

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/codal_port/modaudio.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,22 @@ static void audio_data_fetcher(void) {
125125
}
126126

127127
const uint8_t *src = &audio_source_frame->data[audio_raw_offset];
128-
audio_raw_offset += AUDIO_CHUNK_SIZE;
128+
size_t src_len = MIN(audio_source_frame->used_size - audio_raw_offset, AUDIO_CHUNK_SIZE);
129+
audio_raw_offset += src_len;
129130

130131
uint8_t *dest = &audio_output_buffer[0];
131132
uint32_t sound_level = 0;
132-
for (int i = 0; i < AUDIO_CHUNK_SIZE; ++i) {
133+
134+
for (int i = 0; i < src_len; ++i) {
133135
// Copy sample to the buffer.
134136
*dest++ = src[i];
135137
// Compute the sound level.
136138
sound_level += (src[i] - 128) * (src[i] - 128);
137139
}
140+
141+
// Fill any remaining audio_output_buffer bytes with silence.
142+
memset(dest, 128, AUDIO_CHUNK_SIZE - src_len);
143+
138144
audio_current_sound_level = sound_level / AUDIO_CHUNK_SIZE;
139145

140146
audio_buffer_ready();
@@ -329,8 +335,6 @@ static mp_obj_t microbit_audio_frame_new(const mp_obj_type_t *type_in, size_t n_
329335
mp_raise_ValueError(MP_ERROR_TEXT("size out of bounds"));
330336
} else {
331337
size = args[ARG_duration].u_int * rate / 1000;
332-
// Round up the size to the nearest AUDIO_CHUNK_SIZE.
333-
size = (size + AUDIO_CHUNK_SIZE - 1) & ~(AUDIO_CHUNK_SIZE - 1);
334338
}
335339

336340
return microbit_audio_frame_make_new(size, rate);

0 commit comments

Comments
 (0)