@@ -12,7 +12,7 @@ a speaker to pin 0 and GND on the edge connector to hear the sounds.
12
12
The ``audio `` module can be imported as ``import audio `` or accessed via
13
13
the ``microbit `` module as ``microbit.audio ``.
14
14
15
- There are four different kinds of audio sources that can be played using the
15
+ There are three different kinds of audio sources that can be played using the
16
16
:py:meth: `audio.play ` function:
17
17
18
18
1. `Built in sounds <#built-in-sounds-v2 >`_ (**V2 **),
@@ -24,14 +24,9 @@ There are four different kinds of audio sources that can be played using the
24
24
my_effect = audio.SoundEffect(freq_start=400, freq_end=2500, duration=500)
25
25
audio.play(my_effect)
26
26
27
- 3. `AudioBuffer <#audiobuffer >`_ (**V2 **), a generic buffer for audio that can
28
- be used to record sound from the micro:bit V2 built-in microphone::
29
-
30
- my_audio_buffer = microphone.record()
31
- audio.play(my_audio_buffer)
32
-
33
- 4. `Audio Frames <#audioframe >`_, an iterable (like a list or a generator)
34
- of Audio Frames, which are lists of 32 samples with values from 0 to 255::
27
+ 3. `Audio Frames <#audioframe >`_, an instance or an iterable (like a list or
28
+ generator) of Audio Frames, which are lists of samples with values
29
+ from 0 to 255::
35
30
36
31
square_wave = audio.AudioFrame()
37
32
for i in range(16):
@@ -47,18 +42,16 @@ Functions
47
42
48
43
Play the audio source to completion.
49
44
50
- :param source: There are four types of data that can be used as a source:
45
+ :param source: There are three types of data that can be used as a source:
51
46
52
47
- ``Sound ``: The ``microbit `` module contains a list of
53
48
built-in sounds, e.g. ``audio.play(Sound.TWINKLE) ``. A full list can
54
49
be found in the `Built in sounds <#built-in-sounds-v2 >`_ section.
55
50
- ``SoundEffect ``: A sound effect, or an iterable of sound effects,
56
51
created via the :py:meth: `audio.SoundEffect ` class
57
- - ``AudioBuffer ``: An audio buffer, or an iterable of audio buffers,
58
- created via the :py:meth: `audio.AudioBuffer ` class or
59
- :doc: `microphone.record() <microphone >` function
60
- - ``AudioFrame ``: An iterable of ``AudioFrame `` instances as described
61
- in the `AudioFrame Technical Details <#id2 >`_ section
52
+ - ``AudioFrame ``: An instance or an iterable of ``AudioFrame ``
53
+ instances as described in the
54
+ `AudioFrame Technical Details <#technical-details >`_ section
62
55
63
56
:param wait: If ``wait `` is ``True ``, this function will block until the
64
57
source is exhausted.
@@ -79,6 +72,13 @@ Functions
79
72
80
73
Stops all audio playback.
81
74
75
+ .. py :function :: set_rate(sample_rate)
76
+
77
+ Changes the sampling rate of ``AudioFrame `` playback.
78
+ The default playback rate is 7812 samples per second.
79
+ Decreasing the playback sampling rate results in slowed down sound, and
80
+ increasing it speeds it up.
81
+
82
82
83
83
Built-in sounds **V2 **
84
84
======================
@@ -226,70 +226,16 @@ Sound Effects Example
226
226
:code: python
227
227
228
228
229
- Audio Buffer **V2 **
230
- ===================
231
-
232
- .. py :class ::
233
- AudioBuffer(duration=3000, rate=11000)
234
-
235
- Create a buffer to contain audio data and its sampling rate.
236
-
237
- The sampling rate is configured via constructor or instance attribute,
238
- and is used by the ``microphone.record_into() `` and
239
- ``audio.play() `` functions to configure the recording and playback rates.
240
-
241
- For audio recording, reducing the number of samples recorded per second
242
- will reduce the size of the data buffer, but also reduce the sound quality.
243
- And increasing the sampling rate increases the buffer size and sound
244
- quality.
245
-
246
- For audio playback, reducing the sampling rate compared with the recording
247
- rate, will slow down the audio. And increasing the playback rate
248
- will speed it up.
249
-
250
- The size of the buffer will be determined by the samples per second
251
- and the ``duration `` configured when creating a new instance.
252
-
253
- :param duration: Indicates in milliseconds, how much sound data the buffer
254
- can contained at the configured ``data_rate ``.
255
- :param rate: Sampling rate of for the data in the buffer. This value is
256
- used for recording and playback, and can be edited as an attribute.
257
-
258
- .. py :function :: copy()
259
-
260
- :returns: A copy of the Audio Buffer.
261
-
262
- .. py :attribute :: rate
263
-
264
- The sampling rate for the data inside the buffer.
265
- TODO: Indicate range of valid values here.
266
-
267
- Audio Buffer Example
268
- --------------------
269
-
270
- ::
271
-
272
- my_buffer = audio.AudioBuffer(duration=5000)
273
- microphone.record_into(my_buffer)
274
- audio.play(my_buffer)
275
-
276
- # A smaller buffer can be generated with the same duration by using
277
- # a lower sampling rate
278
- smaller_buffer = audio.AudioBuffer(duration=5000, rate=5500)
279
- microphone.record_into(my_buffer)
280
- audio.play(my_buffer)
281
-
282
-
283
229
AudioFrame
284
230
==========
285
231
286
232
.. py :class ::
287
- AudioFrame
233
+ AudioFrame(size=32)
288
234
289
- An ``AudioFrame `` object is a list of 32 samples each of which is an unsigned byte
290
- (whole number between 0 and 255).
235
+ An ``AudioFrame `` object is a list of samples each of which is an unsigned
236
+ byte (whole number between 0 and 255).
291
237
292
- It takes just over 4 ms to play a single frame .
238
+ :param size: How many samples to contain in this instance .
293
239
294
240
.. py :function :: copyfrom(other)
295
241
@@ -305,13 +251,19 @@ Technical Details
305
251
You don't need to understand this section to use the ``audio `` module.
306
252
It is just here in case you wanted to know how it works.
307
253
308
- The ``audio `` module can consumes an iterable (sequence, like list or tuple, or
309
- generator) of ``AudioFrame `` instances, each 32 samples at 7812.5 Hz, and uses
310
- linear interpolation to output a PWM signal at 32.5 kHz, which gives tolerable
311
- sound quality .
254
+ The ``audio.play() `` function can consume an instance or iterable
255
+ (sequence, like list or tuple, or generator) of ``AudioFrame `` instances.
256
+ Its default playback rate is 7812 Hz, and uses linear interpolation to output
257
+ a PWM signal at 32.5 kHz .
312
258
313
- The function ``play `` fully copies all data from each ``AudioFrame `` before it
314
- calls ``next() `` for the next frame, so a sound source can use the same
259
+ Each ``AudioFrame `` instance is 32 samples by default, but it can be
260
+ configured to a different size via constructor.
261
+
262
+ So, for example, playing 32 samples at 7812 Hz takes just over 4 milliseconds
263
+ (1/7812.5 * 32 = 0.004096 = 4096 microseconds).
264
+
265
+ The function ``play() `` fully copies all data from each ``AudioFrame `` before
266
+ it calls ``next() `` for the next frame, so a sound source can use the same
315
267
``AudioFrame `` repeatedly.
316
268
317
269
The ``audio `` module has an internal 64 sample buffer from which it reads
@@ -325,5 +277,8 @@ the buffer. This means that a sound source has under 4ms to compute the next
325
277
AudioFrame Example
326
278
------------------
327
279
280
+ Creating and populating ``AudioFrame `` iterables and generators with
281
+ different sound waveforms:
282
+
328
283
.. include :: ../examples/waveforms.py
329
284
:code: python
0 commit comments