Skip to content

Commit

Permalink
codal_port/microbit_soundeffect: Apply param and steps when changing fx.
Browse files Browse the repository at this point in the history
Fixes issue #155.

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Mar 25, 2024
1 parent 3074c81 commit 8d9067d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/codal_port/microbit_soundeffect.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@
#define SOUND_EFFECT_FX_VIBRATO (1)
#define SOUND_EFFECT_FX_WARBLE (3)

// These default fx values are the same as used by MakeCode.
#define SOUND_EFFECT_FX_VIBRATO_DEFAULT_PARAM (2)
#define SOUND_EFFECT_FX_TREMOLO_DEFAULT_PARAM (3)
#define SOUND_EFFECT_FX_WARBLE_DEFAULT_PARAM (2)
#define SOUND_EFFECT_FX_VIBRATO_DEFAULT_STEPS (512)
#define SOUND_EFFECT_FX_TREMOLO_DEFAULT_STEPS (900)
#define SOUND_EFFECT_FX_WARBLE_DEFAULT_STEPS (700)

#define SOUND_EFFECT_DEFAULT_FREQ_START (500)
#define SOUND_EFFECT_DEFAULT_FREQ_END (2500)
#define SOUND_EFFECT_DEFAULT_DURATION (500)
Expand Down Expand Up @@ -116,6 +124,18 @@ STATIC const soundeffect_attr_t soundeffect_attr_table[] = {
{ MP_QSTR_shape, SOUND_EXPR_SHAPE_OFFSET, SOUND_EXPR_SHAPE_LENGTH },
};

static const uint8_t fx_default_param[] = {
[SOUND_EFFECT_FX_VIBRATO] = SOUND_EFFECT_FX_VIBRATO_DEFAULT_PARAM,
[SOUND_EFFECT_FX_TREMOLO] = SOUND_EFFECT_FX_TREMOLO_DEFAULT_PARAM,
[SOUND_EFFECT_FX_WARBLE] = SOUND_EFFECT_FX_WARBLE_DEFAULT_PARAM,
};

static const uint16_t fx_default_steps[] = {
[SOUND_EFFECT_FX_VIBRATO] = SOUND_EFFECT_FX_VIBRATO_DEFAULT_STEPS,
[SOUND_EFFECT_FX_TREMOLO] = SOUND_EFFECT_FX_TREMOLO_DEFAULT_STEPS,
[SOUND_EFFECT_FX_WARBLE] = SOUND_EFFECT_FX_WARBLE_DEFAULT_STEPS,
};

const char *microbit_soundeffect_get_sound_expr_data(mp_obj_t self_in) {
const microbit_soundeffect_obj_t *self = MP_OBJ_TO_PTR(self_in);
return &self->sound_expr[0];
Expand Down Expand Up @@ -273,6 +293,11 @@ STATIC void microbit_soundeffect_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des
if (self->is_mutable) {
unsigned int value = mp_obj_get_int(dest[1]);
sound_expr_encode(self, soundeffect_attr->offset, soundeffect_attr->length, value);
if (soundeffect_attr->offset == SOUND_EXPR_FX_CHOICE_OFFSET) {
// Changing the fx choice, so also update the fx parameters for that choice.
sound_expr_encode(self, SOUND_EXPR_FX_PARAM_OFFSET, SOUND_EXPR_FX_PARAM_LENGTH, fx_default_param[value]);
sound_expr_encode(self, SOUND_EXPR_FX_STEPS_OFFSET, SOUND_EXPR_FX_STEPS_LENGTH, fx_default_steps[value]);
}
dest[0] = MP_OBJ_NULL; // Indicate store succeeded.
}
}
Expand Down

0 comments on commit 8d9067d

Please sign in to comment.