@@ -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+
3150Functions
3251=========
3352
@@ -91,11 +110,60 @@ Functions
91110
92111 :return: A representation of the sound pressure level in the range 0 to 255.
93112
113+ .. py :function :: record(duration = 3000 , rate = 11000 , wait = True )
114+
115+ Record sound for the amount of time indicated by ``duration `` at the
116+ sampling rate indicated by ``rate ``.
117+
118+ The amount of memory consumed is directly related to the length of the
119+ recording and the sampling rate. The higher these values, the more memory
120+ it will use.
121+
122+ A lower sampling rate will reduce memory consumption and sound quality.
123+
124+ If there isn't enough memory available a ``MemoryError `` will be raised.
125+
126+ :param duration: How much time to record in milliseconds.
127+ :param rate: Number of samples to capture per second.
128+ :param wait: When set to ``True `` it blocks until the recording is
129+ done, if it is set to ``False `` it will run in the background.
130+ :returns: An ``AudioBuffer ``, configured at the provided ``duration ``
131+ and ``rate ``, with the sound data.
132+
133+ .. py :function :: record_into(buffer, rate = 11000 , wait = True )
134+
135+ Record sound into an existing ``AudioBuffer ``.
136+
137+ :param buffer: An ``AudioBuffer `` to record the microphone sound.
138+ :param rate: Number of samples to capture per second.
139+ :param wait: When set to ``True `` it blocks until the recording is
140+ done, if it is set to ``False `` it will run in the background.
94141
95- Example
96- =======
142+ .. py :function :: is_recording()
97143
98- An example that runs through some of the functions of the microphone API::
144+ :returns: ``True `` if the microphone is currently recording sound, or
145+ ``False `` otherwise.
146+
147+ .. py :function :: stop_recording()
148+
149+ Stops an a recording running in the background.
150+
151+ .. py :function :: set_sensitivity(gain)
152+
153+ Configure the microphone sensitivity to one of these three levels:
154+ ``microphone.SENSITIVITY_LOW ``, ``microphone.SENSITIVITY_MEDIUM ``,
155+ ``microphone.SENSITIVITY_HIGH ``.
156+
157+ These constants correspond to a number, and any values between these
158+ constants are valid arguments
159+
160+ :param gain: Microphone gain.
161+
162+ Examples
163+ ========
164+
165+ An example that runs through some of the functions of the microphone
166+ Sound Events API::
99167
100168 # Basic test for microphone. This test should update the display when
101169 # Button A is pressed and a loud or quiet sound *is* heard, printing the
@@ -143,3 +211,41 @@ An example that runs through some of the functions of the microphone API::
143211 display.clear()
144212 print(sound)
145213 sleep(500)
214+
215+
216+ An example of recording and playback with a display animation::
217+
218+ from microbit import *
219+
220+ talk_open = Image(
221+ "09090:"
222+ "00000:"
223+ "09990:"
224+ "90009:"
225+ "09990"
226+ )
227+ talk_closed = Image(
228+ "09090:"
229+ "00000:"
230+ "00000:"
231+ "99999:"
232+ "00000"
233+ )
234+
235+ my_recording = audio.AudioBuffer(duration=5000, rate=5500)
236+
237+ while True:
238+ if button_a.is_pressed():
239+ microphone.record_into(my_recording, rate=5500, wait=False)
240+ display.show([talk_open, talk_closed], loop=True, wait=False, delay=150)
241+ while button_a.is_pressed():
242+ sleep(50)
243+ display.show(mouth_open, loop=False) # workaround issue #150
244+ display.clear()
245+ if button_b.is_pressed():
246+ audio.play(my_recording, wait=False)
247+ while audio.is_playing():
248+ x = accelerometer.get_x()
249+ my_recording.rate = scale(x, (-1000, 1000), (2250, 11000))
250+ sleep(50)
251+ sleep(100)
0 commit comments