add voice level getter

This commit is contained in:
sommersoft 2019-03-12 16:10:54 -05:00
parent 7aa0840b73
commit a498bcd94d
3 changed files with 22 additions and 1 deletions

View File

@ -113,6 +113,22 @@ STATIC mp_obj_t audioio_mixervoice_obj_stop(size_t n_args, const mp_obj_t *pos_a
}
MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixervoice_stop_obj, 1, audioio_mixervoice_obj_stop);
//| .. method:: get_gain(voice, gain)
//|
//| Get the gain of a voice.
//|
//| Returns as a floating point number between 0 and 1
//|
STATIC mp_obj_t audioio_mixervoice_obj_get_level(mp_obj_t self_in) {
#if !MICROPY_PY_BUILTINS_FLOAT
#error "floating point not supported"
#endif
return mp_obj_new_float(common_hal_audioio_mixervoice_get_level(self_in));
}
MP_DEFINE_CONST_FUN_OBJ_1(audioio_mixervoice_get_level_obj, audioio_mixervoice_obj_get_level);
//| .. method:: set_gain(voice, gain)
//|
//| Set the gain of a voice.
@ -146,7 +162,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixervoice_set_level_obj, 1, audioio_mixervoi
const mp_obj_property_t audioio_mixervoice_level_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&mp_const_none_obj,
.proxy = {(mp_obj_t)&audioio_mixervoice_get_level_obj,
(mp_obj_t)&audioio_mixervoice_set_level_obj,
(mp_obj_t)&mp_const_none_obj},
};

View File

@ -21,6 +21,7 @@ void common_hal_audioio_mixervoice_deinit(audioio_mixervoice_obj_t* self);
void common_hal_audioio_mixervoice_set_parent(audioio_mixervoice_obj_t* self, audioio_mixer_obj_t *parent);
void common_hal_audioio_mixervoice_play(audioio_mixervoice_obj_t* self, mp_obj_t sample, bool loop);
void common_hal_audioio_mixervoice_stop(audioio_mixervoice_obj_t* self);
float common_hal_audioio_mixervoice_get_level(audioio_mixervoice_obj_t* self);
void common_hal_audioio_mixervoice_set_level(audioio_mixervoice_obj_t* self, float gain);
bool common_hal_audioio_mixervoice_get_playing(audioio_mixervoice_obj_t* self);

View File

@ -18,6 +18,10 @@ void common_hal_audioio_mixervoice_set_parent(audioio_mixervoice_obj_t* self, au
self->parent = parent;
}
float common_hal_audioio_mixervoice_get_level(audioio_mixervoice_obj_t* self) {
return self->level;
}
void common_hal_audioio_mixervoice_set_level(audioio_mixervoice_obj_t* self, float level) {
self->level = level * ((1 << 15)-1);
}