@@ -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)
@@ -119,10 +117,10 @@ Functions
119
117
* **return **: a representation of the sound pressure level in the range 0 to
120
118
255.
121
119
122
- .. py :function :: record(duration = 3000 , rate = 7812 , wait = True )
120
+ .. py :function :: record(duration = 3000 , rate = 7812 )
123
121
124
- Record sound for the amount of time indicated by `` duration `` at the
125
- sampling rate indicated by ``rate ``.
122
+ Record sound into an `` AudioFrame `` for the amount of time indicated by
123
+ `` duration `` at the sampling rate indicated by ``rate ``.
126
124
127
125
The amount of memory consumed is directly related to the length of the
128
126
recording and the sampling rate. The higher these values, the more memory
@@ -133,10 +131,8 @@ Functions
133
131
134
132
If there isn't enough memory available a ``MemoryError `` will be raised.
135
133
136
- :param duration: How much time to record in milliseconds.
134
+ :param duration: How long to record in milliseconds.
137
135
:param rate: Number of samples to capture per second.
138
- :param wait: When set to ``True `` it blocks until the recording is
139
- done, if it is set to ``False `` it will run in the background.
140
136
:returns: An ``AudioFrame `` with the sound samples.
141
137
142
138
.. py :function :: record_into(buffer, rate = 7812 , wait = True )
@@ -242,11 +238,10 @@ An example of recording and playback with a display animation::
242
238
"00000"
243
239
)
244
240
245
- RECORDING_RATE = 5500
246
- RECORDING_SECONDS = 5
247
- RECORDING_SIZE = RECORDING_RATE * RECORDING_SECONDS
241
+ RECORDING_RATE = 3906
242
+ RECORDING_MS = 5000
248
243
249
- my_recording = audio.AudioBuffer(size=RECORDING_SIZE )
244
+ my_recording = audio.AudioBuffer(duration=RECORDING_MS, rate=RECORDING_RATE )
250
245
251
246
while True:
252
247
if button_a.is_pressed():
0 commit comments