36
36
#include "shared-bindings/util.h"
37
37
#include "supervisor/shared/translate.h"
38
38
39
- //| .. currentmodule:: audioio
40
- //|
41
- //| :class:`AudioOut` -- Output an analog audio signal
42
- //| ========================================================
43
- //|
44
- //| AudioOut can be used to output an analog audio signal on a given pin.
45
- //|
46
- //| .. class:: AudioOut(left_channel, *, right_channel=None, quiescent_value=0x8000)
47
- //|
48
- //| Create a AudioOut object associated with the given pin(s). This allows you to
49
- //| play audio signals out on the given pin(s).
50
- //|
51
- //| :param ~microcontroller.Pin left_channel: The pin to output the left channel to
52
- //| :param ~microcontroller.Pin right_channel: The pin to output the right channel to
53
- //| :param int quiescent_value: The output value when no signal is present. Samples should start
54
- //| and end with this value to prevent audible popping.
55
- //|
56
- //| Simple 8ksps 440 Hz sin wave::
57
- //|
58
- //| import audiocore
59
- //| import audioio
60
- //| import board
61
- //| import array
62
- //| import time
63
- //| import math
64
- //|
65
- //| # Generate one period of sine wav.
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
- //| dac = audioio.AudioOut(board.SPEAKER)
72
- //| sine_wave = audiocore.RawSample(sine_wave, sample_rate=8000)
73
- //| dac.play(sine_wave, loop=True)
74
- //| time.sleep(1)
75
- //| dac.stop()
76
- //|
77
- //| Playing a wave file from flash::
78
- //|
79
- //| import board
80
- //| import audioio
81
- //| import digitalio
82
- //|
83
- //| # Required for CircuitPlayground Express
84
- //| speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
85
- //| speaker_enable.switch_to_output(value=True)
86
- //|
87
- //| data = open("cplay-5.1-16bit-16khz.wav", "rb")
88
- //| wav = audiocore.WaveFile(data)
89
- //| a = audioio.AudioOut(board.A0)
90
- //|
91
- //| print("playing")
92
- //| a.play(wav)
93
- //| while a.playing:
94
- //| pass
95
- //| print("stopped")
96
- //|
39
+ //|class AudioOut:
40
+ //| """.. currentmodule:: audioio
41
+ //|
42
+ //| :class:`AudioOut` -- Output an analog audio signal
43
+ //| ========================================================
44
+ //|
45
+ //| AudioOut can be used to output an analog audio signal on a given pin."""
46
+ //|
47
+ //| def __init__(self, left_channel: microcontroller.Pin, *, right_channel: microcontroller.Pin = None, quiescent_value: int = 0x8000):
48
+ //| """ Create a AudioOut object associated with the given pin(s). This allows you to
49
+ //| play audio signals out on the given pin(s).
50
+ //|
51
+ //| :param ~microcontroller.Pin left_channel: The pin to output the left channel to
52
+ //| :param ~microcontroller.Pin right_channel: The pin to output the right channel to
53
+ //| :param int quiescent_value: The output value when no signal is present. Samples should start
54
+ //| and end with this value to prevent audible popping.
55
+ //|
56
+ //| Simple 8ksps 440 Hz sin wave::
57
+ //|
58
+ //| import audiocore
59
+ //| import audioio
60
+ //| import board
61
+ //| import array
62
+ //| import time
63
+ //| import math
64
+ //|
65
+ //| # Generate one period of sine wav.
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
+ //| dac = audioio.AudioOut(board.SPEAKER)
72
+ //| sine_wave = audiocore.RawSample(sine_wave, sample_rate=8000)
73
+ //| dac.play(sine_wave, loop=True)
74
+ //| time.sleep(1)
75
+ //| dac.stop()
76
+ //|
77
+ //| Playing a wave file from flash::
78
+ //|
79
+ //| import board
80
+ //| import audioio
81
+ //| import digitalio
82
+ //|
83
+ //| # Required for CircuitPlayground Express
84
+ //| speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
85
+ //| speaker_enable.switch_to_output(value=True)
86
+ //|
87
+ //| data = open("cplay-5.1-16bit-16khz.wav", "rb")
88
+ //| wav = audiocore.WaveFile(data)
89
+ //| a = audioio.AudioOut(board.A0)
90
+ //|
91
+ //| print("playing")
92
+ //| a.play(wav)
93
+ //| while a.playing:
94
+ //| pass
95
+ //| print("stopped")"""
96
+ //| ...
97
97
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 ) {
98
98
enum { ARG_left_channel , ARG_right_channel , ARG_quiescent_value };
99
99
static const mp_arg_t allowed_args [] = {
@@ -115,10 +115,9 @@ STATIC mp_obj_t audioio_audioout_make_new(const mp_obj_type_t *type, size_t n_ar
115
115
return MP_OBJ_FROM_PTR (self );
116
116
}
117
117
118
- //| .. method:: deinit()
119
- //|
120
- //| Deinitialises the AudioOut and releases any hardware resources for reuse.
121
- //|
118
+ //| def deinit(self, ) -> Any:
119
+ //| """Deinitialises the AudioOut and releases any hardware resources for reuse."""
120
+ //| ...
122
121
STATIC mp_obj_t audioio_audioout_deinit (mp_obj_t self_in ) {
123
122
audioio_audioout_obj_t * self = MP_OBJ_TO_PTR (self_in );
124
123
common_hal_audioio_audioout_deinit (self );
@@ -131,17 +130,15 @@ STATIC void check_for_deinit(audioio_audioout_obj_t *self) {
131
130
raise_deinited_error ();
132
131
}
133
132
}
134
- //| .. method:: __enter__()
135
- //|
136
- //| No-op used by Context Managers.
137
- //|
133
+ //| def __enter__(self, ) -> Any:
134
+ //| """No-op used by Context Managers."""
135
+ //| ...
138
136
// Provided by context manager helper.
139
137
140
- //| .. method:: __exit__()
141
- //|
142
- //| Automatically deinitializes the hardware when exiting a context. See
143
- //| :ref:`lifetime-and-contextmanagers` for more info.
144
- //|
138
+ //| def __exit__(self, ) -> Any:
139
+ //| """Automatically deinitializes the hardware when exiting a context. See
140
+ //| :ref:`lifetime-and-contextmanagers` for more info."""
141
+ //| ...
145
142
STATIC mp_obj_t audioio_audioout_obj___exit__ (size_t n_args , const mp_obj_t * args ) {
146
143
(void )n_args ;
147
144
common_hal_audioio_audioout_deinit (args [0 ]);
@@ -150,17 +147,16 @@ STATIC mp_obj_t audioio_audioout_obj___exit__(size_t n_args, const mp_obj_t *arg
150
147
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (audioio_audioout___exit___obj , 4 , 4 , audioio_audioout_obj___exit__ );
151
148
152
149
153
- //| .. 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.
154
153
//|
155
- //| Plays the sample once when loop=False and continuously when loop=True.
156
- //| Does not block. Use `playing` to block.
157
- //|
158
- //| Sample must be an `audiocore.WaveFile`, `audiocore.RawSample`, or `audiomixer.Mixer`.
159
- //|
160
- //| The sample itself should consist of 16 bit samples. Microcontrollers with a lower output
161
- //| resolution will use the highest order bits to output. For example, the SAMD21 has a 10 bit
162
- //| DAC that ignores the lowest 6 bits when playing 16 bit samples.
154
+ //| Sample must be an `audiocore.WaveFile`, `audiocore.RawSample`, or `audiomixer.Mixer`.
163
155
//|
156
+ //| The sample itself should consist of 16 bit samples. Microcontrollers with a lower output
157
+ //| resolution will use the highest order bits to output. For example, the SAMD21 has a 10 bit
158
+ //| DAC that ignores the lowest 6 bits when playing 16 bit samples."""
159
+ //| ...
164
160
STATIC mp_obj_t audioio_audioout_obj_play (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
165
161
enum { ARG_sample , ARG_loop };
166
162
static const mp_arg_t allowed_args [] = {
@@ -179,10 +175,9 @@ STATIC mp_obj_t audioio_audioout_obj_play(size_t n_args, const mp_obj_t *pos_arg
179
175
}
180
176
MP_DEFINE_CONST_FUN_OBJ_KW (audioio_audioout_play_obj , 1 , audioio_audioout_obj_play );
181
177
182
- //| .. method:: stop()
183
- //|
184
- //| Stops playback and resets to the start of the sample.
185
- //|
178
+ //| def stop(self, ) -> Any:
179
+ //| """Stops playback and resets to the start of the sample."""
180
+ //| ...
186
181
STATIC mp_obj_t audioio_audioout_obj_stop (mp_obj_t self_in ) {
187
182
audioio_audioout_obj_t * self = MP_OBJ_TO_PTR (self_in );
188
183
check_for_deinit (self );
@@ -191,10 +186,9 @@ STATIC mp_obj_t audioio_audioout_obj_stop(mp_obj_t self_in) {
191
186
}
192
187
MP_DEFINE_CONST_FUN_OBJ_1 (audioio_audioout_stop_obj , audioio_audioout_obj_stop );
193
188
194
- //| .. attribute:: playing
195
- //|
196
- //| True when an audio sample is being output even if `paused`. (read-only)
197
- //|
189
+ //| playing: Any =
190
+ //| """True when an audio sample is being output even if `paused`. (read-only)"""
191
+ //| ...
198
192
STATIC mp_obj_t audioio_audioout_obj_get_playing (mp_obj_t self_in ) {
199
193
audioio_audioout_obj_t * self = MP_OBJ_TO_PTR (self_in );
200
194
check_for_deinit (self );
@@ -209,10 +203,9 @@ const mp_obj_property_t audioio_audioout_playing_obj = {
209
203
(mp_obj_t )& mp_const_none_obj },
210
204
};
211
205
212
- //| .. method:: pause()
213
- //|
214
- //| Stops playback temporarily while remembering the position. Use `resume` to resume playback.
215
- //|
206
+ //| def pause(self, ) -> Any:
207
+ //| """Stops playback temporarily while remembering the position. Use `resume` to resume playback."""
208
+ //| ...
216
209
STATIC mp_obj_t audioio_audioout_obj_pause (mp_obj_t self_in ) {
217
210
audioio_audioout_obj_t * self = MP_OBJ_TO_PTR (self_in );
218
211
check_for_deinit (self );
@@ -225,10 +218,9 @@ STATIC mp_obj_t audioio_audioout_obj_pause(mp_obj_t self_in) {
225
218
}
226
219
MP_DEFINE_CONST_FUN_OBJ_1 (audioio_audioout_pause_obj , audioio_audioout_obj_pause );
227
220
228
- //| .. method:: resume()
229
- //|
230
- //| Resumes sample playback after :py:func:`pause`.
231
- //|
221
+ //| def resume(self, ) -> Any:
222
+ //| """Resumes sample playback after :py:func:`pause`."""
223
+ //| ...
232
224
STATIC mp_obj_t audioio_audioout_obj_resume (mp_obj_t self_in ) {
233
225
audioio_audioout_obj_t * self = MP_OBJ_TO_PTR (self_in );
234
226
check_for_deinit (self );
@@ -241,10 +233,9 @@ STATIC mp_obj_t audioio_audioout_obj_resume(mp_obj_t self_in) {
241
233
}
242
234
MP_DEFINE_CONST_FUN_OBJ_1 (audioio_audioout_resume_obj , audioio_audioout_obj_resume );
243
235
244
- //| .. attribute:: paused
245
- //|
246
- //| True when playback is paused. (read-only)
247
- //|
236
+ //| paused: Any =
237
+ //| """True when playback is paused. (read-only)"""
238
+ //| ...
248
239
STATIC mp_obj_t audioio_audioout_obj_get_paused (mp_obj_t self_in ) {
249
240
audioio_audioout_obj_t * self = MP_OBJ_TO_PTR (self_in );
250
241
check_for_deinit (self );
0 commit comments