Added inline pyi to audiobusio

This commit is contained in:
dherrada 2020-04-27 16:35:03 -04:00
parent 8344fce994
commit e96235d0cf
No known key found for this signature in database
GPG Key ID: CE2ADBAB8775CE81
2 changed files with 127 additions and 135 deletions

View File

@ -35,65 +35,64 @@
#include "shared-bindings/util.h" #include "shared-bindings/util.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
//| .. currentmodule:: audiobusio //|class I2SOut:
//| """.. currentmodule:: audiobusio
//| //|
//| :class:`I2SOut` -- Output an I2S audio signal //| :class:`I2SOut` -- Output an I2S audio signal
//| ======================================================== //| ========================================================
//| //|
//| I2S is used to output an audio signal on an I2S bus. //| I2S is used to output an audio signal on an I2S bus."""
//| def __init__(self, bit_clock: microcontroller.Pin, word_select: microcontroller.Pin, data: microcontroller.Pin, *, left_justified: bool):
//| """Create a I2SOut object associated with the given pins.
//| //|
//| .. class:: I2SOut(bit_clock, word_select, data, *, left_justified) //| :param ~microcontroller.Pin bit_clock: The bit clock (or serial clock) pin
//| :param ~microcontroller.Pin word_select: The word select (or left/right clock) pin
//| :param ~microcontroller.Pin data: The data pin
//| :param bool left_justified: True when data bits are aligned with the word select clock. False
//| when they are shifted by one to match classic I2S protocol.
//| //|
//| Create a I2SOut object associated with the given pins. //| Simple 8ksps 440 Hz sine wave on `Metro M0 Express <https://www.adafruit.com/product/3505>`_
//| using `UDA1334 Breakout <https://www.adafruit.com/product/3678>`_::
//| //|
//| :param ~microcontroller.Pin bit_clock: The bit clock (or serial clock) pin //| import audiobusio
//| :param ~microcontroller.Pin word_select: The word select (or left/right clock) pin //| import audiocore
//| :param ~microcontroller.Pin data: The data pin //| import board
//| :param bool left_justified: True when data bits are aligned with the word select clock. False //| import array
//| when they are shifted by one to match classic I2S protocol. //| import time
//| import math
//| //|
//| Simple 8ksps 440 Hz sine wave on `Metro M0 Express <https://www.adafruit.com/product/3505>`_ //| # Generate one period of sine wave.
//| using `UDA1334 Breakout <https://www.adafruit.com/product/3678>`_:: //| length = 8000 // 440
//| sine_wave = array.array("H", [0] * length)
//| for i in range(length):
//| sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15)
//| //|
//| import audiobusio //| sine_wave = audiocore.RawSample(sine_wave, sample_rate=8000)
//| import audiocore //| i2s = audiobusio.I2SOut(board.D1, board.D0, board.D9)
//| import board //| i2s.play(sine_wave, loop=True)
//| import array //| time.sleep(1)
//| import time //| i2s.stop()
//| import math
//| //|
//| # Generate one period of sine wave. //| Playing a wave file from flash::
//| length = 8000 // 440
//| sine_wave = array.array("H", [0] * length)
//| for i in range(length):
//| sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15)
//| //|
//| sine_wave = audiocore.RawSample(sine_wave, sample_rate=8000) //| import board
//| i2s = audiobusio.I2SOut(board.D1, board.D0, board.D9) //| import audioio
//| i2s.play(sine_wave, loop=True) //| import audiocore
//| time.sleep(1) //| import audiobusio
//| i2s.stop() //| import digitalio
//|
//| Playing a wave file from flash::
//|
//| import board
//| import audioio
//| import audiocore
//| import audiobusio
//| import digitalio
//| //|
//| //|
//| f = open("cplay-5.1-16bit-16khz.wav", "rb") //| f = open("cplay-5.1-16bit-16khz.wav", "rb")
//| wav = audiocore.WaveFile(f) //| wav = audiocore.WaveFile(f)
//| //|
//| a = audiobusio.I2SOut(board.D1, board.D0, board.D9) //| a = audiobusio.I2SOut(board.D1, board.D0, board.D9)
//|
//| print("playing")
//| a.play(wav)
//| while a.playing:
//| pass
//| print("stopped")
//| //|
//| print("playing")
//| a.play(wav)
//| while a.playing:
//| pass
//| print("stopped")"""
//| ...
STATIC mp_obj_t audiobusio_i2sout_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC mp_obj_t audiobusio_i2sout_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_bit_clock, ARG_word_select, ARG_data, ARG_left_justified }; enum { ARG_bit_clock, ARG_word_select, ARG_data, ARG_left_justified };
static const mp_arg_t allowed_args[] = { static const mp_arg_t allowed_args[] = {
@ -116,10 +115,9 @@ STATIC mp_obj_t audiobusio_i2sout_make_new(const mp_obj_type_t *type, size_t n_a
return MP_OBJ_FROM_PTR(self); return MP_OBJ_FROM_PTR(self);
} }
//| .. method:: deinit() //| def deinit(self, ) -> Any:
//| //| """Deinitialises the I2SOut and releases any hardware resources for reuse."""
//| Deinitialises the I2SOut and releases any hardware resources for reuse. //| ...
//|
STATIC mp_obj_t audiobusio_i2sout_deinit(mp_obj_t self_in) { STATIC mp_obj_t audiobusio_i2sout_deinit(mp_obj_t self_in) {
audiobusio_i2sout_obj_t *self = MP_OBJ_TO_PTR(self_in); audiobusio_i2sout_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_audiobusio_i2sout_deinit(self); common_hal_audiobusio_i2sout_deinit(self);
@ -132,17 +130,15 @@ STATIC void check_for_deinit(audiobusio_i2sout_obj_t *self) {
raise_deinited_error(); raise_deinited_error();
} }
} }
//| .. method:: __enter__() //| def __enter__(self, ) -> Any:
//| //| """No-op used by Context Managers."""
//| No-op used by Context Managers. //| ...
//|
// Provided by context manager helper. // Provided by context manager helper.
//| .. method:: __exit__() //| def __exit__(self, ) -> Any:
//| //| """Automatically deinitializes the hardware when exiting a context. See
//| Automatically deinitializes the hardware when exiting a context. See //| :ref:`lifetime-and-contextmanagers` for more info."""
//| :ref:`lifetime-and-contextmanagers` for more info. //| ...
//|
STATIC mp_obj_t audiobusio_i2sout_obj___exit__(size_t n_args, const mp_obj_t *args) { STATIC mp_obj_t audiobusio_i2sout_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args; (void)n_args;
common_hal_audiobusio_i2sout_deinit(args[0]); common_hal_audiobusio_i2sout_deinit(args[0]);
@ -151,15 +147,14 @@ STATIC mp_obj_t audiobusio_i2sout_obj___exit__(size_t n_args, const mp_obj_t *ar
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audiobusio_i2sout___exit___obj, 4, 4, audiobusio_i2sout_obj___exit__); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audiobusio_i2sout___exit___obj, 4, 4, audiobusio_i2sout_obj___exit__);
//| .. method:: play(sample, *, loop=False) //| def play(self, sample: Any, *, loop: Any = False) -> Any:
//| """Plays the sample once when loop=False and continuously when loop=True.
//| Does not block. Use `playing` to block.
//| //|
//| Plays the sample once when loop=False and continuously when loop=True. //| Sample must be an `audiocore.WaveFile`, `audiocore.RawSample`, or `audiomixer.Mixer`.
//| Does not block. Use `playing` to block.
//|
//| Sample must be an `audiocore.WaveFile`, `audiocore.RawSample`, or `audiomixer.Mixer`.
//|
//| The sample itself should consist of 8 bit or 16 bit samples.
//| //|
//| The sample itself should consist of 8 bit or 16 bit samples."""
//| ...
STATIC mp_obj_t audiobusio_i2sout_obj_play(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC mp_obj_t audiobusio_i2sout_obj_play(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_sample, ARG_loop }; enum { ARG_sample, ARG_loop };
static const mp_arg_t allowed_args[] = { static const mp_arg_t allowed_args[] = {
@ -178,10 +173,9 @@ STATIC mp_obj_t audiobusio_i2sout_obj_play(size_t n_args, const mp_obj_t *pos_ar
} }
MP_DEFINE_CONST_FUN_OBJ_KW(audiobusio_i2sout_play_obj, 1, audiobusio_i2sout_obj_play); MP_DEFINE_CONST_FUN_OBJ_KW(audiobusio_i2sout_play_obj, 1, audiobusio_i2sout_obj_play);
//| .. method:: stop() //| def stop(self, ) -> Any:
//| //| """Stops playback."""
//| Stops playback. //| ...
//|
STATIC mp_obj_t audiobusio_i2sout_obj_stop(mp_obj_t self_in) { STATIC mp_obj_t audiobusio_i2sout_obj_stop(mp_obj_t self_in) {
audiobusio_i2sout_obj_t *self = MP_OBJ_TO_PTR(self_in); audiobusio_i2sout_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self); check_for_deinit(self);
@ -190,10 +184,9 @@ STATIC mp_obj_t audiobusio_i2sout_obj_stop(mp_obj_t self_in) {
} }
MP_DEFINE_CONST_FUN_OBJ_1(audiobusio_i2sout_stop_obj, audiobusio_i2sout_obj_stop); MP_DEFINE_CONST_FUN_OBJ_1(audiobusio_i2sout_stop_obj, audiobusio_i2sout_obj_stop);
//| .. attribute:: playing //| playing: Any =
//| //| """True when the audio sample is being output. (read-only)"""
//| True when the audio sample is being output. (read-only) //| ...
//|
STATIC mp_obj_t audiobusio_i2sout_obj_get_playing(mp_obj_t self_in) { STATIC mp_obj_t audiobusio_i2sout_obj_get_playing(mp_obj_t self_in) {
audiobusio_i2sout_obj_t *self = MP_OBJ_TO_PTR(self_in); audiobusio_i2sout_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self); check_for_deinit(self);
@ -208,10 +201,9 @@ const mp_obj_property_t audiobusio_i2sout_playing_obj = {
(mp_obj_t)&mp_const_none_obj}, (mp_obj_t)&mp_const_none_obj},
}; };
//| .. method:: pause() //| def pause(self, ) -> Any:
//| //| """Stops playback temporarily while remembering the position. Use `resume` to resume playback."""
//| Stops playback temporarily while remembering the position. Use `resume` to resume playback. //| ...
//|
STATIC mp_obj_t audiobusio_i2sout_obj_pause(mp_obj_t self_in) { STATIC mp_obj_t audiobusio_i2sout_obj_pause(mp_obj_t self_in) {
audiobusio_i2sout_obj_t *self = MP_OBJ_TO_PTR(self_in); audiobusio_i2sout_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self); check_for_deinit(self);
@ -224,10 +216,9 @@ STATIC mp_obj_t audiobusio_i2sout_obj_pause(mp_obj_t self_in) {
} }
MP_DEFINE_CONST_FUN_OBJ_1(audiobusio_i2sout_pause_obj, audiobusio_i2sout_obj_pause); MP_DEFINE_CONST_FUN_OBJ_1(audiobusio_i2sout_pause_obj, audiobusio_i2sout_obj_pause);
//| .. method:: resume() //| def resume(self, ) -> Any:
//| //| """Resumes sample playback after :py:func:`pause`."""
//| Resumes sample playback after :py:func:`pause`. //| ...
//|
STATIC mp_obj_t audiobusio_i2sout_obj_resume(mp_obj_t self_in) { STATIC mp_obj_t audiobusio_i2sout_obj_resume(mp_obj_t self_in) {
audiobusio_i2sout_obj_t *self = MP_OBJ_TO_PTR(self_in); audiobusio_i2sout_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self); check_for_deinit(self);
@ -240,10 +231,9 @@ STATIC mp_obj_t audiobusio_i2sout_obj_resume(mp_obj_t self_in) {
} }
MP_DEFINE_CONST_FUN_OBJ_1(audiobusio_i2sout_resume_obj, audiobusio_i2sout_obj_resume); MP_DEFINE_CONST_FUN_OBJ_1(audiobusio_i2sout_resume_obj, audiobusio_i2sout_obj_resume);
//| .. attribute:: paused //| paused: Any =
//| //| """True when playback is paused. (read-only)"""
//| True when playback is paused. (read-only) //| ...
//|
STATIC mp_obj_t audiobusio_i2sout_obj_get_paused(mp_obj_t self_in) { STATIC mp_obj_t audiobusio_i2sout_obj_get_paused(mp_obj_t self_in) {
audiobusio_i2sout_obj_t *self = MP_OBJ_TO_PTR(self_in); audiobusio_i2sout_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self); check_for_deinit(self);

View File

@ -36,32 +36,39 @@
#include "shared-bindings/util.h" #include "shared-bindings/util.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
//| .. currentmodule:: audiobusio
//|class PDMIn:
//| """.. currentmodule:: audiobusio
//| //|
//| :class:`PDMIn` -- Record an input PDM audio stream //| :class:`PDMIn` -- Record an input PDM audio stream
//| ======================================================== //| ========================================================
//| //|
//| PDMIn can be used to record an input audio signal on a given set of pins. //| PDMIn can be used to record an input audio signal on a given set of pins."""
//| //|
//| .. class:: PDMIn(clock_pin, data_pin, *, sample_rate=16000, bit_depth=8, mono=True, oversample=64, startup_delay=0.11) //| def __init__(self, clock_pin: microcontroller.Pin, data_pin: microcontroller.Pin, *, sample_rate: int = 16000, bit_depth: int = 8, mono: bool = True, oversample: int = 64, startup_delay: float = 0.11):
//| //| """Create a PDMIn object associated with the given pins. This allows you to
//| Create a PDMIn object associated with the given pins. This allows you to //| record audio signals from the given pins. Individual ports may put further
//| record audio signals from the given pins. Individual ports may put further //| restrictions on the recording parameters. The overall sample rate is
//| restrictions on the recording parameters. The overall sample rate is //| determined by `sample_rate` x ``oversample``, and the total must be 1MHz or
//| determined by `sample_rate` x ``oversample``, and the total must be 1MHz or //| higher, so `sample_rate` must be a minimum of 16000.
//| higher, so `sample_rate` must be a minimum of 16000.
//|
//| :param ~microcontroller.Pin clock_pin: The pin to output the clock to
//| :param ~microcontroller.Pin data_pin: The pin to read the data from
//| :param int sample_rate: Target sample_rate of the resulting samples. Check `sample_rate` for actual value.
//| Minimum sample_rate is about 16000 Hz.
//| :param int bit_depth: Final number of bits per sample. Must be divisible by 8
//| :param bool mono: True when capturing a single channel of audio, captures two channels otherwise
//| :param int oversample: Number of single bit samples to decimate into a final sample. Must be divisible by 8
//| :param float startup_delay: seconds to wait after starting microphone clock
//| to allow microphone to turn on. Most require only 0.01s; some require 0.1s. Longer is safer.
//| Must be in range 0.0-1.0 seconds.
//| //|
//| :param ~microcontroller.Pin clock_pin: The pin to output the clock to
//| :param ~microcontroller.Pin data_pin: The pin to read the data from
//| :param int sample_rate: Target sample_rate of the resulting samples. Check `sample_rate` for actual value.
//| Minimum sample_rate is about 16000 Hz.
//| :param int bit_depth: Final number of bits per sample. Must be divisible by 8
//| :param bool mono: True when capturing a single channel of audio, captures two channels otherwise
//| :param int oversample: Number of single bit samples to decimate into a final sample. Must be divisible by 8
//| :param float startup_delay: seconds to wait after starting microphone clock
//| to allow microphone to turn on. Most require only 0.01s; some require 0.1s. Longer is safer.
//| Must be in range 0.0-1.0 seconds."""
//| ...
//| Record 8-bit unsigned samples to buffer:: //| Record 8-bit unsigned samples to buffer::
//| //|
@ -86,7 +93,7 @@
//| b.append(0) //| b.append(0)
//| with audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16) as mic: //| with audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16) as mic:
//| mic.record(b, len(b)) //| mic.record(b, len(b))
//| //|
STATIC mp_obj_t audiobusio_pdmin_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC mp_obj_t audiobusio_pdmin_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_clock_pin, ARG_data_pin, ARG_sample_rate, ARG_bit_depth, ARG_mono, ARG_oversample, ARG_startup_delay }; enum { ARG_clock_pin, ARG_data_pin, ARG_sample_rate, ARG_bit_depth, ARG_mono, ARG_oversample, ARG_startup_delay };
static const mp_arg_t allowed_args[] = { static const mp_arg_t allowed_args[] = {
@ -138,10 +145,9 @@ STATIC mp_obj_t audiobusio_pdmin_make_new(const mp_obj_type_t *type, size_t n_ar
return MP_OBJ_FROM_PTR(self); return MP_OBJ_FROM_PTR(self);
} }
//| .. method:: deinit() //| def deinit(self, ) -> Any:
//| //| """Deinitialises the PDMIn and releases any hardware resources for reuse."""
//| Deinitialises the PDMIn and releases any hardware resources for reuse. //| ...
//|
STATIC mp_obj_t audiobusio_pdmin_deinit(mp_obj_t self_in) { STATIC mp_obj_t audiobusio_pdmin_deinit(mp_obj_t self_in) {
audiobusio_pdmin_obj_t *self = MP_OBJ_TO_PTR(self_in); audiobusio_pdmin_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_audiobusio_pdmin_deinit(self); common_hal_audiobusio_pdmin_deinit(self);
@ -154,16 +160,14 @@ STATIC void check_for_deinit(audiobusio_pdmin_obj_t *self) {
raise_deinited_error(); raise_deinited_error();
} }
} }
//| .. method:: __enter__() //| def __enter__(self, ) -> Any:
//| //| """No-op used by Context Managers."""
//| No-op used by Context Managers. //| ...
//|
// Provided by context manager helper. // Provided by context manager helper.
//| .. method:: __exit__() //| def __exit__(self, ) -> Any:
//| //| """Automatically deinitializes the hardware when exiting a context."""
//| Automatically deinitializes the hardware when exiting a context. //| ...
//|
STATIC mp_obj_t audiobusio_pdmin_obj___exit__(size_t n_args, const mp_obj_t *args) { STATIC mp_obj_t audiobusio_pdmin_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args; (void)n_args;
common_hal_audiobusio_pdmin_deinit(args[0]); common_hal_audiobusio_pdmin_deinit(args[0]);
@ -172,18 +176,17 @@ STATIC mp_obj_t audiobusio_pdmin_obj___exit__(size_t n_args, const mp_obj_t *arg
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audiobusio_pdmin___exit___obj, 4, 4, audiobusio_pdmin_obj___exit__); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audiobusio_pdmin___exit___obj, 4, 4, audiobusio_pdmin_obj___exit__);
//| .. method:: record(destination, destination_length) //| def record(self, destination: Any, destination_length: Any) -> Any:
//| //| """Records destination_length bytes of samples to destination. This is
//| Records destination_length bytes of samples to destination. This is
//| blocking. //| blocking.
//| //|
//| An IOError may be raised when the destination is too slow to record the //| An IOError may be raised when the destination is too slow to record the
//| audio at the given rate. For internal flash, writing all 1s to the file //| audio at the given rate. For internal flash, writing all 1s to the file
//| before recording is recommended to speed up writes. //| before recording is recommended to speed up writes.
//|
//| :return: The number of samples recorded. If this is less than ``destination_length``,
//| some samples were missed due to processing time.
//| //|
//| :return: The number of samples recorded. If this is less than ``destination_length``,
//| some samples were missed due to processing time."""
//| ...
STATIC mp_obj_t audiobusio_pdmin_obj_record(mp_obj_t self_obj, mp_obj_t destination, mp_obj_t destination_length) { STATIC mp_obj_t audiobusio_pdmin_obj_record(mp_obj_t self_obj, mp_obj_t destination, mp_obj_t destination_length) {
audiobusio_pdmin_obj_t *self = MP_OBJ_TO_PTR(self_obj); audiobusio_pdmin_obj_t *self = MP_OBJ_TO_PTR(self_obj);
check_for_deinit(self); check_for_deinit(self);
@ -214,11 +217,10 @@ STATIC mp_obj_t audiobusio_pdmin_obj_record(mp_obj_t self_obj, mp_obj_t destinat
} }
MP_DEFINE_CONST_FUN_OBJ_3(audiobusio_pdmin_record_obj, audiobusio_pdmin_obj_record); MP_DEFINE_CONST_FUN_OBJ_3(audiobusio_pdmin_record_obj, audiobusio_pdmin_obj_record);
//| .. attribute:: sample_rate //| sample_rate: Any =
//| //| """The actual sample_rate of the recording. This may not match the constructed
//| The actual sample_rate of the recording. This may not match the constructed //| sample rate due to internal clock limitations."""
//| sample rate due to internal clock limitations. //| ...
//|
STATIC mp_obj_t audiobusio_pdmin_obj_get_sample_rate(mp_obj_t self_in) { STATIC mp_obj_t audiobusio_pdmin_obj_get_sample_rate(mp_obj_t self_in) {
audiobusio_pdmin_obj_t *self = MP_OBJ_TO_PTR(self_in); audiobusio_pdmin_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self); check_for_deinit(self);