From a498bcd94d7f869e2cb2e992babacb981de688c0 Mon Sep 17 00:00:00 2001 From: sommersoft Date: Tue, 12 Mar 2019 16:10:54 -0500 Subject: [PATCH] add voice level getter --- shared-bindings/audioio/MixerVoice.c | 18 +++++++++++++++++- shared-bindings/audioio/MixerVoice.h | 1 + shared-module/audioio/MixerVoice.c | 4 ++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/shared-bindings/audioio/MixerVoice.c b/shared-bindings/audioio/MixerVoice.c index 02f57d4195..cba82feb21 100644 --- a/shared-bindings/audioio/MixerVoice.c +++ b/shared-bindings/audioio/MixerVoice.c @@ -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}, }; diff --git a/shared-bindings/audioio/MixerVoice.h b/shared-bindings/audioio/MixerVoice.h index a67a5ec909..641a0faf25 100644 --- a/shared-bindings/audioio/MixerVoice.h +++ b/shared-bindings/audioio/MixerVoice.h @@ -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); diff --git a/shared-module/audioio/MixerVoice.c b/shared-module/audioio/MixerVoice.c index 024460af1a..3e8aaff897 100644 --- a/shared-module/audioio/MixerVoice.c +++ b/shared-module/audioio/MixerVoice.c @@ -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); }