2017-04-21 17:43:03 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Scott Shawcroft 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "lib/utils/context_manager_helpers.h"
|
2017-06-20 14:18:46 -04:00
|
|
|
#include "py/binary.h"
|
2017-04-21 17:43:03 -04:00
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
|
|
|
#include "shared-bindings/audioio/AudioOut.h"
|
2018-03-12 19:09:13 -04:00
|
|
|
#include "shared-bindings/audioio/RawSample.h"
|
2017-10-02 20:49:40 -04:00
|
|
|
#include "shared-bindings/util.h"
|
2018-07-31 19:53:54 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
2017-04-21 17:43:03 -04:00
|
|
|
|
|
|
|
//| .. currentmodule:: audioio
|
|
|
|
//|
|
|
|
|
//| :class:`AudioOut` -- Output an analog audio signal
|
|
|
|
//| ========================================================
|
|
|
|
//|
|
|
|
|
//| AudioOut can be used to output an analog audio signal on a given pin.
|
|
|
|
//|
|
2018-10-17 14:31:08 -04:00
|
|
|
//| .. class:: AudioOut(left_channel, *, right_channel=None, quiescent_value=0x8000)
|
2017-04-21 17:43:03 -04:00
|
|
|
//|
|
2018-03-12 19:09:13 -04:00
|
|
|
//| Create a AudioOut object associated with the given pin(s). This allows you to
|
|
|
|
//| play audio signals out on the given pin(s).
|
2017-04-21 17:43:03 -04:00
|
|
|
//|
|
2018-03-12 19:09:13 -04:00
|
|
|
//| :param ~microcontroller.Pin left_channel: The pin to output the left channel to
|
|
|
|
//| :param ~microcontroller.Pin right_channel: The pin to output the right channel to
|
2018-10-17 14:31:08 -04:00
|
|
|
//| :param int quiescent_value: The output value when no signal is present. Samples should start
|
|
|
|
//| and end with this value to prevent audible popping.
|
2017-04-21 17:43:03 -04:00
|
|
|
//|
|
|
|
|
//| Simple 8ksps 440 Hz sin wave::
|
|
|
|
//|
|
|
|
|
//| import audioio
|
|
|
|
//| import board
|
|
|
|
//| import array
|
|
|
|
//| import time
|
|
|
|
//| import math
|
|
|
|
//|
|
|
|
|
//| # Generate one period of sine wav.
|
|
|
|
//| length = 8000 // 440
|
2017-06-20 14:18:46 -04:00
|
|
|
//| sine_wave = array.array("H", [0] * length)
|
2017-04-21 17:43:03 -04:00
|
|
|
//| for i in range(length):
|
2017-06-20 14:18:46 -04:00
|
|
|
//| sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15)
|
2017-04-21 17:43:03 -04:00
|
|
|
//|
|
2018-03-12 19:09:13 -04:00
|
|
|
//| dac = audioio.AudioOut(board.SPEAKER)
|
2018-06-29 16:46:25 -04:00
|
|
|
//| sine_wave = audioio.RawSample(sine_wave, sample_rate=8000)
|
2018-03-12 19:09:13 -04:00
|
|
|
//| dac.play(sine_wave, loop=True)
|
2017-06-07 17:39:12 -04:00
|
|
|
//| time.sleep(1)
|
2018-06-29 16:46:25 -04:00
|
|
|
//| dac.stop()
|
2017-04-21 17:43:03 -04:00
|
|
|
//|
|
2017-05-10 21:21:02 -04:00
|
|
|
//| Playing a wave file from flash::
|
|
|
|
//|
|
|
|
|
//| import board
|
|
|
|
//| import audioio
|
|
|
|
//| import digitalio
|
|
|
|
//|
|
|
|
|
//| # Required for CircuitPlayground Express
|
|
|
|
//| speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
|
|
|
|
//| speaker_enable.switch_to_output(value=True)
|
|
|
|
//|
|
2018-05-19 10:41:36 -04:00
|
|
|
//| data = open("cplay-5.1-16bit-16khz.wav", "rb")
|
|
|
|
//| wav = audioio.WaveFile(data)
|
2018-03-12 19:09:13 -04:00
|
|
|
//| a = audioio.AudioOut(board.A0)
|
2017-05-10 21:21:02 -04:00
|
|
|
//|
|
|
|
|
//| print("playing")
|
2018-03-12 19:09:13 -04:00
|
|
|
//| a.play(wav)
|
2017-05-10 21:21:02 -04:00
|
|
|
//| while a.playing:
|
|
|
|
//| pass
|
|
|
|
//| print("stopped")
|
|
|
|
//|
|
2019-01-14 20:26:36 -05:00
|
|
|
STATIC mp_obj_t audioio_audioout_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2018-10-17 14:31:08 -04:00
|
|
|
enum { ARG_left_channel, ARG_right_channel, ARG_quiescent_value };
|
2018-03-12 19:09:13 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_left_channel, MP_ARG_OBJ | MP_ARG_REQUIRED },
|
|
|
|
{ MP_QSTR_right_channel, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = mp_const_none} },
|
2018-12-03 11:20:33 -05:00
|
|
|
{ MP_QSTR_quiescent_value, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x8000} },
|
2018-03-12 19:09:13 -04:00
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2019-01-14 20:26:36 -05:00
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2018-03-12 19:09:13 -04:00
|
|
|
|
|
|
|
mp_obj_t left_channel_obj = args[ARG_left_channel].u_obj;
|
|
|
|
assert_pin(left_channel_obj, false);
|
|
|
|
const mcu_pin_obj_t *left_channel_pin = MP_OBJ_TO_PTR(left_channel_obj);
|
|
|
|
|
|
|
|
mp_obj_t right_channel_obj = args[ARG_right_channel].u_obj;
|
|
|
|
const mcu_pin_obj_t *right_channel_pin = NULL;
|
|
|
|
if (right_channel_obj != mp_const_none) {
|
|
|
|
assert_pin(right_channel_obj, false);
|
|
|
|
right_channel_pin = MP_OBJ_TO_PTR(right_channel_obj);
|
|
|
|
}
|
2017-04-21 17:43:03 -04:00
|
|
|
|
2018-02-06 16:03:17 -05:00
|
|
|
// create AudioOut object from the given pin
|
2017-04-21 17:43:03 -04:00
|
|
|
audioio_audioout_obj_t *self = m_new_obj(audioio_audioout_obj_t);
|
|
|
|
self->base.type = &audioio_audioout_type;
|
2018-10-17 14:31:08 -04:00
|
|
|
common_hal_audioio_audioout_construct(self, left_channel_pin, right_channel_pin, args[ARG_quiescent_value].u_int);
|
2017-04-21 17:43:03 -04:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
//| .. method:: deinit()
|
|
|
|
//|
|
2018-02-06 16:03:17 -05:00
|
|
|
//| Deinitialises the AudioOut and releases any hardware resources for reuse.
|
2017-04-21 17:43:03 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t audioio_audioout_deinit(mp_obj_t self_in) {
|
|
|
|
audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
common_hal_audioio_audioout_deinit(self);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_deinit_obj, audioio_audioout_deinit);
|
|
|
|
|
2019-06-12 04:00:59 -04:00
|
|
|
STATIC void check_for_deinit(audioio_audioout_obj_t *self) {
|
|
|
|
if (common_hal_audioio_audioout_deinited(self)) {
|
|
|
|
raise_deinited_error();
|
|
|
|
}
|
|
|
|
}
|
2017-04-21 17:43:03 -04:00
|
|
|
//| .. method:: __enter__()
|
|
|
|
//|
|
|
|
|
//| No-op used by Context Managers.
|
|
|
|
//|
|
|
|
|
// Provided by context manager helper.
|
|
|
|
|
|
|
|
//| .. method:: __exit__()
|
|
|
|
//|
|
2017-06-07 17:39:12 -04:00
|
|
|
//| Automatically deinitializes the hardware when exiting a context. See
|
|
|
|
//| :ref:`lifetime-and-contextmanagers` for more info.
|
2017-04-21 17:43:03 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t audioio_audioout_obj___exit__(size_t n_args, const mp_obj_t *args) {
|
|
|
|
(void)n_args;
|
|
|
|
common_hal_audioio_audioout_deinit(args[0]);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audioio_audioout___exit___obj, 4, 4, audioio_audioout_obj___exit__);
|
|
|
|
|
|
|
|
|
2018-03-12 19:09:13 -04:00
|
|
|
//| .. method:: play(sample, *, loop=False)
|
2017-04-21 17:43:03 -04:00
|
|
|
//|
|
|
|
|
//| Plays the sample once when loop=False and continuously when loop=True.
|
|
|
|
//| Does not block. Use `playing` to block.
|
|
|
|
//|
|
2018-03-12 19:09:13 -04:00
|
|
|
//| Sample must be an `audioio.WaveFile` or `audioio.RawSample`.
|
|
|
|
//|
|
|
|
|
//| The sample itself should consist of 16 bit samples. Microcontrollers with a lower output
|
|
|
|
//| resolution will use the highest order bits to output. For example, the SAMD21 has a 10 bit
|
|
|
|
//| DAC that ignores the lowest 6 bits when playing 16 bit samples.
|
|
|
|
//|
|
2017-04-21 17:43:03 -04:00
|
|
|
STATIC mp_obj_t audioio_audioout_obj_play(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2018-03-12 19:09:13 -04:00
|
|
|
enum { ARG_sample, ARG_loop };
|
2017-04-21 17:43:03 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2018-03-12 19:09:13 -04:00
|
|
|
{ MP_QSTR_sample, MP_ARG_OBJ | MP_ARG_REQUIRED },
|
|
|
|
{ MP_QSTR_loop, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
|
2017-04-21 17:43:03 -04:00
|
|
|
};
|
|
|
|
audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-04-21 17:43:03 -04: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-03-12 19:09:13 -04:00
|
|
|
mp_obj_t sample = args[ARG_sample].u_obj;
|
|
|
|
common_hal_audioio_audioout_play(self, sample, args[ARG_loop].u_bool);
|
|
|
|
|
2017-04-21 17:43:03 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(audioio_audioout_play_obj, 1, audioio_audioout_obj_play);
|
|
|
|
|
|
|
|
//| .. method:: stop()
|
|
|
|
//|
|
2018-05-07 20:47:29 -04:00
|
|
|
//| Stops playback and resets to the start of the sample.
|
2017-04-21 17:43:03 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t audioio_audioout_obj_stop(mp_obj_t self_in) {
|
|
|
|
audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-04-21 17:43:03 -04:00
|
|
|
common_hal_audioio_audioout_stop(self);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_stop_obj, audioio_audioout_obj_stop);
|
|
|
|
|
|
|
|
//| .. attribute:: playing
|
|
|
|
//|
|
2018-05-07 20:47:29 -04:00
|
|
|
//| True when an audio sample is being output even if `paused`. (read-only)
|
2017-04-21 17:43:03 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t audioio_audioout_obj_get_playing(mp_obj_t self_in) {
|
|
|
|
audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-04-21 17:43:03 -04:00
|
|
|
return mp_obj_new_bool(common_hal_audioio_audioout_get_playing(self));
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_get_playing_obj, audioio_audioout_obj_get_playing);
|
|
|
|
|
|
|
|
const mp_obj_property_t audioio_audioout_playing_obj = {
|
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = {(mp_obj_t)&audioio_audioout_get_playing_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2018-05-07 20:47:29 -04:00
|
|
|
//| .. method:: pause()
|
|
|
|
//|
|
|
|
|
//| Stops playback temporarily while remembering the position. Use `resume` to resume playback.
|
|
|
|
//|
|
|
|
|
STATIC mp_obj_t audioio_audioout_obj_pause(mp_obj_t self_in) {
|
|
|
|
audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2018-05-07 20:47:29 -04:00
|
|
|
|
|
|
|
if (!common_hal_audioio_audioout_get_playing(self)) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_RuntimeError(translate("Not playing"));
|
2018-05-07 20:47:29 -04:00
|
|
|
}
|
|
|
|
common_hal_audioio_audioout_pause(self);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_pause_obj, audioio_audioout_obj_pause);
|
|
|
|
|
|
|
|
//| .. method:: resume()
|
|
|
|
//|
|
|
|
|
//| Resumes sample playback after :py:func:`pause`.
|
|
|
|
//|
|
|
|
|
STATIC mp_obj_t audioio_audioout_obj_resume(mp_obj_t self_in) {
|
|
|
|
audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2018-05-07 20:47:29 -04:00
|
|
|
|
2018-05-08 16:00:05 -04:00
|
|
|
if (common_hal_audioio_audioout_get_paused(self)) {
|
|
|
|
common_hal_audioio_audioout_resume(self);
|
2018-05-07 20:47:29 -04:00
|
|
|
}
|
2018-05-08 16:00:05 -04:00
|
|
|
|
2018-05-07 20:47:29 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_resume_obj, audioio_audioout_obj_resume);
|
|
|
|
|
|
|
|
//| .. attribute:: paused
|
|
|
|
//|
|
|
|
|
//| True when playback is paused. (read-only)
|
|
|
|
//|
|
|
|
|
STATIC mp_obj_t audioio_audioout_obj_get_paused(mp_obj_t self_in) {
|
|
|
|
audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2018-05-07 20:47:29 -04:00
|
|
|
return mp_obj_new_bool(common_hal_audioio_audioout_get_paused(self));
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_get_paused_obj, audioio_audioout_obj_get_paused);
|
|
|
|
|
|
|
|
const mp_obj_property_t audioio_audioout_paused_obj = {
|
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = {(mp_obj_t)&audioio_audioout_get_paused_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2017-04-21 17:43:03 -04:00
|
|
|
STATIC const mp_rom_map_elem_t audioio_audioout_locals_dict_table[] = {
|
|
|
|
// Methods
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audioio_audioout_deinit_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audioio_audioout___exit___obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_play), MP_ROM_PTR(&audioio_audioout_play_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&audioio_audioout_stop_obj) },
|
2018-05-07 20:47:29 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pause), MP_ROM_PTR(&audioio_audioout_pause_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_resume), MP_ROM_PTR(&audioio_audioout_resume_obj) },
|
2017-04-21 17:43:03 -04:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_playing), MP_ROM_PTR(&audioio_audioout_playing_obj) },
|
2018-05-07 20:47:29 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_paused), MP_ROM_PTR(&audioio_audioout_paused_obj) },
|
2017-04-21 17:43:03 -04:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(audioio_audioout_locals_dict, audioio_audioout_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t audioio_audioout_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_AudioOut,
|
|
|
|
.make_new = audioio_audioout_make_new,
|
|
|
|
.locals_dict = (mp_obj_dict_t*)&audioio_audioout_locals_dict,
|
|
|
|
};
|