Skip to content

Commit 24bd949

Browse files
docs: Update Recording & Playback based on the latest implementation.
1 parent eef81c0 commit 24bd949

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

docs/audio.rst

+33-13
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,6 @@ Functions
7272
7373
Stops all audio playback.
7474

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-
8275

8376
Built-in sounds **V2**
8477
======================
@@ -230,12 +223,38 @@ AudioFrame
230223
==========
231224

232225
.. py:class::
233-
AudioFrame(size=32)
226+
AudioFrame(duration=-1, rate=7812)
234227

235-
An ``AudioFrame`` object is a list of samples each of which is an unsigned
228+
An ``AudioFrame`` object is a list of samples, each of which is an unsigned
236229
byte (whole number between 0 and 255).
237230

238-
:param size: How many samples to contain in this instance.
231+
The number of samples in an AudioFrame will depend on the
232+
``rate`` (number of samples per second) and ``duration`` parameters.
233+
The total number of samples will always be a round up multiple of 32.
234+
235+
On micro:bit V1 the constructor does not take any arguments,
236+
and an AudioFrame instance is always 32 bytes.
237+
238+
:param duration: (**V2**) Indicates how many milliseconds of audio this
239+
instance can store.
240+
:param rate: (**V2**) The sampling rate at which data will be stored
241+
via the microphone, or played via the ``audio.play()`` function.
242+
243+
.. py:function:: set_rate(sample_rate)
244+
245+
(**V2 only**) Configure the sampling rate associated with the data
246+
in the ``AudioFrame`` instance.
247+
248+
For recording from the microphone, increasing the sampling rate
249+
increases the sound quality, but reduces the length of audio it
250+
can store.
251+
During playback, increasing the sampling rate speeds up the sound
252+
and decreasing it slows it down.
253+
254+
.. py:function:: get_rate()
255+
256+
(**V2 only**) Return the configured sampling rate for this
257+
``AudioFrame`` instance.
239258

240259
.. py:function:: copyfrom(other)
241260
@@ -252,12 +271,13 @@ Technical Details
252271
It is just here in case you wanted to know how it works.
253272

254273
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
274+
(sequence, like list or tuple, or generator) of ``AudioFrame`` instances,
275+
The ``AudioFrame`` default playback rate is 7812 Hz, and the output is a
257276
a PWM signal at 32.5 kHz.
258277

259278
Each ``AudioFrame`` instance is 32 samples by default, but it can be
260-
configured to a different size via constructor.
279+
configured to a different size via constructor and the
280+
``AudioFrame.set_rate()`` method.
261281

262282
So, for example, playing 32 samples at 7812 Hz takes just over 4 milliseconds
263283
(1/7812.5 * 32 = 0.004096 = 4096 microseconds).

docs/microphone.rst

+15-20
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ then be played with the ``audio.play()`` function.
3636

3737
Audio sampling is the process of converting sound into a digital format.
3838
To do this, the microphone takes samples of the sound waves at regular
39-
intervals. How many samples are recorded per second is known as the
39+
intervals. The number of samples recorded per second is known as the
4040
"sampling rate", so recording at a higher sampling rate increases the sound
41-
quality, but as more samples are saved, it also takes more memory.
41+
quality, but as more samples are saved, it also consumes more memory.
4242

4343
The microphone sampling rate can be configured during sound recording via
44-
the ``rate`` argument in the ``record()`` and ``record_into()`` functions.
44+
the ``AudioFrame.rate()`` method functions.
4545

4646
At the other side, the audio playback sampling rate indicates how many samples
4747
are played per second. So if audio is played back with a higher sampling rate
@@ -56,24 +56,22 @@ increased or decreased? Let's try it out!::
5656

5757
from microbit import *
5858

59-
RECORDING_SAMPLING_RATE = 11000
60-
6159
while True:
6260
if pin_logo.is_touched():
6361
# Record and play back at the same rate
64-
my_recording = microphone.record(duration=3000, rate=RECORDING_SAMPLING_RATE)
62+
my_recording = microphone.record(duration=3000)
6563
audio.play(my_recording)
6664

6765
if button_a.is_pressed():
6866
# Play back at half the sampling rate
69-
my_recording = microphone.record(duration=3000, rate=RECORDING_SAMPLING_RATE)
70-
audio.set_rate(RECORDING_SAMPLING_RATE / 2)
67+
my_recording = microphone.record(duration=3000)
68+
my_recording.set_rate(my_recording.get_rate() // 2)
7169
audio.play(my_recording)
7270

7371
if button_b.is_pressed():
7472
# Play back at twice the sampling rate
75-
my_recording = microphone.record(duration=3000, rate=RECORDING_SAMPLING_RATE)
76-
audio.set_rate(RECORDING_SAMPLING_RATE * 2)
73+
my_recording = microphone.record(duration=3000)
74+
my_recording.set_rate(my_recording.get_rate() * 2)
7775
audio.play(my_recording)
7876

7977
sleep(200)
@@ -119,10 +117,10 @@ Functions
119117
* **return**: a representation of the sound pressure level in the range 0 to
120118
255.
121119

122-
.. py:function:: record(duration=3000, rate=7812, wait=True)
120+
.. py:function:: record(duration=3000, rate=7812)
123121
124-
Record sound for the amount of time indicated by ``duration`` at the
125-
sampling rate indicated by ``rate``.
122+
Record sound into an ``AudioFrame`` for the amount of time indicated by
123+
``duration`` at the sampling rate indicated by ``rate``.
126124

127125
The amount of memory consumed is directly related to the length of the
128126
recording and the sampling rate. The higher these values, the more memory
@@ -133,10 +131,8 @@ Functions
133131

134132
If there isn't enough memory available a ``MemoryError`` will be raised.
135133

136-
:param duration: How much time to record in milliseconds.
134+
:param duration: How long to record in milliseconds.
137135
:param rate: Number of samples to capture per second.
138-
:param wait: When set to ``True`` it blocks until the recording is
139-
done, if it is set to ``False`` it will run in the background.
140136
:returns: An ``AudioFrame`` with the sound samples.
141137

142138
.. py:function:: record_into(buffer, rate=7812, wait=True)
@@ -242,11 +238,10 @@ An example of recording and playback with a display animation::
242238
"00000"
243239
)
244240

245-
RECORDING_RATE = 5500
246-
RECORDING_SECONDS = 5
247-
RECORDING_SIZE = RECORDING_RATE * RECORDING_SECONDS
241+
RECORDING_RATE = 3906
242+
RECORDING_MS = 5000
248243

249-
my_recording = audio.AudioBuffer(size=RECORDING_SIZE)
244+
my_recording = audio.AudioBuffer(duration=RECORDING_MS, rate=RECORDING_RATE)
250245

251246
while True:
252247
if button_a.is_pressed():

0 commit comments

Comments
 (0)