DM: mixer voice stuff

This commit is contained in:
dean 2018-11-20 14:14:22 -05:00
parent feef177858
commit 1bf6c588e7
8 changed files with 60 additions and 121 deletions

View File

@ -25,6 +25,7 @@
*/ */
#include "shared-bindings/audioio/Mixer.h" #include "shared-bindings/audioio/Mixer.h"
#include "shared-module/audioio/MixerVoice.h" #include "shared-module/audioio/MixerVoice.h"
#include "shared-bindings/audioio/MixerVoice.h"
#include <stdint.h> #include <stdint.h>
@ -113,6 +114,7 @@ STATIC mp_obj_t audioio_mixer_make_new(const mp_obj_type_t *type, size_t n_args,
for(int v=0; v<voice_count; v++){ for(int v=0; v<voice_count; v++){
self->voice[v] = audioio_mixervoice_type.make_new(&audioio_mixervoice_type, 0, 0, NULL); self->voice[v] = audioio_mixervoice_type.make_new(&audioio_mixervoice_type, 0, 0, NULL);
common_hal_audioio_mixervoice_set_parent(self->voice[v], self);
} }
self->voice_tuple = mp_obj_new_tuple(self->voice_count, self->voice); self->voice_tuple = mp_obj_new_tuple(self->voice_count, self->voice);
@ -148,54 +150,6 @@ STATIC mp_obj_t audioio_mixer_obj___exit__(size_t n_args, const mp_obj_t *args)
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audioio_mixer___exit___obj, 4, 4, audioio_mixer_obj___exit__); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audioio_mixer___exit___obj, 4, 4, audioio_mixer_obj___exit__);
//| .. method:: play(sample, *, voice=0, loop=False)
//|
//| Plays the sample once when loop=False and continuously when loop=True.
//| Does not block. Use `playing` to block.
//|
//| Sample must be an `audioio.WaveFile`, `audioio.Mixer` or `audioio.RawSample`.
//|
//| The sample must match the Mixer's encoding settings given in the constructor.
//|
STATIC mp_obj_t audioio_mixer_obj_play(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_sample, ARG_voice, ARG_loop };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_sample, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_voice, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
{ MP_QSTR_loop, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
};
audioio_mixer_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(common_hal_audioio_mixer_deinited(self));
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_obj_t sample = args[ARG_sample].u_obj;
common_hal_audioio_mixer_play(self, sample, args[ARG_voice].u_int, args[ARG_loop].u_bool);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixer_play_obj, 1, audioio_mixer_obj_play);
//| .. method:: stop_voice(voice=0)
//|
//| Stops playback of the sample on the given voice.
//|
STATIC mp_obj_t audioio_mixer_obj_stop_voice(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_voice };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_voice, MP_ARG_INT, {.u_int = 0} },
};
audioio_mixer_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(common_hal_audioio_mixer_deinited(self));
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
common_hal_audioio_mixer_stop_voice(self, args[ARG_voice].u_int);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixer_stop_voice_obj, 1, audioio_mixer_obj_stop_voice);
//| .. attribute:: playing //| .. attribute:: playing
//| //|
//| True when any voice is being output. (read-only) //| True when any voice is being output. (read-only)
@ -245,7 +199,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(audioio_mixer_get_voice_obj, audioio_mixer_obj_get_voi
const mp_obj_property_t audioio_mixer_voice_obj = { const mp_obj_property_t audioio_mixer_voice_obj = {
.base.type = &mp_type_property, .base.type = &mp_type_property,
.proxy = {(mp_obj_t)&audioio_mixer_obj_get_voice, .proxy = {(mp_obj_t)&audioio_mixer_get_voice_obj,
(mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj}, (mp_obj_t)&mp_const_none_obj},
}; };
@ -256,8 +210,6 @@ STATIC const mp_rom_map_elem_t audioio_mixer_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audioio_mixer_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audioio_mixer_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audioio_mixer___exit___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audioio_mixer___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_play), MP_ROM_PTR(&audioio_mixer_play_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop_voice), MP_ROM_PTR(&audioio_mixer_stop_voice_obj) },
// Properties // Properties
{ MP_ROM_QSTR(MP_QSTR_playing), MP_ROM_PTR(&audioio_mixer_playing_obj) }, { MP_ROM_QSTR(MP_QSTR_playing), MP_ROM_PTR(&audioio_mixer_playing_obj) },

View File

@ -44,10 +44,6 @@ void common_hal_audioio_mixer_construct(audioio_mixer_obj_t* self,
void common_hal_audioio_mixer_deinit(audioio_mixer_obj_t* self); void common_hal_audioio_mixer_deinit(audioio_mixer_obj_t* self);
bool common_hal_audioio_mixer_deinited(audioio_mixer_obj_t* self); bool common_hal_audioio_mixer_deinited(audioio_mixer_obj_t* self);
void common_hal_audioio_mixer_play(audioio_mixer_obj_t* self, mp_obj_t sample, uint8_t voice, bool loop);
void common_hal_audioio_mixer_stop_voice(audioio_mixer_obj_t* self, uint8_t voice);
void common_hal_audioio_mixer_set_gain(audioio_mixer_obj_t* self, uint8_t voice, float gain);
bool common_hal_audioio_mixer_get_playing(audioio_mixer_obj_t* self); bool common_hal_audioio_mixer_get_playing(audioio_mixer_obj_t* self);
uint32_t common_hal_audioio_mixer_get_sample_rate(audioio_mixer_obj_t* self); uint32_t common_hal_audioio_mixer_get_sample_rate(audioio_mixer_obj_t* self);

View File

@ -36,7 +36,8 @@
STATIC mp_obj_t audioio_mixervoice_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { STATIC mp_obj_t audioio_mixervoice_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
audioio_mixervoice_obj_t *self = m_new(audioio_mixervoice_obj_t, 1); audioio_mixervoice_obj_t *self = m_new(audioio_mixervoice_obj_t, 1);
self->base.type = &audioio_mixervoice_type; self->base.type = &audioio_mixervoice_type;
self->sample = NULL;
self->gain = ((1 << 15)-1);
return MP_OBJ_FROM_PTR(self); return MP_OBJ_FROM_PTR(self);
} }
@ -76,41 +77,38 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audioio_mixervoice___exit___obj, 4, 4
//| The sample must match the Mixer's encoding settings given in the constructor. //| The sample must match the Mixer's encoding settings given in the constructor.
//| //|
STATIC mp_obj_t audioio_mixervoice_obj_play(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC mp_obj_t audioio_mixervoice_obj_play(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
#if 0
enum { ARG_sample, ARG_loop }; enum { ARG_sample, ARG_loop };
static const mp_arg_t allowed_args[] = { static const mp_arg_t allowed_args[] = {
{ MP_QSTR_sample, MP_ARG_OBJ | MP_ARG_REQUIRED }, { MP_QSTR_sample, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_loop, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} }, { MP_QSTR_loop, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
}; };
audioio_mixervoice_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); audioio_mixervoice_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(common_hal_audioio_mixervoice_deinited(self)); //raise_error_if_deinited(common_hal_audioio_mixervoice_deinited(self));
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_obj_t sample = args[ARG_sample].u_obj; mp_obj_t sample = args[ARG_sample].u_obj;
common_hal_audioio_mixer_play(self, sample, self->u_int, args[ARG_loop].u_ool); common_hal_audioio_mixervoice_play(self, sample, args[ARG_loop].u_bool);
#endif
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixervoice_play_obj, 1, audioio_mixervoice_obj_play); MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixervoice_play_obj, 1, audioio_mixervoice_obj_play);
//| .. method:: stop_voice() //| .. method:: stop()
//| //|
//| Stops playback of the sample on this voice. //| Stops playback of the sample on this voice.
//| //|
STATIC mp_obj_t audioio_mixervoice_obj_stop(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC mp_obj_t audioio_mixervoice_obj_stop(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
#if 0
enum { ARG_voice }; enum { ARG_voice };
static const mp_arg_t allowed_args[] = { static const mp_arg_t allowed_args[] = {
{ MP_QSTR_voice, MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_voice, MP_ARG_INT, {.u_int = 0} },
}; };
audioio_mixervoice_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); audioio_mixervoice_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(common_hal_audioio_mixer_deinited(self)); //raise_error_if_deinited(common_hal_audioio_mixer_deinited(self));
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
common_hal_audioio_mixer_stop_voice(self, args[ARG_voice].u_int); common_hal_audioio_mixervoice_stop(self);
#endif
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixervoice_stop_obj, 1, audioio_mixervoice_obj_stop); MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixervoice_stop_obj, 1, audioio_mixervoice_obj_stop);

View File

@ -12,11 +12,13 @@
#include "shared-module/audioio/Mixer.h" #include "shared-module/audioio/Mixer.h"
#include "shared-bindings/audioio/RawSample.h" #include "shared-bindings/audioio/RawSample.h"
#include "shared-module/audioio/MixerVoice.h" #include "shared-module/audioio/MixerVoice.h"
#include "shared-module/audioio/Mixer.h"
extern const mp_obj_type_t audioio_mixer_type; extern const mp_obj_type_t audioio_mixer_type;
extern const mp_obj_type_t audioio_mixervoice_type; extern const mp_obj_type_t audioio_mixervoice_type;
void common_hal_audioio_mixervoice_deinit(audioio_mixervoice_obj_t* self); 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_play(audioio_mixervoice_obj_t* self, mp_obj_t sample, bool loop);
void common_hal_audioio_mixervoice_stop(audioio_mixervoice_obj_t* self); void common_hal_audioio_mixervoice_stop(audioio_mixervoice_obj_t* self);
void common_hal_audioio_mixervoice_set_gain(audioio_mixervoice_obj_t* self, float gain); void common_hal_audioio_mixervoice_set_gain(audioio_mixervoice_obj_t* self, float gain);

View File

@ -25,6 +25,7 @@
*/ */
#include "shared-bindings/audioio/Mixer.h" #include "shared-bindings/audioio/Mixer.h"
#include "shared-bindings/audioio/MixerVoice.h"
#include <stdint.h> #include <stdint.h>
@ -58,13 +59,6 @@ void common_hal_audioio_mixer_construct(audioio_mixer_obj_t* self,
self->channel_count = channel_count; self->channel_count = channel_count;
self->sample_rate = sample_rate; self->sample_rate = sample_rate;
self->voice_count = voice_count; 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) { 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; 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) { bool common_hal_audioio_mixer_get_playing(audioio_mixer_obj_t* self) {
#if 0
for (int32_t v = 0; v < self->voice_count; v++) { 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; return true;
} }
} }
#endif
return false; 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 channel,
uint8_t** buffer, uint8_t** buffer,
uint32_t* buffer_length) { uint32_t* buffer_length) {
#if 0
if (!single_channel) { if (!single_channel) {
channel = 0; 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; self->use_first_buffer = !self->use_first_buffer;
bool voices_active = false; bool voices_active = false;
for (int32_t v = 0; v < self->voice_count; v++) { 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; uint32_t j = 0;
bool voice_done = voice->sample == NULL; 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; self->right_read_count += 1;
*buffer = *buffer + self->bits_per_sample / 8; *buffer = *buffer + self->bits_per_sample / 8;
} }
#endif
return GET_BUFFER_MORE_DATA; return GET_BUFFER_MORE_DATA;
} }

View File

@ -31,7 +31,6 @@
#include "py/objtuple.h" #include "py/objtuple.h"
#include "shared-module/audioio/__init__.h" #include "shared-module/audioio/__init__.h"
#include "shared-bindings/audioio/MixerVoice.h"
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;

View File

@ -12,7 +12,49 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-module/audioio/__init__.h" #include "shared-module/audioio/__init__.h"
#include "shared-module/audioio/RawSample.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) { void common_hal_audioio_mixervoice_set_gain(audioio_mixervoice_obj_t* self, float gain) {
self->gain = gain * ((1 << 15)-1); 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;
}

View File

@ -11,9 +11,11 @@
#include "py/obj.h" #include "py/obj.h"
#include "shared-module/audioio/__init__.h" #include "shared-module/audioio/__init__.h"
#include "shared-module/audioio/Mixer.h"
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
audioio_mixer_obj_t *parent;
mp_obj_t sample; mp_obj_t sample;
bool loop; bool loop;
bool more_data; bool more_data;