Skip to content

Commit

Permalink
Implement level as BlockInput.
Browse files Browse the repository at this point in the history
  • Loading branch information
relic-se committed Dec 20, 2024
1 parent 5602618 commit 542c7c9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
8 changes: 4 additions & 4 deletions shared-bindings/audiomixer/MixerVoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/util.h"
#include "shared-module/synthio/block.h"

//| class MixerVoice:
//| """Voice objects used with Mixer
Expand Down Expand Up @@ -75,17 +76,16 @@ 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: float
//| level: synthio.BlockInput
//| """The volume level of a voice, as a floating point number between 0 and 1."""
static mp_obj_t audiomixer_mixervoice_obj_get_level(mp_obj_t self_in) {
return mp_obj_new_float(common_hal_audiomixer_mixervoice_get_level(self_in));
return common_hal_audiomixer_mixervoice_get_level(self_in);
}
MP_DEFINE_CONST_FUN_OBJ_1(audiomixer_mixervoice_get_level_obj, audiomixer_mixervoice_obj_get_level);

static mp_obj_t audiomixer_mixervoice_obj_set_level(mp_obj_t self_in, mp_obj_t level_in) {
audiomixer_mixervoice_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_float_t level = mp_arg_validate_obj_float_range(level_in, 0, 1, MP_QSTR_level);
common_hal_audiomixer_mixervoice_set_level(self, level);
common_hal_audiomixer_mixervoice_set_level(self, level_in);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(audiomixer_mixervoice_set_level_obj, audiomixer_mixervoice_obj_set_level);
Expand Down
4 changes: 2 additions & 2 deletions shared-bindings/audiomixer/MixerVoice.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ void common_hal_audiomixer_mixervoice_construct(audiomixer_mixervoice_obj_t *sel
void common_hal_audiomixer_mixervoice_set_parent(audiomixer_mixervoice_obj_t *self, audiomixer_mixer_obj_t *parent);
void common_hal_audiomixer_mixervoice_play(audiomixer_mixervoice_obj_t *self, mp_obj_t sample, bool loop);
void common_hal_audiomixer_mixervoice_stop(audiomixer_mixervoice_obj_t *self);
mp_float_t common_hal_audiomixer_mixervoice_get_level(audiomixer_mixervoice_obj_t *self);
void common_hal_audiomixer_mixervoice_set_level(audiomixer_mixervoice_obj_t *self, mp_float_t gain);
mp_obj_t common_hal_audiomixer_mixervoice_get_level(audiomixer_mixervoice_obj_t *self);
void common_hal_audiomixer_mixervoice_set_level(audiomixer_mixervoice_obj_t *self, mp_obj_t gain);

bool common_hal_audiomixer_mixervoice_get_playing(audiomixer_mixervoice_obj_t *self);

Expand Down
7 changes: 5 additions & 2 deletions shared-module/audiomixer/Mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,12 @@ static void mix_down_one_voice(audiomixer_mixer_obj_t *self,
}
}

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

// 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); //, n / self->channel_count); // Requires #9776
uint16_t level = (uint16_t)(synthio_block_slot_get_limited(&voice->level, MICROPY_FLOAT_CONST(0.0), MICROPY_FLOAT_CONST(1.0)) * (1 << 15));

// First active voice gets copied over verbatim.
if (!voices_active) {
Expand Down
10 changes: 5 additions & 5 deletions shared-module/audiomixer/MixerVoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@

void common_hal_audiomixer_mixervoice_construct(audiomixer_mixervoice_obj_t *self) {
self->sample = NULL;
self->level = 1 << 15;
common_hal_audiomixer_mixervoice_set_level(self, mp_obj_new_float(1.0));
}

void common_hal_audiomixer_mixervoice_set_parent(audiomixer_mixervoice_obj_t *self, audiomixer_mixer_obj_t *parent) {
self->parent = parent;
}

mp_float_t common_hal_audiomixer_mixervoice_get_level(audiomixer_mixervoice_obj_t *self) {
return (mp_float_t)self->level / (1 << 15);
mp_obj_t common_hal_audiomixer_mixervoice_get_level(audiomixer_mixervoice_obj_t *self) {
return self->level.obj;
}

void common_hal_audiomixer_mixervoice_set_level(audiomixer_mixervoice_obj_t *self, mp_float_t level) {
self->level = (uint16_t)(level * (1 << 15));
void common_hal_audiomixer_mixervoice_set_level(audiomixer_mixervoice_obj_t *self, mp_obj_t arg) {
synthio_block_assign_slot(arg, &self->level, MP_QSTR_level);
}

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

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

typedef struct {
mp_obj_base_t base;
Expand All @@ -18,5 +19,5 @@ typedef struct {
bool more_data;
uint32_t *remaining_buffer;
uint32_t buffer_length;
uint16_t level;
synthio_block_slot_t level;
} audiomixer_mixervoice_obj_t;

0 comments on commit 542c7c9

Please sign in to comment.