Skip to content

Commit 52f230c

Browse files
docs: Add initial proposal for recording & playback API.
1 parent 1c3fc31 commit 52f230c

File tree

2 files changed

+147
-9
lines changed

2 files changed

+147
-9
lines changed

docs/audio.rst

+51-3
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,25 @@ a speaker to pin 0 and GND on the edge connector to hear the sounds.
1212
The ``audio`` module can be imported as ``import audio`` or accessed via
1313
the ``microbit`` module as ``microbit.audio``.
1414

15-
There are three different kinds of audio sources that can be played using the
15+
There are four different kinds of audio sources that can be played using the
1616
:py:meth:`audio.play` function:
1717

1818
1. `Built in sounds <#built-in-sounds-v2>`_ (**V2**),
1919
e.g. ``audio.play(Sound.HAPPY)``
20+
2021
2. `Sound Effects <#sound-effects-v2>`_ (**V2**), a way to create custom sounds
2122
by configuring its parameters::
2223

2324
my_effect = audio.SoundEffect(freq_start=400, freq_end=2500, duration=500)
2425
audio.play(my_effect)
2526

26-
3. `Audio Frames <#audioframe>`_, an iterable (like a list or a generator)
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)
2734
of Audio Frames, which are lists of 32 samples with values from 0 to 255::
2835

2936
square_wave = audio.AudioFrame()
@@ -40,13 +47,16 @@ Functions
4047
4148
Play the audio source to completion.
4249

43-
:param source: There are three types of data that can be used as a source:
50+
:param source: There are four types of data that can be used as a source:
4451

4552
- ``Sound``: The ``microbit`` module contains a list of
4653
built-in sounds, e.g. ``audio.play(Sound.TWINKLE)``. A full list can
4754
be found in the `Built in sounds <#built-in-sounds-v2>`_ section.
4855
- ``SoundEffect``: A sound effect, or an iterable of sound effects,
4956
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/recording>` function
5060
- ``AudioFrame``: An iterable of ``AudioFrame`` instances as described
5161
in the `AudioFrame Technical Details <#id2>`_ section
5262

@@ -215,6 +225,44 @@ Sound Effects Example
215225
.. include:: ../examples/soundeffects.py
216226
:code: python
217227

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 used by the ``microphone.record_into()`` and
238+
``audio.play()`` functions to configure the recording and playback rates.
239+
This value can be changed as an attribute to increase or decrease the
240+
recording quality or the playback speed.
241+
242+
:param duration: Indicates in milliseconds, how much sound data the buffer
243+
can contained at the configured ``data_rate``.
244+
:param rate: Sampling rate of for the data in the buffer. This value is
245+
used for recording and playback, and can be edited as an attribute.
246+
247+
.. py:function:: copy()
248+
249+
:returns: A copy of the Audio Buffer.
250+
251+
.. py:attribute:: rate
252+
253+
The sampling rate for the data inside the buffer.
254+
TODO: Indicate range of valid values here.
255+
256+
Audio Buffer Example
257+
--------------------
258+
259+
::
260+
261+
my_buffer = audio.AudioBuffer(duration=5000)
262+
microphone.record_into(my_buffer)
263+
audio.play(my_buffer)
264+
265+
218266
AudioFrame
219267
==========
220268

docs/microphone.rst

+96-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Microphone **V2**
44
.. py:module:: microbit.microphone
55
66
This object lets you access the built-in microphone available on the
7-
micro:bit **V2**. It can be used to respond to sound. The microphone input
8-
is located on the front of the board alongside a microphone activity LED,
9-
which is lit when the microphone is in use.
7+
micro:bit **V2**. It can be used to record and respond to sound.
8+
The microphone input is located on the front of the board alongside a
9+
microphone activity LED, which is lit when the microphone is in use.
1010

1111
.. image:: microphone.png
1212
:width: 300px
@@ -28,6 +28,25 @@ accessible via variables in ``microbit.SoundEvent``:
2828
- ``microbit.SoundEvent.LOUD``: Represents the transition of sound events,
2929
from ``quiet`` to ``loud`` like clapping or shouting.
3030

31+
Recording
32+
=========
33+
34+
TODO:
35+
* Describe the feature.
36+
* Indicate how the sampling rate relates to recording quality.
37+
* Indicate how changing the sampling rate on the fly affects playback speed.
38+
* What happens if the user changes the sampling rate while recording?
39+
40+
::
41+
42+
from microbit import *
43+
44+
while True:
45+
if button_a.is_pressed():
46+
my_recording = microphone.record(duration=5000)
47+
audio.play(my_recording)
48+
sleep(200)
49+
3150
Functions
3251
=========
3352

@@ -70,11 +89,43 @@ Functions
7089
* **return**: a representation of the sound pressure level in the range 0 to
7190
255.
7291

92+
.. py:function:: record(duration=3000, rate=11000, wait=True)
93+
94+
Record sound for the amount of time indicated by ``duration`` at the
95+
sampling rate indicated by ``rate``.
96+
97+
The amount of memory consumed is directly related to the length of the
98+
recording and the sampling rate. The higher these values, the more memory
99+
it will use.
100+
If there isn't enough memory available a ``MemoryError`` will be raised.
101+
102+
:param duration: How long to record in milliseconds.
103+
:param rate: Number of samples per second.
104+
:returns: An ``AudioBuffer``, configured at the provided ``duration``
105+
and ``rate``, with the sound data.
106+
107+
.. py:function:: record_into(buffer, wait=True)
108+
109+
Description.
110+
111+
:param buffer: An ``AudioBuffer`` to record the microphone sound.
73112

74-
Example
75-
=======
113+
.. py:function:: is_recording()
76114
77-
An example that runs through some of the functions of the microphone API::
115+
:returns: ``True`` if the microphone is currently recording sound, or
116+
``False`` otherwise.
117+
118+
.. py:function:: set_sensitivity(gain)
119+
120+
TODO: Decide if we want to admit values or create "3 levels".
121+
122+
:param gain: Description.
123+
124+
Examples
125+
========
126+
127+
An example that runs through some of the functions of the microphone
128+
Sound Events API::
78129

79130
# Basic test for microphone. This test should update the display when
80131
# Button A is pressed and a loud or quiet sound *is* heard, printing the
@@ -122,3 +173,42 @@ An example that runs through some of the functions of the microphone API::
122173
display.clear()
123174
print(sound)
124175
sleep(500)
176+
177+
178+
An example of recording and playback with an animation::
179+
180+
from microbit import *
181+
182+
talk_open = Image(
183+
"09090:"
184+
"00000:"
185+
"09990:"
186+
"90009:"
187+
"09990"
188+
)
189+
talk_closed = Image(
190+
"09090:"
191+
"00000:"
192+
"00000:"
193+
"99999:"
194+
"00000"
195+
)
196+
197+
my_recording = audio.AudioBuffer(duration=5000, rate=5500)
198+
199+
while True:
200+
if button_a.is_pressed():
201+
my_recording.rate = 5500
202+
microphone.record_into(my_recording, wait=False)
203+
display.show([talk_open, talk_closed], loop=True, wait=False, delay=150)
204+
while button_a.is_pressed():
205+
sleep(50)
206+
display.show(mouth_open, loop=False) # workaround issue #150
207+
display.clear()
208+
if button_b.is_pressed():
209+
audio.play(my_recording, wait=False)
210+
while audio.is_playing():
211+
x = accelerometer.get_x()
212+
my_recording.rate = scale(x, (-1000, 1000), (2250, 11000))
213+
sleep(50)
214+
sleep(100)

0 commit comments

Comments
 (0)