You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/doc/en/audio/play.md
+1-14Lines changed: 1 addition & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -118,17 +118,4 @@ Steps:
118
118
-`p.play(bytes(ctx))` plays the audio, `p` is the opened player object, `ctx` is the `PCM` data converted to type bytes
119
119
-`time.sleep_ms(10)` Here there is a loop to wait for the playback to complete, as the playback operation is performed asynchronously, and if the program exits early, then it may result in the audio not being played completely.
120
120
121
-
4. Done
122
-
123
-
### Other
124
-
125
-
The `Player` and `Recorder` modules have some `bugs` to be worked out, make sure they are created before other modules (`Camera` module, `Display` module, etc.). For example:
Copy file name to clipboardExpand all lines: docs/doc/en/audio/record.md
+62-53Lines changed: 62 additions & 53 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,34 +5,38 @@ update:
5
5
author: lxowalle
6
6
version: 1.0.0
7
7
content: Initial document
8
+
- date: 2025-01-24
9
+
author: lxowalle
10
+
version: 1.0.1
11
+
content:
12
+
- Update the usage instructions for the audio module.
8
13
---
9
14
10
15
## Introduction
11
16
12
-
This document provides the usage of audio recording and supports recording audio in `PCM` and `WAV` formats.
17
+
This document provides instructions on how to use the recording feature, supporting the recording of audio in both `PCM` and `WAV` formats.
18
+
19
+
`PCM (Pulse Code Modulation)` is a digital audio encoding format used to convert analog audio signals into digital signals. It is also the commonly required format for general hardware processing.
20
+
21
+
`WAV (Waveform Audio File Format)` is a popular audio file format. It is typically used to store uncompressed `PCM` audio data but also supports other encoding formats.
13
22
14
-
The `MaixCAM` has a microphone on board, so you can use the recording function directly.
23
+
The MaixCAM board comes with a built-in microphone, so you can directly use the recording feature.
15
24
16
25
### How to use
17
26
18
-
#### Getting `PCM` data
27
+
#### Record an Audio File in `PCM`/`WAV` Format
19
28
20
29
If you don't pass `path` when constructing a `Recorder` object, it will only record audio and not save it to a file, but you can save it to a file manually.
- Note that the default sample rate is 48k, the sample format is little-endian format - signed 16-bit, and the sample channel is 1. You can also customise the parameters like this `r = audio.Recorder(sample_rate=48000, format=audio.Format.FMT_S16_LE, channel = 1)`. So far only tested with sample rate 48000, format `FMT_S16_LE`, and number of sampling channels 1.
57
+
- Note that the default sample rate is 48k, the sample format is little-endian format - signed 16-bit, and the sample channel is 1. You can also customise the parameters like this `r = audio.Recorder(sample_rate=48000, format=audio.Format.FMT_S16_LE, channel = 1)`. So far only tested with sample rate 16000 and 48000, format `FMT_S16_LE`, and number of sampling channels 1.
54
58
55
-
-`r.volume(12)` is used to set the volume, the volume range is [0,100]
59
+
-`r.volume(100)` is used to set the volume, the volume range is [0,100]
56
60
57
61
3. Start recording
58
62
59
63
```python
60
-
data =r.record()
64
+
r.record(3000)
61
65
```
62
66
63
-
-`data` is `bytes` type data in `PCM` format that holds the currently recorded audio. The `PCM` format is set when initialising the `Recorder` object, see step 2. Note that if the recording is too fast and there is no data in the audio buffer, it is possible to return an empty `bytes` of data.
67
+
- Record audio for 3000 milliseconds.
68
+
69
+
- This function will block until the recording is complete.
64
70
65
-
4. Done, you can do voice processing on the `PCM` data returned by `r.record()` when doing your own applications.
71
+
4. Done
66
72
67
-
#### Records audio and saves it in `WAV`format.
73
+
#### Record an Audio File in `PCM`/`WAV`Format (Non-blocking)
68
74
69
-
If you pass `path` when constructing a `Recorder` object, the recorded audio will be saved to a `path` file, and you can also get the currently recorded `PCM` data via the `record` method. `path` only supports paths with `.pcm` and `.wav` suffixes, and the `record` method does not return `WAV` headers when recording `.wav`, it only returns `PCM` data.
75
+
When developing applications, if you need to record audio but do not want the recording function to occupy time for other applications, you can enable non-blocking mode.
#### Record audio and save to `WAV` format (blocking)
94
+
1. In non-blocking recording, you need to use the `reset(True)` function to enable the audio stream and the `reset(False)` function to stop the audio stream.
90
95
91
-
If the `record_ms` parameter is set during recording, recording audio will block until the time set by `record_ms`is reached, unit: ms.
96
+
2. The length of the audio data returned by `record` may not match the input time. For example, if you request to record `50ms` of audio but only `20ms` of data is ready in the audio buffer, then `record(50)` will only return `20ms` of audio data.
92
97
93
-
```python
94
-
from maix import audio, time, app
98
+
3. If you want the audio data returned by `record()` to match the input parameter, you can wait until the buffer has enough audio data before reading.
The above example will keep recording `5000`ms and save it to `WAV` format, during the recording period it will block in `record` method, note that `PCM` data will not be returned when `record` is set to `record_ms`.
107
+
Use the `get_remaining_frames()` function to get the number of remaining frames in the receive buffer. Note that this returns the number of frames, not bytes. Use `sample_rate()` to get the audio sample rate and calculate the actual number of frames to read.
106
108
107
-
###Other
109
+
#### Obtain Real-time `PCM` Audio Stream
108
110
109
-
The `Player` and `Recorder` modules have some `bugs` to be worked out, make sure they are created before other modules (`Camera` module, `Display` module, etc.). For example:
111
+
When developing applications that need to process audio data, you may not need to save files but only require the raw `PCM` stream. To achieve this, simply do not provide a path when creating the `Recorder`. Of course, you can also enable non-blocking mode.
0 commit comments