Skip to content

Commit 822b3c9

Browse files
committed
codal_port/modaudio: Support playing a tuple/list of SoundEffect's.
For simplicity of implementation, this just concatenates the tuple/list of SoundEffect's into a big string to be passed directly to CODAL. Signed-off-by: Damien George <[email protected]>
1 parent 9a5958b commit 822b3c9

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/codal_port/microbit_soundeffect.c

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
#include "modmicrobit.h"
2929
#include "modaudio.h"
3030

31-
#define SOUND_EXPR_TOTAL_LENGTH (72)
32-
3331
#define SOUND_EXPR_WAVE_OFFSET (0)
3432
#define SOUND_EXPR_WAVE_LENGTH (1)
3533
#define SOUND_EXPR_VOLUME_START_OFFSET (1)

src/codal_port/modaudio.c

+18
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,24 @@ void microbit_audio_play_source(mp_obj_t src, mp_obj_t pin_select, bool wait, ui
143143
sound_expr_data = sound->name;
144144
} else if (mp_obj_is_type(src, &microbit_soundeffect_type)) {
145145
sound_expr_data = microbit_soundeffect_get_sound_expr_data(src);
146+
} else if (mp_obj_is_type(src, &mp_type_tuple) || mp_obj_is_type(src, &mp_type_list)) {
147+
// A tuple/list passed in. Need to check if it contains SoundEffect instances.
148+
size_t len;
149+
mp_obj_t *items;
150+
mp_obj_get_array(src, &len, &items);
151+
if (len > 0 && mp_obj_is_type(items[0], &microbit_soundeffect_type)) {
152+
// A tuple/list of SoundEffect instances. Convert it to a long string of
153+
// sound expression data, each effect separated by a ",".
154+
char *data = m_new(char, len * (SOUND_EXPR_TOTAL_LENGTH + 1));
155+
sound_expr_data = data;
156+
for (size_t i = 0; i < len; ++i) {
157+
memcpy(data, microbit_soundeffect_get_sound_expr_data(items[i]), SOUND_EXPR_TOTAL_LENGTH);
158+
data += SOUND_EXPR_TOTAL_LENGTH;
159+
*data++ = ',';
160+
}
161+
// Replace last "," with a string null terminator.
162+
data[-1] = '\0';
163+
}
146164
}
147165

148166
if (sound_expr_data != NULL) {

src/codal_port/modaudio.h

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#define LOG_AUDIO_CHUNK_SIZE (5)
3333
#define AUDIO_CHUNK_SIZE (1 << LOG_AUDIO_CHUNK_SIZE)
3434

35+
#define SOUND_EXPR_TOTAL_LENGTH (72)
36+
3537
typedef struct _microbit_audio_frame_obj_t {
3638
mp_obj_base_t base;
3739
uint8_t data[AUDIO_CHUNK_SIZE];

0 commit comments

Comments
 (0)