1
1
"""Respond to sound using the built-in microphone (V2 only).
2
2
"""
3
3
4
- from typing import Optional , Tuple
4
+ from typing import Optional , Tuple , Union
5
5
from ..microbit import SoundEvent
6
- from ..microbit .audio import AudioFrame
6
+ from ..microbit .audio import AudioRecording , AudioTrack
7
7
8
8
def current_event () -> Optional [SoundEvent ]:
9
9
"""Get the last recorded sound event
@@ -88,12 +88,10 @@ def sound_level_db() -> int:
88
88
"""
89
89
...
90
90
91
- def record (duration : int , rate : int = 7812 ) -> AudioFrame :
92
- """Record sound into an ``AudioFrame `` for the amount of time indicated by
91
+ def record (duration : int , rate : int = 11_000 ) -> AudioRecording :
92
+ """Record sound into an ``AudioRecording `` for the amount of time indicated by
93
93
``duration`` at the sampling rate indicated by ``rate``.
94
94
95
- Example: ``my_frame = microphone.record(3000)``
96
-
97
95
The amount of memory consumed is directly related to the length of the
98
96
recording and the sampling rate. The higher these values, the more memory
99
97
it will use.
@@ -105,20 +103,23 @@ def record(duration: int, rate: int = 7812) -> AudioFrame:
105
103
106
104
:param duration: How long to record in milliseconds.
107
105
:param rate: Number of samples to capture per second.
108
- :return : An ``AudioFrame `` with the sound samples.
106
+ :returns : An ``AudioRecording `` with the sound samples.
109
107
"""
110
108
...
111
109
112
- def record_into (buffer : AudioFrame , rate : int = 7812 , wait : bool = True ) -> None :
113
- """Record sound into an existing ``AudioFrame `` until it is filled,
114
- or the ``stop_recording()`` function is called.
110
+ def record_into (buffer : Union [ AudioRecording , AudioTrack ] , wait : bool = True ) -> AudioTrack :
111
+ """Record sound into an existing ``AudioRecording `` or ``AudioTrack``
112
+ until it is filled, or the ``stop_recording()`` function is called.
115
113
116
- Example: ``microphone.record_into(my_frame)``
114
+ This function also returns an ``AudioTrack`` created from the provided
115
+ input buffer, which length matches the recording duration.
116
+ This is useful when recording with ``wait`` set to ``False``, and the
117
+ recording is stopped before the input buffer is filled.
117
118
118
- :param buffer: An ``AudioFrame`` to record sound.
119
- :param rate: Number of samples to capture per second.
119
+ :param buffer: ``AudioRecording`` or ``AudioTrack`` to record sound into.
120
120
:param wait: When set to ``True`` it blocks until the recording is
121
121
done, if it is set to ``False`` it will run in the background.
122
+ :returns: An ``AudioTrack`` which ends where the recording ended.
122
123
"""
123
124
...
124
125
0 commit comments