Skip to content

Add end() method to MixerVoice to allow samples to finish playing bef… #10309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
14 changes: 14 additions & 0 deletions shared-bindings/audiomixer/MixerVoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ 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);

//| def end(self) -> None:
//| """ Sets looping to False if sample is playing. This allows the looped
//| sample to complete its current playback and end further looping """
//| ...
//|
static mp_obj_t audiomixer_mixervoice_obj_end(mp_obj_t self_in) {
audiomixer_mixervoice_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_audiomixer_mixervoice_end(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(audiomixer_mixervoice_end_obj, audiomixer_mixervoice_obj_end);


//| level: synthio.BlockInput
//| """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.
Expand Down Expand Up @@ -140,6 +153,7 @@ static const mp_rom_map_elem_t audiomixer_mixervoice_locals_dict_table[] = {
// Methods
{ MP_ROM_QSTR(MP_QSTR_play), MP_ROM_PTR(&audiomixer_mixervoice_play_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&audiomixer_mixervoice_stop_obj) },
{ MP_ROM_QSTR(MP_QSTR_end), MP_ROM_PTR(&audiomixer_mixervoice_end_obj) },

// Properties
{ MP_ROM_QSTR(MP_QSTR_playing), MP_ROM_PTR(&audiomixer_mixervoice_playing_obj) },
Expand Down
1 change: 1 addition & 0 deletions shared-bindings/audiomixer/MixerVoice.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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);
void common_hal_audiomixer_mixervoice_end(audiomixer_mixervoice_obj_t *self);
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);

Expand Down
6 changes: 6 additions & 0 deletions shared-module/audiomixer/MixerVoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ bool common_hal_audiomixer_mixervoice_get_playing(audiomixer_mixervoice_obj_t *s
void common_hal_audiomixer_mixervoice_stop(audiomixer_mixervoice_obj_t *self) {
self->sample = NULL;
}

void common_hal_audiomixer_mixervoice_end(audiomixer_mixervoice_obj_t *self) {
if (self->sample != NULL) {
self->loop = false;
}
}
Loading