Merge branch 'dm-mixer' of https://github.com/adafruit/circuitpython into mixer_volume

This commit is contained in:
sommersoft 2019-03-08 13:05:45 -06:00
commit e2fd34d7cc
9 changed files with 428 additions and 107 deletions

View File

@ -1,4 +1,4 @@
LD_FILE = boards/samd51x19-bootloader-external-flash.ld
LD_FILE = boards/samd51x19-external-flash.ld
USB_VID = 0x239A
USB_PID = 0x8030
USB_PRODUCT = "Trellis M4 Express"

View File

@ -24,6 +24,8 @@
* THE SOFTWARE.
*/
#include "shared-bindings/audioio/Mixer.h"
#include "shared-module/audioio/MixerVoice.h"
#include "shared-bindings/audioio/MixerVoice.h"
#include <stdint.h>
@ -103,10 +105,16 @@ STATIC mp_obj_t audioio_mixer_make_new(const mp_obj_type_t *type, size_t n_args,
if (bits_per_sample != 8 && bits_per_sample != 16) {
mp_raise_ValueError(translate("bits_per_sample must be 8 or 16"));
}
audioio_mixer_obj_t *self = m_new_obj_var(audioio_mixer_obj_t, audioio_mixer_voice_t, voice_count);
audioio_mixer_obj_t *self = m_new_obj_var(audioio_mixer_obj_t, mp_obj_t, voice_count);
self->base.type = &audioio_mixer_type;
common_hal_audioio_mixer_construct(self, voice_count, args[ARG_buffer_size].u_int, bits_per_sample, args[ARG_samples_signed].u_bool, channel_count, sample_rate);
for(int v=0; v<voice_count; v++){
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);
return MP_OBJ_FROM_PTR(self);
}
@ -139,54 +147,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__);
//| .. 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
//|
//| True when any voice is being output. (read-only)
@ -216,7 +176,6 @@ STATIC mp_obj_t audioio_mixer_obj_get_sample_rate(mp_obj_t self_in) {
}
MP_DEFINE_CONST_FUN_OBJ_1(audioio_mixer_get_sample_rate_obj, audioio_mixer_obj_get_sample_rate);
const mp_obj_property_t audioio_mixer_sample_rate_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&audioio_mixer_get_sample_rate_obj,
@ -224,17 +183,35 @@ const mp_obj_property_t audioio_mixer_sample_rate_obj = {
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: voice
//|
//| tuple of voice objects
//|
STATIC mp_obj_t audioio_mixer_obj_get_voice(mp_obj_t self_in) {
audioio_mixer_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_audioio_mixer_deinited(self));
return self->voice_tuple;
}
MP_DEFINE_CONST_FUN_OBJ_1(audioio_mixer_get_voice_obj, audioio_mixer_obj_get_voice);
const mp_obj_property_t audioio_mixer_voice_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&audioio_mixer_get_voice_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
STATIC const mp_rom_map_elem_t audioio_mixer_locals_dict_table[] = {
// Methods
{ 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___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
{ MP_ROM_QSTR(MP_QSTR_playing), MP_ROM_PTR(&audioio_mixer_playing_obj) },
{ MP_ROM_QSTR(MP_QSTR_sample_rate), MP_ROM_PTR(&audioio_mixer_sample_rate_obj) },
{ MP_ROM_QSTR(MP_QSTR_voice), MP_ROM_PTR(&audioio_mixer_voice_obj) }
};
STATIC MP_DEFINE_CONST_DICT(audioio_mixer_locals_dict, audioio_mixer_locals_dict_table);

View File

@ -32,6 +32,7 @@
#include "shared-bindings/audioio/RawSample.h"
extern const mp_obj_type_t audioio_mixer_type;
extern const mp_obj_type_t audioio_mixervoice_type;
void common_hal_audioio_mixer_construct(audioio_mixer_obj_t* self,
uint8_t voice_count,
@ -43,8 +44,6 @@ void common_hal_audioio_mixer_construct(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);
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);
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);

View File

@ -0,0 +1,195 @@
/*
* MixerVoice.c
*
* Created on: Nov 15, 2018
* Author: dean
*/
#include "shared-bindings/audioio/Mixer.h"
#include "shared-bindings/audioio/MixerVoice.h"
#include <stdint.h>
#include "lib/utils/context_manager_helpers.h"
#include "py/binary.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/audioio/RawSample.h"
#include "shared-bindings/util.h"
#include "supervisor/shared/translate.h"
//| .. currentmodule:: audioio
//|
//| :class:`Mixer` -- Mixes one or more audio samples together
//| ===========================================================
//|
//| Mixer mixes multiple samples into one sample.
//|
//| .. class:: Mixer(channel_count=2, buffer_size=1024)
//|
//| Create a Mixer object that can mix multiple channels with the same sample rate.
//|
//| :param int channel_count: The maximum number of samples to mix at once
//| :param int buffer_size: The total size in bytes of the buffers to mix into
//|
// TODO: support mono or stereo voices
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);
self->base.type = &audioio_mixervoice_type;
self->sample = NULL;
self->gain = ((1 << 15)-1);
return MP_OBJ_FROM_PTR(self);
}
//| .. method:: deinit()
//|
//| Deinitialises the Voice and releases any hardware resources for reuse.
//|
STATIC mp_obj_t audioio_mixervoice_deinit(mp_obj_t self_in) {
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(audioio_mixervoice_deinit_obj, audioio_mixervoice_deinit);
//| .. method:: __enter__()
//|
//| No-op used by Context Managers.
//|
// Provided by context manager helper.
//| .. method:: __exit__()
//|
//| Automatically deinitializes the hardware when exiting a context. See
//| :ref:`lifetime-and-contextmanagers` for more info.
//|
STATIC mp_obj_t audioio_mixervoice_obj___exit__(size_t n_args, const mp_obj_t *args) {
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audioio_mixervoice___exit___obj, 4, 4, audioio_mixervoice_obj___exit__);
//| .. method:: play(sample, *, 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_mixervoice_obj_play(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_sample, ARG_loop };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_sample, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ 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]);
//raise_error_if_deinited(common_hal_audioio_mixervoice_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_mixervoice_play(self, sample, args[ARG_loop].u_bool);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixervoice_play_obj, 1, audioio_mixervoice_obj_play);
//| .. method:: stop()
//|
//| 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) {
enum { ARG_voice };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_voice, MP_ARG_INT, {.u_int = 0} },
};
audioio_mixervoice_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_mixervoice_stop(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixervoice_stop_obj, 1, audioio_mixervoice_obj_stop);
//| .. method:: set_gain(voice, gain)
//|
//| Set the gain of a voice.
//|
//| gain must be a floating point number between 0 and 1
//|
STATIC mp_obj_t audioio_mixervoice_obj_set_gain(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_gain };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_gain, MP_ARG_OBJ | MP_ARG_REQUIRED },
};
audioio_mixervoice_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
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);
#if MICROPY_PY_BUILTINS_FLOAT
float gain = mp_obj_get_float(args[ARG_gain].u_obj);
#else
#error "floating point not supported"
#endif
if (gain > 1 || gain < 0) {
mp_raise_ValueError(translate("gain must be between 0 and 1"));
}
common_hal_audioio_mixervoice_set_gain(self, gain);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixervoice_set_gain_obj, 1, audioio_mixervoice_obj_set_gain);
const mp_obj_property_t audioio_mixervoice_gain_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&audioio_mixervoice_set_gain_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: playing
//|
//| True when any voice is being output. (read-only)
//|
STATIC mp_obj_t audioio_mixervoice_obj_get_playing(mp_obj_t self_in) {
#if 0
audioio_mixer_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_audioio_mixer_deinited(self));
return mp_obj_new_bool(common_hal_audioio_mixer_get_playing(self));
#endif
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(audioio_mixervoice_get_playing_obj, audioio_mixervoice_obj_get_playing);
const mp_obj_property_t audioio_mixervoice_playing_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&audioio_mixervoice_get_playing_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
STATIC const mp_rom_map_elem_t audioio_mixervoice_locals_dict_table[] = {
// Methods
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audioio_mixervoice_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audioio_mixervoice___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_play), MP_ROM_PTR(&audioio_mixervoice_play_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&audioio_mixervoice_stop_obj) },
// Properties
{ MP_ROM_QSTR(MP_QSTR_playing), MP_ROM_PTR(&audioio_mixervoice_playing_obj) },
{ MP_ROM_QSTR(MP_QSTR_gain), MP_ROM_PTR(&audioio_mixervoice_gain_obj) },
};
STATIC MP_DEFINE_CONST_DICT(audioio_mixervoice_locals_dict, audioio_mixervoice_locals_dict_table);
const mp_obj_type_t audioio_mixervoice_type = {
{ &mp_type_type },
.name = MP_QSTR_mixervoice,
.make_new = audioio_mixervoice_make_new,
.locals_dict = (mp_obj_dict_t*)&audioio_mixervoice_locals_dict,
};

View File

@ -0,0 +1,28 @@
/*
* MixerVoice.h
*
* Created on: Nov 15, 2018
* Author: dean
*/
#ifndef SHARED_BINDINGS_AUDIOIO_MIXERVOICE_H_
#define SHARED_BINDINGS_AUDIOIO_MIXERVOICE_H_
#include "common-hal/microcontroller/Pin.h"
#include "shared-module/audioio/Mixer.h"
#include "shared-bindings/audioio/RawSample.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_mixervoice_type;
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);
void common_hal_audioio_mixervoice_set_gain(audioio_mixervoice_obj_t* self, float gain);
bool common_hal_audioio_mixervoice_get_playing(audioio_mixervoice_obj_t* self);
#endif /* SHARED_BINDINGS_AUDIOIO_MIXERVOICE_H_ */

View File

@ -25,6 +25,7 @@
*/
#include "shared-bindings/audioio/Mixer.h"
#include "shared-bindings/audioio/MixerVoice.h"
#include <stdint.h>
@ -58,10 +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;
for (uint8_t i = 0; i < self->voice_count; i++) {
self->voice[i].sample = NULL;
}
}
void common_hal_audioio_mixer_deinit(audioio_mixer_obj_t* self) {
@ -77,46 +74,9 @@ 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"));
}
audioio_mixer_voice_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;
}
void common_hal_audioio_mixer_stop_voice(audioio_mixer_obj_t* self, uint8_t voice) {
self->voice[voice].sample = NULL;
}
bool common_hal_audioio_mixer_get_playing(audioio_mixer_obj_t* self) {
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;
}
}
@ -126,9 +86,11 @@ bool common_hal_audioio_mixer_get_playing(audioio_mixer_obj_t* self) {
void audioio_mixer_reset_buffer(audioio_mixer_obj_t* self,
bool single_channel,
uint8_t channel) {
#if 0
for (int32_t i = 0; i < self->voice_count; i++) {
self->voice[i].sample = NULL;
}
#endif
}
uint32_t add8signed(uint32_t a, uint32_t b) {
@ -215,6 +177,68 @@ uint32_t add16unsigned(uint32_t a, uint32_t b) {
#endif
}
//TODO:
static inline uint32_t mult8unsigned(uint32_t val, int32_t mul) {
#if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
return val;
val = __USUB8(val, 0x80808080);
#else
uint32_t result = 0;
for (int8_t i = 0; i < 4; i++) {
int8_t ai = (val >> (sizeof(uint8_t) * 8 * i)) - 128;
}
return result;
#endif
}
//TODO:
static inline uint32_t mult8signed(uint32_t val, int32_t mul) {
#if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
return val;
#else
uint32_t result = 0;
for (int8_t i = 0; i < 4; i++) {
int8_t ai = val >> (sizeof(int8_t) * 8 * i);
}
return result;
#endif
}
//TODO:
static inline uint32_t mult16unsigned(uint32_t val, int32_t mul) {
#if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
return val;
val = __USUB16(val, 0x80008000);
#else
uint32_t result = 0;
for (int8_t i = 0; i < 2; i++) {
int16_t ai = (val >> (sizeof(uint16_t) * 8 * i)) - 0x8000;
}
return result;
#endif
}
static inline uint32_t mult16signed(uint32_t val, int32_t mul) {
#if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
int32_t hi, lo;
int32_t bits = 16; // saturate to 16 bits
int32_t shift = 0; // shift is done automatically
asm volatile("smulwb %0, %1, %2" : "=r" (lo) : "r" (mul), "r" (val));
asm volatile("smulwt %0, %1, %2" : "=r" (hi) : "r" (mul), "r" (val));
asm volatile("ssat %0, %1, %2, asr %3" : "=r" (lo) : "I" (bits), "r" (lo), "I" (shift));
asm volatile("ssat %0, %1, %2, asr %3" : "=r" (hi) : "I" (bits), "r" (hi), "I" (shift));
asm volatile("pkhbt %0, %1, %2, lsl #16" : "=r" (val) : "r" (lo), "r" (hi)); // pack
return val;
#else
uint32_t result = 0;
//TODO:
for (int8_t i = 0; i < 2; i++) {
int16_t ai = val >> (sizeof(int16_t) * 8 * i);
}
return result;
#endif
}
audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self,
bool single_channel,
uint8_t channel,
@ -243,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_mixer_voice_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;
@ -285,6 +309,22 @@ audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self,
sample_value = voice->remaining_buffer[j];
}
// apply the mixer gain
if (!self->samples_signed) {
if (self->bits_per_sample == 8) {
sample_value = mult8unsigned(sample_value, voice->gain);
} else {
sample_value = mult16unsigned(sample_value, voice->gain);
}
}
else{
if (self->bits_per_sample == 8) {
sample_value = mult8signed(sample_value, voice->gain);
} else {
sample_value = mult16signed(sample_value, voice->gain);
}
}
if (!voices_active) {
word_buffer[i] = sample_value;
} else {

View File

@ -28,17 +28,10 @@
#define MICROPY_INCLUDED_SHARED_MODULE_AUDIOIO_MIXER_H
#include "py/obj.h"
#include "py/objtuple.h"
#include "shared-module/audioio/__init__.h"
typedef struct {
mp_obj_t sample;
bool loop;
bool more_data;
uint32_t* remaining_buffer;
uint32_t buffer_length;
} audioio_mixer_voice_t;
typedef struct {
mp_obj_base_t base;
uint32_t* first_buffer;
@ -55,7 +48,8 @@ typedef struct {
uint32_t right_read_count;
uint8_t voice_count;
audioio_mixer_voice_t voice[];
mp_obj_tuple_t *voice_tuple;
mp_obj_t voice[];
} audioio_mixer_obj_t;

View File

@ -0,0 +1,60 @@
/*
* MixerVoice.c
*
* Created on: Nov 15, 2018
* Author: dean
*/
#include "shared-bindings/audioio/Mixer.h"
#include <stdint.h>
#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;
}

View File

@ -0,0 +1,28 @@
/*
* MixerVoice.h
*
* Created on: Nov 15, 2018
* Author: dean
*/
#ifndef SHARED_MODULE_AUDIOIO_MIXERVOICE_H_
#define SHARED_MODULE_AUDIOIO_MIXERVOICE_H_
#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;
uint32_t* remaining_buffer;
uint32_t buffer_length;
int16_t gain;
} audioio_mixervoice_obj_t;
#endif /* SHARED_MODULE_AUDIOIO_MIXERVOICE_H_ */