2018-11-15 14:11:45 -05:00
|
|
|
/*
|
2019-08-03 12:20:06 -04:00
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
2018-11-15 14:11:45 -05:00
|
|
|
*
|
2019-08-03 12:20:06 -04:00
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 DeanM for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2018-11-15 14:11:45 -05:00
|
|
|
*/
|
2019-08-25 00:36:18 -04:00
|
|
|
#include "shared-bindings/audiomixer/Mixer.h"
|
|
|
|
#include "shared-bindings/audiomixer/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"
|
2019-08-03 12:20:06 -04:00
|
|
|
#include "shared-bindings/audiocore/RawSample.h"
|
2018-11-15 14:11:45 -05:00
|
|
|
#include "shared-bindings/util.h"
|
|
|
|
#include "supervisor/shared/translate.h"
|
|
|
|
|
2020-04-30 13:06:09 -04:00
|
|
|
//| class MixerVoice:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Voice objects used with Mixer
|
2018-11-15 14:11:45 -05:00
|
|
|
//|
|
2020-04-30 13:06:09 -04:00
|
|
|
//| Used to access and control samples with `audiomixer.Mixer`."""
|
2018-11-15 14:11:45 -05:00
|
|
|
//|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def __init__(self):
|
2020-04-30 13:06:09 -04:00
|
|
|
//| """MixerVoice instance object(s) created by `audiomixer.Mixer`."""
|
|
|
|
//| ...
|
2018-11-15 14:11:45 -05:00
|
|
|
//|
|
|
|
|
// TODO: support mono or stereo voices
|
2019-08-25 00:36:18 -04:00
|
|
|
STATIC mp_obj_t audiomixer_mixervoice_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
audiomixer_mixervoice_obj_t *self = m_new_obj(audiomixer_mixervoice_obj_t);
|
|
|
|
self->base.type = &audiomixer_mixervoice_type;
|
2019-08-24 08:56:12 -04:00
|
|
|
|
2019-08-25 00:36:18 -04:00
|
|
|
common_hal_audiomixer_mixervoice_construct(self);
|
2019-08-24 08:56:12 -04:00
|
|
|
|
2018-11-15 14:11:45 -05:00
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2020-07-03 13:49:00 -04:00
|
|
|
//| def play(self, sample: Union[audiocore.WaveFile, Mixer, audiocore.RawSample], *, loop: bool = False) -> None:
|
2020-04-30 13:06:09 -04:00
|
|
|
//| """Plays the sample once when ``loop=False``, and continuously when ``loop=True``.
|
|
|
|
//| Does not block. Use `playing` to block.
|
2018-11-15 14:11:45 -05:00
|
|
|
//|
|
2020-04-30 13:06:09 -04:00
|
|
|
//| Sample must be an `audiocore.WaveFile`, `audiomixer.Mixer` or `audiocore.RawSample`.
|
|
|
|
//|
|
|
|
|
//| The sample must match the `audiomixer.Mixer`'s encoding settings given in the constructor."""
|
|
|
|
//| ...
|
2018-11-15 14:11:45 -05:00
|
|
|
//|
|
2019-08-25 00:36:18 -04:00
|
|
|
STATIC mp_obj_t audiomixer_mixervoice_obj_play(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2018-11-15 14:11:45 -05:00
|
|
|
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} },
|
|
|
|
};
|
2019-08-25 00:36:18 -04:00
|
|
|
audiomixer_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);
|
|
|
|
|
|
|
|
mp_obj_t sample = args[ARG_sample].u_obj;
|
2019-08-25 00:36:18 -04:00
|
|
|
common_hal_audiomixer_mixervoice_play(self, sample, args[ARG_loop].u_bool);
|
2018-11-15 14:11:45 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2019-08-25 00:36:18 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(audiomixer_mixervoice_play_obj, 1, audiomixer_mixervoice_obj_play);
|
2018-11-15 14:11:45 -05:00
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def stop(self) -> None:
|
2020-04-30 13:06:09 -04:00
|
|
|
//| """Stops playback of the sample on this voice."""
|
|
|
|
//| ...
|
|
|
|
//|
|
2019-08-25 00:36:18 -04:00
|
|
|
STATIC mp_obj_t audiomixer_mixervoice_obj_stop(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2018-11-15 14:11:45 -05:00
|
|
|
enum { ARG_voice };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_voice, MP_ARG_INT, {.u_int = 0} },
|
|
|
|
};
|
2019-08-25 00:36:18 -04:00
|
|
|
audiomixer_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);
|
|
|
|
|
2019-08-25 00:36:18 -04:00
|
|
|
common_hal_audiomixer_mixervoice_stop(self);
|
2018-11-20 14:14:22 -05:00
|
|
|
|
2018-11-15 14:11:45 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2019-08-25 00:36:18 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(audiomixer_mixervoice_stop_obj, 1, audiomixer_mixervoice_obj_stop);
|
2018-11-15 14:11:45 -05:00
|
|
|
|
2020-07-02 13:25:07 -04:00
|
|
|
//| level: float = ...
|
2020-04-30 13:06:09 -04:00
|
|
|
//| """The volume level of a voice, as a floating point number between 0 and 1."""
|
|
|
|
//|
|
2019-08-25 00:36:18 -04:00
|
|
|
STATIC mp_obj_t audiomixer_mixervoice_obj_get_level(mp_obj_t self_in) {
|
|
|
|
return mp_obj_new_float(common_hal_audiomixer_mixervoice_get_level(self_in));
|
2019-03-12 17:10:54 -04:00
|
|
|
}
|
2019-08-25 00:36:18 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(audiomixer_mixervoice_get_level_obj, audiomixer_mixervoice_obj_get_level);
|
2019-03-12 17:10:54 -04:00
|
|
|
|
2019-08-25 00:36:18 -04:00
|
|
|
STATIC mp_obj_t audiomixer_mixervoice_obj_set_level(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2019-03-12 11:24:54 -04:00
|
|
|
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
|
|
|
};
|
2019-08-25 00:36:18 -04:00
|
|
|
audiomixer_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);
|
|
|
|
|
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
|
|
|
|
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-08-25 00:36:18 -04:00
|
|
|
common_hal_audiomixer_mixervoice_set_level(self, level);
|
2018-11-15 14:11:45 -05:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2019-08-25 00:36:18 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(audiomixer_mixervoice_set_level_obj, 1, audiomixer_mixervoice_obj_set_level);
|
2018-11-15 14:11:45 -05:00
|
|
|
|
2019-08-25 00:36:18 -04:00
|
|
|
const mp_obj_property_t audiomixer_mixervoice_level_obj = {
|
2018-11-15 14:11:45 -05:00
|
|
|
.base.type = &mp_type_property,
|
2019-08-25 00:36:18 -04:00
|
|
|
.proxy = {(mp_obj_t)&audiomixer_mixervoice_get_level_obj,
|
|
|
|
(mp_obj_t)&audiomixer_mixervoice_set_level_obj,
|
2018-11-15 14:11:45 -05:00
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2020-07-02 11:55:27 -04:00
|
|
|
//| playing: bool = ...
|
2020-04-30 13:06:09 -04:00
|
|
|
//| """True when this voice is being output. (read-only)"""
|
|
|
|
//|
|
2019-08-03 12:20:06 -04:00
|
|
|
|
2019-08-25 00:36:18 -04:00
|
|
|
STATIC mp_obj_t audiomixer_mixervoice_obj_get_playing(mp_obj_t self_in) {
|
|
|
|
audiomixer_mixervoice_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-08-24 09:20:26 -04:00
|
|
|
|
2019-08-25 00:36:18 -04:00
|
|
|
return mp_obj_new_bool(common_hal_audiomixer_mixervoice_get_playing(self));
|
2019-08-24 09:20:26 -04:00
|
|
|
|
2018-11-15 14:11:45 -05:00
|
|
|
}
|
2019-08-25 00:36:18 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(audiomixer_mixervoice_get_playing_obj, audiomixer_mixervoice_obj_get_playing);
|
2018-11-15 14:11:45 -05:00
|
|
|
|
2019-08-25 00:36:18 -04:00
|
|
|
const mp_obj_property_t audiomixer_mixervoice_playing_obj = {
|
2018-11-15 14:11:45 -05:00
|
|
|
.base.type = &mp_type_property,
|
2019-08-25 00:36:18 -04:00
|
|
|
.proxy = {(mp_obj_t)&audiomixer_mixervoice_get_playing_obj,
|
2018-11-15 14:11:45 -05:00
|
|
|
(mp_obj_t)&mp_const_none_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2019-08-25 00:36:18 -04:00
|
|
|
STATIC const mp_rom_map_elem_t audiomixer_mixervoice_locals_dict_table[] = {
|
2018-11-15 14:11:45 -05:00
|
|
|
// Methods
|
2019-08-25 00:36:18 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_play), MP_ROM_PTR(&audiomixer_mixervoice_play_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&audiomixer_mixervoice_stop_obj) },
|
2018-11-15 14:11:45 -05:00
|
|
|
|
|
|
|
// Properties
|
2019-08-25 00:36:18 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_playing), MP_ROM_PTR(&audiomixer_mixervoice_playing_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_level), MP_ROM_PTR(&audiomixer_mixervoice_level_obj) },
|
2018-11-15 14:11:45 -05:00
|
|
|
};
|
2019-08-25 00:36:18 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(audiomixer_mixervoice_locals_dict, audiomixer_mixervoice_locals_dict_table);
|
2018-11-15 14:11:45 -05:00
|
|
|
|
2019-08-25 00:36:18 -04:00
|
|
|
const mp_obj_type_t audiomixer_mixervoice_type = {
|
2018-11-15 14:11:45 -05:00
|
|
|
{ &mp_type_type },
|
2019-03-11 19:30:56 -04:00
|
|
|
.name = MP_QSTR_MixerVoice,
|
2019-08-25 00:36:18 -04:00
|
|
|
.make_new = audiomixer_mixervoice_make_new,
|
|
|
|
.locals_dict = (mp_obj_dict_t*)&audiomixer_mixervoice_locals_dict,
|
2018-11-15 14:11:45 -05:00
|
|
|
};
|