@@ -4,9 +4,9 @@ Microphone **V2**
4
4
.. py :module :: microbit.microphone
5
5
6
6
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.
10
10
11
11
.. image :: microphone.png
12
12
:width: 300px
@@ -28,6 +28,25 @@ accessible via variables in ``microbit.SoundEvent``:
28
28
- ``microbit.SoundEvent.LOUD ``: Represents the transition of sound events,
29
29
from ``quiet `` to ``loud `` like clapping or shouting.
30
30
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
+
31
50
Functions
32
51
=========
33
52
@@ -70,11 +89,60 @@ Functions
70
89
* **return **: a representation of the sound pressure level in the range 0 to
71
90
255.
72
91
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
+
101
+ A lower sampling rate will reduce memory consumption and sound quality.
102
+
103
+ If there isn't enough memory available a ``MemoryError `` will be raised.
104
+
105
+ :param duration: How much time to record in milliseconds.
106
+ :param rate: Number of samples to capture per second.
107
+ :param wait: When set to ``True `` it blocks until the recording is
108
+ done, if it is set to ``False `` it will run in the background.
109
+ :returns: An ``AudioBuffer ``, configured at the provided ``duration ``
110
+ and ``rate ``, with the sound data.
111
+
112
+ .. py :function :: record_into(buffer, rate = 11000 , wait = True )
113
+
114
+ Record sound into an existing ``AudioBuffer ``.
115
+
116
+ :param buffer: An ``AudioBuffer `` to record the microphone sound.
117
+ :param rate: Number of samples to capture per second.
118
+ :param wait: When set to ``True `` it blocks until the recording is
119
+ done, if it is set to ``False `` it will run in the background.
73
120
74
- Example
75
- =======
121
+ .. py :function :: is_recording()
76
122
77
- An example that runs through some of the functions of the microphone API::
123
+ :returns: ``True `` if the microphone is currently recording sound, or
124
+ ``False `` otherwise.
125
+
126
+ .. py :function :: stop_recording()
127
+
128
+ Stops an a recording running in the background.
129
+
130
+ .. py :function :: set_sensitivity(gain)
131
+
132
+ Configure the microphone sensitivity to one of these three levels:
133
+ ``microphone.SENSITIVITY_LOW ``, ``microphone.SENSITIVITY_MEDIUM ``,
134
+ ``microphone.SENSITIVITY_HIGH ``.
135
+
136
+ These constants correspond to a number, and any values between these
137
+ constants are valid arguments
138
+
139
+ :param gain: Microphone gain.
140
+
141
+ Examples
142
+ ========
143
+
144
+ An example that runs through some of the functions of the microphone
145
+ Sound Events API::
78
146
79
147
# Basic test for microphone. This test should update the display when
80
148
# Button A is pressed and a loud or quiet sound *is* heard, printing the
@@ -122,3 +190,41 @@ An example that runs through some of the functions of the microphone API::
122
190
display.clear()
123
191
print(sound)
124
192
sleep(500)
193
+
194
+
195
+ An example of recording and playback with a display animation::
196
+
197
+ from microbit import *
198
+
199
+ talk_open = Image(
200
+ "09090:"
201
+ "00000:"
202
+ "09990:"
203
+ "90009:"
204
+ "09990"
205
+ )
206
+ talk_closed = Image(
207
+ "09090:"
208
+ "00000:"
209
+ "00000:"
210
+ "99999:"
211
+ "00000"
212
+ )
213
+
214
+ my_recording = audio.AudioBuffer(duration=5000, rate=5500)
215
+
216
+ while True:
217
+ if button_a.is_pressed():
218
+ microphone.record_into(my_recording, rate=5500, wait=False)
219
+ display.show([talk_open, talk_closed], loop=True, wait=False, delay=150)
220
+ while button_a.is_pressed():
221
+ sleep(50)
222
+ display.show(mouth_open, loop=False) # workaround issue #150
223
+ display.clear()
224
+ if button_b.is_pressed():
225
+ audio.play(my_recording, wait=False)
226
+ while audio.is_playing():
227
+ x = accelerometer.get_x()
228
+ my_recording.rate = scale(x, (-1000, 1000), (2250, 11000))
229
+ sleep(50)
230
+ sleep(100)
0 commit comments