@@ -36,12 +36,12 @@ then be played with the ``audio.play()`` function.
36
36
37
37
Audio sampling is the process of converting sound into a digital format.
38
38
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
40
40
"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.
42
42
43
43
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.
45
45
46
46
At the other side, the audio playback sampling rate indicates how many samples
47
47
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!::
56
56
57
57
from microbit import *
58
58
59
- RECORDING_SAMPLING_RATE = 11000
60
-
61
59
while True:
62
60
if pin_logo.is_touched():
63
61
# 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)
65
63
audio.play(my_recording)
66
64
67
65
if button_a.is_pressed():
68
66
# 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)
71
69
audio.play(my_recording)
72
70
73
71
if button_b.is_pressed():
74
72
# 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)
77
75
audio.play(my_recording)
78
76
79
77
sleep(200)
@@ -141,10 +139,10 @@ Functions
141
139
142
140
:return: A representation of the sound pressure level in the range 0 to 255.
143
141
144
- .. py :function :: record(duration = 3000 , rate = 7812 , wait = True )
142
+ .. py :function :: record(duration = 3000 , rate = 7812 )
145
143
146
- Record sound for the amount of time indicated by `` duration `` at the
147
- sampling rate indicated by ``rate ``.
144
+ Record sound into an `` AudioFrame `` for the amount of time indicated by
145
+ `` duration `` at the sampling rate indicated by ``rate ``.
148
146
149
147
The amount of memory consumed is directly related to the length of the
150
148
recording and the sampling rate. The higher these values, the more memory
@@ -155,10 +153,8 @@ Functions
155
153
156
154
If there isn't enough memory available a ``MemoryError `` will be raised.
157
155
158
- :param duration: How much time to record in milliseconds.
156
+ :param duration: How long to record in milliseconds.
159
157
:param rate: Number of samples to capture per second.
160
- :param wait: When set to ``True `` it blocks until the recording is
161
- done, if it is set to ``False `` it will run in the background.
162
158
:returns: An ``AudioFrame `` with the sound samples.
163
159
164
160
.. py :function :: record_into(buffer, rate = 7812 , wait = True )
@@ -264,11 +260,10 @@ An example of recording and playback with a display animation::
264
260
"00000"
265
261
)
266
262
267
- RECORDING_RATE = 5500
268
- RECORDING_SECONDS = 5
269
- RECORDING_SIZE = RECORDING_RATE * RECORDING_SECONDS
263
+ RECORDING_RATE = 3906
264
+ RECORDING_MS = 5000
270
265
271
- my_recording = audio.AudioBuffer(size=RECORDING_SIZE )
266
+ my_recording = audio.AudioBuffer(duration=RECORDING_MS, rate=RECORDING_RATE )
272
267
273
268
while True:
274
269
if button_a.is_pressed():
0 commit comments