2018-11-15 14:11:45 -05:00
|
|
|
/*
|
|
|
|
* MixerVoice.c
|
|
|
|
*
|
|
|
|
* Created on: Nov 15, 2018
|
|
|
|
* Author: dean
|
|
|
|
*/
|
|
|
|
#include "shared-bindings/audioio/Mixer.h"
|
2018-11-15 15:04:11 -05:00
|
|
|
#include "shared-bindings/audioio/MixerVoice.h"
|
2018-11-15 14:11:45 -05:00
|
|
|
|
|
|
|
#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
|
2019-03-11 19:30:56 -04:00
|
|
|
STATIC mp_obj_t audioio_mixervoice_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
audioio_mixervoice_obj_t *self = m_new_obj(audioio_mixervoice_obj_t);
|
2018-11-15 14:11:45 -05:00
|
|
|
self->base.type = &audioio_mixervoice_type;
|
2018-11-20 14:14:22 -05:00
|
|
|
self->sample = NULL;
|
2019-03-12 11:24:54 -04:00
|
|
|
self->level = ((1 << 15)-1);
|
2018-11-15 14:11:45 -05:00
|
|
|
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]);
|
2018-11-20 14:14:22 -05:00
|
|
|
//raise_error_if_deinited(common_hal_audioio_mixervoice_deinited(self));
|
2018-11-15 14:11:45 -05:00
|
|
|
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;
|
2018-11-20 14:14:22 -05:00
|
|
|
common_hal_audioio_mixervoice_play(self, sample, args[ARG_loop].u_bool);
|
2018-11-15 14:11:45 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixervoice_play_obj, 1, audioio_mixervoice_obj_play);
|
|
|
|
|
2018-11-20 14:14:22 -05:00
|
|
|
//| .. method:: stop()
|
2018-11-15 14:11:45 -05:00
|
|
|
//|
|
|
|
|
//| 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]);
|
2018-11-20 14:14:22 -05:00
|
|
|
//raise_error_if_deinited(common_hal_audioio_mixer_deinited(self));
|
2018-11-15 14:11:45 -05:00
|
|
|
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);
|
|
|
|
|
2018-11-20 14:14:22 -05:00
|
|
|
common_hal_audioio_mixervoice_stop(self);
|
|
|
|
|
2018-11-15 14:11:45 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixervoice_stop_obj, 1, audioio_mixervoice_obj_stop);
|
|
|
|
|
2019-03-12 17:10:54 -04:00
|
|
|
//| .. 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);
|
|
|
|
|
2018-11-15 14:11:45 -05:00
|
|
|
//| .. method:: set_gain(voice, gain)
|
|
|
|
//|
|
|
|
|
//| Set the gain of a voice.
|
|
|
|
//|
|
|
|
|
//| gain must be a floating point number between 0 and 1
|
|
|
|
//|
|
2019-03-12 11:24:54 -04:00
|
|
|
STATIC mp_obj_t audioio_mixervoice_obj_set_level(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_level };
|
2018-11-15 14:11:45 -05:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2019-03-12 11:24:54 -04:00
|
|
|
{ MP_QSTR_level, MP_ARG_OBJ | MP_ARG_REQUIRED },
|
2018-11-15 14:11:45 -05:00
|
|
|
};
|
2018-11-15 15:04:11 -05:00
|
|
|
audioio_mixervoice_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
2018-11-15 14:11:45 -05:00
|
|
|
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
|
2019-03-12 11:24:54 -04:00
|
|
|
float level = mp_obj_get_float(args[ARG_level].u_obj);
|
2018-11-15 14:11:45 -05:00
|
|
|
#else
|
|
|
|
#error "floating point not supported"
|
|
|
|
#endif
|
|
|
|
|
2019-03-12 11:24:54 -04:00
|
|
|
if (level > 1 || level < 0) {
|
|
|
|
mp_raise_ValueError(translate("level must be between 0 and 1"));
|
2018-11-15 14:11:45 -05:00
|
|
|
}
|
|
|
|
|
2019-03-12 11:24:54 -04:00
|
|
|
common_hal_audioio_mixervoice_set_level(self, level);
|
2018-11-15 14:11:45 -05:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2019-03-12 11:24:54 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixervoice_set_level_obj, 1, audioio_mixervoice_obj_set_level);
|
2018-11-15 14:11:45 -05:00
|
|
|
|
2019-03-12 11:24:54 -04:00
|
|
|
const mp_obj_property_t audioio_mixervoice_level_obj = {
|
2018-11-15 14:11:45 -05:00
|
|
|
.base.type = &mp_type_property,
|
2019-03-12 17:10:54 -04:00
|
|
|
.proxy = {(mp_obj_t)&audioio_mixervoice_get_level_obj,
|
2019-03-12 11:24:54 -04:00
|
|
|
(mp_obj_t)&audioio_mixervoice_set_level_obj,
|
2018-11-15 14:11:45 -05:00
|
|
|
(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) },
|
2019-03-12 11:24:54 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_level), MP_ROM_PTR(&audioio_mixervoice_level_obj) },
|
2018-11-15 14:11:45 -05:00
|
|
|
};
|
|
|
|
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 },
|
2019-03-11 19:30:56 -04:00
|
|
|
.name = MP_QSTR_MixerVoice,
|
2018-11-15 14:11:45 -05:00
|
|
|
.make_new = audioio_mixervoice_make_new,
|
|
|
|
.locals_dict = (mp_obj_dict_t*)&audioio_mixervoice_locals_dict,
|
|
|
|
};
|