Skip to content

Commit

Permalink
Revert to original functionality when synthio is not supported.
Browse files Browse the repository at this point in the history
  • Loading branch information
relic-se committed Dec 20, 2024
1 parent e37502c commit a5956ea
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
6 changes: 5 additions & 1 deletion shared-bindings/audiomixer/MixerVoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/util.h"
#if CIRCUITPY_SYNTHIO
#include "shared-module/synthio/block.h"
#endif

//| class MixerVoice:
//| """Voice objects used with Mixer
Expand Down Expand Up @@ -77,7 +79,9 @@ static mp_obj_t audiomixer_mixervoice_obj_stop(size_t n_args, const mp_obj_t *po
MP_DEFINE_CONST_FUN_OBJ_KW(audiomixer_mixervoice_stop_obj, 1, audiomixer_mixervoice_obj_stop);

//| level: synthio.BlockInput
//| """The volume level of a voice, as a floating point number between 0 and 1."""
//| """The volume level of a voice, as a floating point number between 0 and 1. If your board
//| does not support synthio, this property will only accept a float value.
//| """
static mp_obj_t audiomixer_mixervoice_obj_get_level(mp_obj_t self_in) {
return common_hal_audiomixer_mixervoice_get_level(self_in);
}
Expand Down
8 changes: 7 additions & 1 deletion shared-module/audiomixer/Mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,18 @@ static void mix_down_one_voice(audiomixer_mixer_obj_t *self,
}
}

uint32_t n = MIN(MIN(voice->buffer_length, length), SYNTHIO_MAX_DUR * self->channel_count);
uint32_t *src = voice->remaining_buffer;

#if CIRCUITPY_SYNTHIO
uint32_t n = MIN(MIN(voice->buffer_length, length), SYNTHIO_MAX_DUR * self->channel_count);

// Get the current level from the BlockInput. These may change at run time so you need to do bounds checking if required.
shared_bindings_synthio_lfo_tick(self->sample_rate);
uint16_t level = (uint16_t)(synthio_block_slot_get_limited(&voice->level, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0)) * (1 << 15));
#else
uint32_t n = MIN(voice->buffer_length, length);
uint16_t level = voice->level;
#endif

// First active voice gets copied over verbatim.
if (!voices_active) {
Expand Down
8 changes: 8 additions & 0 deletions shared-module/audiomixer/MixerVoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ void common_hal_audiomixer_mixervoice_set_parent(audiomixer_mixervoice_obj_t *se
}

mp_obj_t common_hal_audiomixer_mixervoice_get_level(audiomixer_mixervoice_obj_t *self) {
#if CIRCUITPY_SYNTHIO
return self->level.obj;
#else
return mp_obj_new_float((mp_float_t)self->level / (1 << 15));
#endif
}

void common_hal_audiomixer_mixervoice_set_level(audiomixer_mixervoice_obj_t *self, mp_obj_t arg) {
#if CIRCUITPY_SYNTHIO
synthio_block_assign_slot(arg, &self->level, MP_QSTR_level);
#else
self->level = (uint16_t)(mp_arg_validate_obj_float_range(arg, 0, 1, MP_QSTR_level) * (1 << 15));
#endif
}

bool common_hal_audiomixer_mixervoice_get_loop(audiomixer_mixervoice_obj_t *self) {
Expand Down
6 changes: 6 additions & 0 deletions shared-module/audiomixer/MixerVoice.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

#include "shared-module/audiomixer/__init__.h"
#include "shared-module/audiomixer/Mixer.h"
#if CIRCUITPY_SYNTHIO
#include "shared-module/synthio/block.h"
#endif

typedef struct {
mp_obj_base_t base;
Expand All @@ -19,5 +21,9 @@ typedef struct {
bool more_data;
uint32_t *remaining_buffer;
uint32_t buffer_length;
#if CIRCUITPY_SYNTHIO
synthio_block_slot_t level;
#else
uint16_t level;
#endif
} audiomixer_mixervoice_obj_t;

0 comments on commit a5956ea

Please sign in to comment.