Skip to content

Commit 8d9067d

Browse files
committed
codal_port/microbit_soundeffect: Apply param and steps when changing fx.
Fixes issue #155. Signed-off-by: Damien George <[email protected]>
1 parent 3074c81 commit 8d9067d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/codal_port/microbit_soundeffect.c

+25
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@
6969
#define SOUND_EFFECT_FX_VIBRATO (1)
7070
#define SOUND_EFFECT_FX_WARBLE (3)
7171

72+
// These default fx values are the same as used by MakeCode.
73+
#define SOUND_EFFECT_FX_VIBRATO_DEFAULT_PARAM (2)
74+
#define SOUND_EFFECT_FX_TREMOLO_DEFAULT_PARAM (3)
75+
#define SOUND_EFFECT_FX_WARBLE_DEFAULT_PARAM (2)
76+
#define SOUND_EFFECT_FX_VIBRATO_DEFAULT_STEPS (512)
77+
#define SOUND_EFFECT_FX_TREMOLO_DEFAULT_STEPS (900)
78+
#define SOUND_EFFECT_FX_WARBLE_DEFAULT_STEPS (700)
79+
7280
#define SOUND_EFFECT_DEFAULT_FREQ_START (500)
7381
#define SOUND_EFFECT_DEFAULT_FREQ_END (2500)
7482
#define SOUND_EFFECT_DEFAULT_DURATION (500)
@@ -116,6 +124,18 @@ STATIC const soundeffect_attr_t soundeffect_attr_table[] = {
116124
{ MP_QSTR_shape, SOUND_EXPR_SHAPE_OFFSET, SOUND_EXPR_SHAPE_LENGTH },
117125
};
118126

127+
static const uint8_t fx_default_param[] = {
128+
[SOUND_EFFECT_FX_VIBRATO] = SOUND_EFFECT_FX_VIBRATO_DEFAULT_PARAM,
129+
[SOUND_EFFECT_FX_TREMOLO] = SOUND_EFFECT_FX_TREMOLO_DEFAULT_PARAM,
130+
[SOUND_EFFECT_FX_WARBLE] = SOUND_EFFECT_FX_WARBLE_DEFAULT_PARAM,
131+
};
132+
133+
static const uint16_t fx_default_steps[] = {
134+
[SOUND_EFFECT_FX_VIBRATO] = SOUND_EFFECT_FX_VIBRATO_DEFAULT_STEPS,
135+
[SOUND_EFFECT_FX_TREMOLO] = SOUND_EFFECT_FX_TREMOLO_DEFAULT_STEPS,
136+
[SOUND_EFFECT_FX_WARBLE] = SOUND_EFFECT_FX_WARBLE_DEFAULT_STEPS,
137+
};
138+
119139
const char *microbit_soundeffect_get_sound_expr_data(mp_obj_t self_in) {
120140
const microbit_soundeffect_obj_t *self = MP_OBJ_TO_PTR(self_in);
121141
return &self->sound_expr[0];
@@ -273,6 +293,11 @@ STATIC void microbit_soundeffect_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des
273293
if (self->is_mutable) {
274294
unsigned int value = mp_obj_get_int(dest[1]);
275295
sound_expr_encode(self, soundeffect_attr->offset, soundeffect_attr->length, value);
296+
if (soundeffect_attr->offset == SOUND_EXPR_FX_CHOICE_OFFSET) {
297+
// Changing the fx choice, so also update the fx parameters for that choice.
298+
sound_expr_encode(self, SOUND_EXPR_FX_PARAM_OFFSET, SOUND_EXPR_FX_PARAM_LENGTH, fx_default_param[value]);
299+
sound_expr_encode(self, SOUND_EXPR_FX_STEPS_OFFSET, SOUND_EXPR_FX_STEPS_LENGTH, fx_default_steps[value]);
300+
}
276301
dest[0] = MP_OBJ_NULL; // Indicate store succeeded.
277302
}
278303
}

0 commit comments

Comments
 (0)