@@ -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,43 @@ 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
+ 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.
73
112
74
- Example
75
- =======
113
+ .. py :function :: is_recording()
76
114
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::
78
129
79
130
# Basic test for microphone. This test should update the display when
80
131
# 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::
122
173
display.clear()
123
174
print(sound)
124
175
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