35
35
#include "shared-bindings/util.h"
36
36
#include "supervisor/shared/translate.h"
37
37
38
- //| .. currentmodule:: audiobusio
39
- //|
40
- //| :class:`I2SOut` -- Output an I2S audio signal
41
- //| ========================================================
42
- //|
43
- //| I2S is used to output an audio signal on an I2S bus.
44
- //|
45
- //| .. class:: I2SOut(bit_clock, word_select, data, *, left_justified)
46
- //|
47
- //| Create a I2SOut object associated with the given pins.
48
- //|
49
- //| :param ~microcontroller.Pin bit_clock: The bit clock (or serial clock) pin
50
- //| :param ~microcontroller.Pin word_select: The word select (or left/right clock) pin
51
- //| :param ~microcontroller.Pin data: The data pin
52
- //| :param bool left_justified: True when data bits are aligned with the word select clock. False
53
- //| when they are shifted by one to match classic I2S protocol.
54
- //|
55
- //| Simple 8ksps 440 Hz sine wave on `Metro M0 Express <https://www.adafruit.com/product/3505>`_
56
- //| using `UDA1334 Breakout <https://www.adafruit.com/product/3678>`_::
57
- //|
58
- //| import audiobusio
59
- //| import audiocore
60
- //| import board
61
- //| import array
62
- //| import time
63
- //| import math
64
- //|
65
- //| # Generate one period of sine wave.
66
- //| length = 8000 // 440
67
- //| sine_wave = array.array("H", [0] * length)
68
- //| for i in range(length):
69
- //| sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15)
70
- //|
71
- //| sine_wave = audiocore.RawSample(sine_wave, sample_rate=8000)
72
- //| i2s = audiobusio.I2SOut(board.D1, board.D0, board.D9)
73
- //| i2s.play(sine_wave, loop=True)
74
- //| time.sleep(1)
75
- //| i2s.stop()
76
- //|
77
- //| Playing a wave file from flash::
78
- //|
79
- //| import board
80
- //| import audioio
81
- //| import audiocore
82
- //| import audiobusio
83
- //| import digitalio
84
- //|
85
- //|
86
- //| f = open("cplay-5.1-16bit-16khz.wav", "rb")
87
- //| wav = audiocore.WaveFile(f)
88
- //|
89
- //| a = audiobusio.I2SOut(board.D1, board.D0, board.D9)
90
- //|
91
- //| print("playing")
92
- //| a.play(wav)
93
- //| while a.playing:
94
- //| pass
95
- //| print("stopped")
96
- //|
38
+ //|class I2SOut:
39
+ //| """.. currentmodule:: audiobusio
40
+ //|
41
+ //| :class:`I2SOut` -- Output an I2S audio signal
42
+ //| ========================================================
43
+ //|
44
+ //| I2S is used to output an audio signal on an I2S bus."""
45
+ //| def __init__(self, bit_clock: microcontroller.Pin, word_select: microcontroller.Pin, data: microcontroller.Pin, *, left_justified: bool):
46
+ //| """Create a I2SOut object associated with the given pins.
47
+ //|
48
+ //| :param ~microcontroller.Pin bit_clock: The bit clock (or serial clock) pin
49
+ //| :param ~microcontroller.Pin word_select: The word select (or left/right clock) pin
50
+ //| :param ~microcontroller.Pin data: The data pin
51
+ //| :param bool left_justified: True when data bits are aligned with the word select clock. False
52
+ //| when they are shifted by one to match classic I2S protocol.
53
+ //|
54
+ //| Simple 8ksps 440 Hz sine wave on `Metro M0 Express <https://www.adafruit.com/product/3505>`_
55
+ //| using `UDA1334 Breakout <https://www.adafruit.com/product/3678>`_::
56
+ //|
57
+ //| import audiobusio
58
+ //| import audiocore
59
+ //| import board
60
+ //| import array
61
+ //| import time
62
+ //| import math
63
+ //|
64
+ //| # Generate one period of sine wave.
65
+ //| length = 8000 // 440
66
+ //| sine_wave = array.array("H", [0] * length)
67
+ //| for i in range(length):
68
+ //| sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15)
69
+ //|
70
+ //| sine_wave = audiocore.RawSample(sine_wave, sample_rate=8000)
71
+ //| i2s = audiobusio.I2SOut(board.D1, board.D0, board.D9)
72
+ //| i2s.play(sine_wave, loop=True)
73
+ //| time.sleep(1)
74
+ //| i2s.stop()
75
+ //|
76
+ //| Playing a wave file from flash::
77
+ //|
78
+ //| import board
79
+ //| import audioio
80
+ //| import audiocore
81
+ //| import audiobusio
82
+ //| import digitalio
83
+ //|
84
+ //|
85
+ //| f = open("cplay-5.1-16bit-16khz.wav", "rb")
86
+ //| wav = audiocore.WaveFile(f)
87
+ //|
88
+ //| a = audiobusio.I2SOut(board.D1, board.D0, board.D9)
89
+ //|
90
+ //| print("playing")
91
+ //| a.play(wav)
92
+ //| while a.playing:
93
+ //| pass
94
+ //| print("stopped")"""
95
+ //| ...
97
96
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 ) {
98
97
enum { ARG_bit_clock , ARG_word_select , ARG_data , ARG_left_justified };
99
98
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
116
115
return MP_OBJ_FROM_PTR (self );
117
116
}
118
117
119
- //| .. method:: deinit()
120
- //|
121
- //| Deinitialises the I2SOut and releases any hardware resources for reuse.
122
- //|
118
+ //| def deinit(self, ) -> Any:
119
+ //| """Deinitialises the I2SOut and releases any hardware resources for reuse."""
120
+ //| ...
123
121
STATIC mp_obj_t audiobusio_i2sout_deinit (mp_obj_t self_in ) {
124
122
audiobusio_i2sout_obj_t * self = MP_OBJ_TO_PTR (self_in );
125
123
common_hal_audiobusio_i2sout_deinit (self );
@@ -132,17 +130,15 @@ STATIC void check_for_deinit(audiobusio_i2sout_obj_t *self) {
132
130
raise_deinited_error ();
133
131
}
134
132
}
135
- //| .. method:: __enter__()
136
- //|
137
- //| No-op used by Context Managers.
138
- //|
133
+ //| def __enter__(self, ) -> Any:
134
+ //| """No-op used by Context Managers."""
135
+ //| ...
139
136
// Provided by context manager helper.
140
137
141
- //| .. method:: __exit__()
142
- //|
143
- //| Automatically deinitializes the hardware when exiting a context. See
144
- //| :ref:`lifetime-and-contextmanagers` for more info.
145
- //|
138
+ //| def __exit__(self, ) -> Any:
139
+ //| """Automatically deinitializes the hardware when exiting a context. See
140
+ //| :ref:`lifetime-and-contextmanagers` for more info."""
141
+ //| ...
146
142
STATIC mp_obj_t audiobusio_i2sout_obj___exit__ (size_t n_args , const mp_obj_t * args ) {
147
143
(void )n_args ;
148
144
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
151
147
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (audiobusio_i2sout___exit___obj , 4 , 4 , audiobusio_i2sout_obj___exit__ );
152
148
153
149
154
- //| .. method:: play(sample, *, loop=False)
150
+ //| def play(self, sample: Any, *, loop: Any = False) -> Any:
151
+ //| """Plays the sample once when loop=False and continuously when loop=True.
152
+ //| Does not block. Use `playing` to block.
155
153
//|
156
- //| Plays the sample once when loop=False and continuously when loop=True.
157
- //| Does not block. Use `playing` to block.
158
- //|
159
- //| Sample must be an `audiocore.WaveFile`, `audiocore.RawSample`, or `audiomixer.Mixer`.
160
- //|
161
- //| The sample itself should consist of 8 bit or 16 bit samples.
154
+ //| Sample must be an `audiocore.WaveFile`, `audiocore.RawSample`, or `audiomixer.Mixer`.
162
155
//|
156
+ //| The sample itself should consist of 8 bit or 16 bit samples."""
157
+ //| ...
163
158
STATIC mp_obj_t audiobusio_i2sout_obj_play (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
164
159
enum { ARG_sample , ARG_loop };
165
160
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
178
173
}
179
174
MP_DEFINE_CONST_FUN_OBJ_KW (audiobusio_i2sout_play_obj , 1 , audiobusio_i2sout_obj_play );
180
175
181
- //| .. method:: stop()
182
- //|
183
- //| Stops playback.
184
- //|
176
+ //| def stop(self, ) -> Any:
177
+ //| """Stops playback."""
178
+ //| ...
185
179
STATIC mp_obj_t audiobusio_i2sout_obj_stop (mp_obj_t self_in ) {
186
180
audiobusio_i2sout_obj_t * self = MP_OBJ_TO_PTR (self_in );
187
181
check_for_deinit (self );
@@ -190,10 +184,9 @@ STATIC mp_obj_t audiobusio_i2sout_obj_stop(mp_obj_t self_in) {
190
184
}
191
185
MP_DEFINE_CONST_FUN_OBJ_1 (audiobusio_i2sout_stop_obj , audiobusio_i2sout_obj_stop );
192
186
193
- //| .. attribute:: playing
194
- //|
195
- //| True when the audio sample is being output. (read-only)
196
- //|
187
+ //| playing: Any =
188
+ //| """True when the audio sample is being output. (read-only)"""
189
+ //| ...
197
190
STATIC mp_obj_t audiobusio_i2sout_obj_get_playing (mp_obj_t self_in ) {
198
191
audiobusio_i2sout_obj_t * self = MP_OBJ_TO_PTR (self_in );
199
192
check_for_deinit (self );
@@ -208,10 +201,9 @@ const mp_obj_property_t audiobusio_i2sout_playing_obj = {
208
201
(mp_obj_t )& mp_const_none_obj },
209
202
};
210
203
211
- //| .. method:: pause()
212
- //|
213
- //| Stops playback temporarily while remembering the position. Use `resume` to resume playback.
214
- //|
204
+ //| def pause(self, ) -> Any:
205
+ //| """Stops playback temporarily while remembering the position. Use `resume` to resume playback."""
206
+ //| ...
215
207
STATIC mp_obj_t audiobusio_i2sout_obj_pause (mp_obj_t self_in ) {
216
208
audiobusio_i2sout_obj_t * self = MP_OBJ_TO_PTR (self_in );
217
209
check_for_deinit (self );
@@ -224,10 +216,9 @@ STATIC mp_obj_t audiobusio_i2sout_obj_pause(mp_obj_t self_in) {
224
216
}
225
217
MP_DEFINE_CONST_FUN_OBJ_1 (audiobusio_i2sout_pause_obj , audiobusio_i2sout_obj_pause );
226
218
227
- //| .. method:: resume()
228
- //|
229
- //| Resumes sample playback after :py:func:`pause`.
230
- //|
219
+ //| def resume(self, ) -> Any:
220
+ //| """Resumes sample playback after :py:func:`pause`."""
221
+ //| ...
231
222
STATIC mp_obj_t audiobusio_i2sout_obj_resume (mp_obj_t self_in ) {
232
223
audiobusio_i2sout_obj_t * self = MP_OBJ_TO_PTR (self_in );
233
224
check_for_deinit (self );
@@ -240,10 +231,9 @@ STATIC mp_obj_t audiobusio_i2sout_obj_resume(mp_obj_t self_in) {
240
231
}
241
232
MP_DEFINE_CONST_FUN_OBJ_1 (audiobusio_i2sout_resume_obj , audiobusio_i2sout_obj_resume );
242
233
243
- //| .. attribute:: paused
244
- //|
245
- //| True when playback is paused. (read-only)
246
- //|
234
+ //| paused: Any =
235
+ //| """True when playback is paused. (read-only)"""
236
+ //| ...
247
237
STATIC mp_obj_t audiobusio_i2sout_obj_get_paused (mp_obj_t self_in ) {
248
238
audiobusio_i2sout_obj_t * self = MP_OBJ_TO_PTR (self_in );
249
239
check_for_deinit (self );
0 commit comments