Skip to content

Commit d72c0f8

Browse files
committed
codal_port/modaudio: Remove buffer expansion.
Playing generated samples (eg from v1 code) now sounds better without buffer expansion. Signed-off-by: Damien George <damien@micropython.org>
1 parent b0ab2cb commit d72c0f8

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

src/codal_port/modaudio.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,14 @@
3737
#define LOG_AUDIO_CHUNK_SIZE (5)
3838
#define AUDIO_CHUNK_SIZE (1 << LOG_AUDIO_CHUNK_SIZE)
3939
#define DEFAULT_SAMPLE_RATE (7812)
40-
#define BUFFER_EXPANSION (4) // smooth out the samples via linear interpolation
41-
#define OUT_CHUNK_SIZE (BUFFER_EXPANSION * AUDIO_CHUNK_SIZE)
4240

4341
typedef enum {
4442
AUDIO_OUTPUT_STATE_IDLE,
4543
AUDIO_OUTPUT_STATE_DATA_READY,
4644
AUDIO_OUTPUT_STATE_DATA_WRITTEN,
4745
} audio_output_state_t;
4846

49-
static uint8_t audio_output_buffer[OUT_CHUNK_SIZE];
47+
static uint8_t audio_output_buffer[AUDIO_CHUNK_SIZE];
5048
static volatile audio_output_state_t audio_output_state;
5149
static volatile bool audio_fetcher_scheduled;
5250
static size_t audio_raw_offset;
@@ -127,16 +125,9 @@ STATIC void audio_data_fetcher(void) {
127125
audio_raw_offset += AUDIO_CHUNK_SIZE;
128126

129127
uint8_t *dest = &audio_output_buffer[0];
130-
uint32_t last = dest[OUT_CHUNK_SIZE - 1];
131128
for (int i = 0; i < AUDIO_CHUNK_SIZE; ++i) {
132-
uint32_t cur = src[i];
133-
for (int j = 0; j < BUFFER_EXPANSION; ++j) {
134-
// Get next sample with linear interpolation.
135-
uint32_t sample = ((BUFFER_EXPANSION - 1 - j) * last + (j + 1) * cur) / BUFFER_EXPANSION;
136-
// Write sample to the buffer.
137-
*dest++ = sample;
138-
}
139-
last = cur;
129+
// Copy sample to the buffer.
130+
*dest++ = src[i];
140131
}
141132

142133
audio_buffer_ready();
@@ -151,7 +142,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(audio_data_fetcher_wrapper_obj, audio_data_fetc
151142
void microbit_hal_audio_raw_ready_callback(void) {
152143
if (audio_output_state == AUDIO_OUTPUT_STATE_DATA_READY) {
153144
// there is data ready to send out to the audio pipeline, so send it
154-
microbit_hal_audio_raw_write_data(&audio_output_buffer[0], OUT_CHUNK_SIZE);
145+
microbit_hal_audio_raw_write_data(&audio_output_buffer[0], AUDIO_CHUNK_SIZE);
155146
audio_output_state = AUDIO_OUTPUT_STATE_DATA_WRITTEN;
156147
} else {
157148
// no data ready, need to call this function later when data is ready
@@ -166,7 +157,7 @@ void microbit_hal_audio_raw_ready_callback(void) {
166157
static void audio_init(uint32_t sample_rate) {
167158
audio_fetcher_scheduled = false;
168159
audio_output_state = AUDIO_OUTPUT_STATE_IDLE;
169-
microbit_hal_audio_raw_init(BUFFER_EXPANSION * sample_rate);
160+
microbit_hal_audio_raw_init(sample_rate);
170161
}
171162

172163
void microbit_audio_play_source(mp_obj_t src, mp_obj_t pin_select, bool wait, uint32_t sample_rate) {

0 commit comments

Comments
 (0)