From 1bf6c588e7e1ea11c865c33f660ec126f752b1dc Mon Sep 17 00:00:00 2001 From: dean Date: Tue, 20 Nov 2018 14:14:22 -0500 Subject: DM: mixer voice stuff --- shared-module/audioio/Mixer.c | 58 ++------------------------------------ shared-module/audioio/Mixer.h | 1 - shared-module/audioio/MixerVoice.c | 42 +++++++++++++++++++++++++++ shared-module/audioio/MixerVoice.h | 2 ++ 4 files changed, 47 insertions(+), 56 deletions(-) (limited to 'shared-module') diff --git a/shared-module/audioio/Mixer.c b/shared-module/audioio/Mixer.c index 28815470d..ea53140ab 100644 --- a/shared-module/audioio/Mixer.c +++ b/shared-module/audioio/Mixer.c @@ -25,6 +25,7 @@ */ #include "shared-bindings/audioio/Mixer.h" +#include "shared-bindings/audioio/MixerVoice.h" #include @@ -58,13 +59,6 @@ void common_hal_audioio_mixer_construct(audioio_mixer_obj_t* self, self->channel_count = channel_count; self->sample_rate = sample_rate; self->voice_count = voice_count; - -#if 0 - for (uint8_t i = 0; i < self->voice_count; i++) { - self->voice[i].sample = NULL; - self->voice[i].gain = ((1<<15)-1); - } -#endif } void common_hal_audioio_mixer_deinit(audioio_mixer_obj_t* self) { @@ -80,56 +74,12 @@ uint32_t common_hal_audioio_mixer_get_sample_rate(audioio_mixer_obj_t* self) { return self->sample_rate; } -void common_hal_audioio_mixer_play(audioio_mixer_obj_t* self, mp_obj_t sample, uint8_t v, bool loop) { - if (v >= self->voice_count) { - mp_raise_ValueError(translate("Voice index too high")); - } - if (audiosample_sample_rate(sample) != self->sample_rate) { - mp_raise_ValueError(translate("The sample's sample rate does not match the mixer's")); - } - if (audiosample_channel_count(sample) != self->channel_count) { - mp_raise_ValueError(translate("The sample's channel count does not match the mixer's")); - } - if (audiosample_bits_per_sample(sample) != self->bits_per_sample) { - mp_raise_ValueError(translate("The sample's bits_per_sample does not match the mixer's")); - } - bool single_buffer; - bool samples_signed; - uint32_t max_buffer_length; - uint8_t spacing; - audiosample_get_buffer_structure(sample, false, &single_buffer, &samples_signed, - &max_buffer_length, &spacing); - if (samples_signed != self->samples_signed) { - mp_raise_ValueError(translate("The sample's signedness does not match the mixer's")); - } -#if 0 - audioio_mixervoice_obj_t* voice = &self->voice[v]; - voice->sample = sample; - voice->loop = loop; - - audiosample_reset_buffer(sample, false, 0); - audioio_get_buffer_result_t result = audiosample_get_buffer(sample, false, 0, (uint8_t**) &voice->remaining_buffer, &voice->buffer_length); - // Track length in terms of words. - voice->buffer_length /= sizeof(uint32_t); - voice->more_data = result == GET_BUFFER_MORE_DATA; - -#endif -} - -void common_hal_audioio_mixer_stop_voice(audioio_mixer_obj_t* self, uint8_t voice) { -#if 0 - self->voice[voice].sample = NULL; -#endif -} - bool common_hal_audioio_mixer_get_playing(audioio_mixer_obj_t* self) { -#if 0 for (int32_t v = 0; v < self->voice_count; v++) { - if (self->voice[v].sample != NULL) { + if (common_hal_audioio_mixervoice_get_playing(MP_OBJ_TO_PTR(self->voice[v]))) { return true; } } -#endif return false; } @@ -294,7 +244,6 @@ audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self, uint8_t channel, uint8_t** buffer, uint32_t* buffer_length) { -#if 0 if (!single_channel) { channel = 0; } @@ -318,7 +267,7 @@ audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self, self->use_first_buffer = !self->use_first_buffer; bool voices_active = false; for (int32_t v = 0; v < self->voice_count; v++) { - audioio_mixervoice_obj_t* voice = &self->voice[v]; + audioio_mixervoice_obj_t* voice = MP_OBJ_TO_PTR(self->voice[v]); uint32_t j = 0; bool voice_done = voice->sample == NULL; @@ -415,7 +364,6 @@ audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self, self->right_read_count += 1; *buffer = *buffer + self->bits_per_sample / 8; } -#endif return GET_BUFFER_MORE_DATA; } diff --git a/shared-module/audioio/Mixer.h b/shared-module/audioio/Mixer.h index 083a3ce7e..db5c6ce6f 100644 --- a/shared-module/audioio/Mixer.h +++ b/shared-module/audioio/Mixer.h @@ -31,7 +31,6 @@ #include "py/objtuple.h" #include "shared-module/audioio/__init__.h" -#include "shared-bindings/audioio/MixerVoice.h" typedef struct { mp_obj_base_t base; diff --git a/shared-module/audioio/MixerVoice.c b/shared-module/audioio/MixerVoice.c index 28df1c5d1..3579d500f 100644 --- a/shared-module/audioio/MixerVoice.c +++ b/shared-module/audioio/MixerVoice.c @@ -12,7 +12,49 @@ #include "py/runtime.h" #include "shared-module/audioio/__init__.h" #include "shared-module/audioio/RawSample.h" +#include "shared-module/audioio/MixerVoice.h" + +void common_hal_audioio_mixervoice_set_parent(audioio_mixervoice_obj_t* self, audioio_mixer_obj_t *parent) { + self->parent = parent; +} void common_hal_audioio_mixervoice_set_gain(audioio_mixervoice_obj_t* self, float gain) { self->gain = gain * ((1 << 15)-1); } + +void common_hal_audioio_mixervoice_play(audioio_mixervoice_obj_t* self, mp_obj_t sample, bool loop) { + if (audiosample_sample_rate(sample) != self->parent->sample_rate) { + mp_raise_ValueError(translate("The sample's sample rate does not match the mixer's")); + } + if (audiosample_channel_count(sample) != self->parent->channel_count) { + mp_raise_ValueError(translate("The sample's channel count does not match the mixer's")); + } + if (audiosample_bits_per_sample(sample) != self->parent->bits_per_sample) { + mp_raise_ValueError(translate("The sample's bits_per_sample does not match the mixer's")); + } + bool single_buffer; + bool samples_signed; + uint32_t max_buffer_length; + uint8_t spacing; + audiosample_get_buffer_structure(sample, false, &single_buffer, &samples_signed, + &max_buffer_length, &spacing); + if (samples_signed != self->parent->samples_signed) { + mp_raise_ValueError(translate("The sample's signedness does not match the mixer's")); + } + self->sample = sample; + self->loop = loop; + + audiosample_reset_buffer(sample, false, 0); + audioio_get_buffer_result_t result = audiosample_get_buffer(sample, false, 0, (uint8_t**) &self->remaining_buffer, &self->buffer_length); + // Track length in terms of words. + self->buffer_length /= sizeof(uint32_t); + self->more_data = result == GET_BUFFER_MORE_DATA; +} + +bool common_hal_audioio_mixervoice_get_playing(audioio_mixervoice_obj_t* self) { + return self->sample != NULL; +} + +void common_hal_audioio_mixervoice_stop(audioio_mixervoice_obj_t* self) { + self->sample = NULL; +} diff --git a/shared-module/audioio/MixerVoice.h b/shared-module/audioio/MixerVoice.h index b917829dd..1cf686a38 100644 --- a/shared-module/audioio/MixerVoice.h +++ b/shared-module/audioio/MixerVoice.h @@ -11,9 +11,11 @@ #include "py/obj.h" #include "shared-module/audioio/__init__.h" +#include "shared-module/audioio/Mixer.h" typedef struct { mp_obj_base_t base; + audioio_mixer_obj_t *parent; mp_obj_t sample; bool loop; bool more_data; -- cgit v1.2.3