Skip to content

Commit 588b493

Browse files
committed
1. makefile for mp to build the adf components
2. create audio module 3. create audio.player 4. create audio.recorder 5. examples for micropython
1 parent 40cb926 commit 588b493

24 files changed

+2251
-1
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ docs/_static/NotoSansSC-Regular.otf
4444
*.gcno
4545
coverage.info
4646
coverage_report/
47+
48+
.vscode

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "components/esp-sr"]
88
path = components/esp-sr
99
url = https://github.com/espressif/esp-sr.git
10+
[submodule "micropython_adf/micropython"]
11+
path = micropython_adf/micropython
12+
url = https://github.com/micropython/micropython.git

components/audio_stream/i2s_stream.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ typedef struct i2s_stream {
5151
bool use_alc;
5252
void *volume_handle;
5353
int volume;
54+
bool uninstall_drv;
5455
} i2s_stream_t;
5556

5657
static esp_err_t i2s_mono_fix(int bits, uint8_t *sbuff, uint32_t len)
@@ -145,7 +146,9 @@ static esp_err_t _i2s_open(audio_element_handle_t self)
145146
static esp_err_t _i2s_destroy(audio_element_handle_t self)
146147
{
147148
i2s_stream_t *i2s = (i2s_stream_t *)audio_element_getdata(self);
148-
i2s_driver_uninstall(i2s->config.i2s_port);
149+
if (i2s->uninstall_drv) {
150+
i2s_driver_uninstall(i2s->config.i2s_port);
151+
}
149152
audio_free(i2s);
150153
return ESP_OK;
151154
}
@@ -335,6 +338,7 @@ audio_element_handle_t i2s_stream_init(i2s_stream_cfg_t *config)
335338
i2s->type = config->type;
336339
i2s->use_alc = config->use_alc;
337340
i2s->volume = config->volume;
341+
i2s->uninstall_drv = config->uninstall_drv;
338342

339343
if (config->type == AUDIO_STREAM_READER) {
340344
cfg.read = _i2s_read;

components/audio_stream/include/i2s_stream.h

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typedef struct {
4848
int task_core; /*!< Task running in core (0 or 1) */
4949
int task_prio; /*!< Task priority (based on freeRTOS priority) */
5050
int multi_out_num; /*!< The number of multiple output */
51+
bool uninstall_drv; /*!< whether uninstall the i2s driver when stream destroyed*/
5152
} i2s_stream_cfg_t;
5253

5354
#define I2S_STREAM_TASK_STACK (3072+512)
@@ -78,6 +79,7 @@ typedef struct {
7879
.use_alc = false, \
7980
.volume = 0, \
8081
.multi_out_num = 0, \
82+
.uninstall_drv = true, \
8183
}
8284

8385

micropython_adf/README.md

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# Integrate the ADF components as parts of MicroPython
2+
3+
## Prepare
4+
5+
To use this integration, please read the [documents](https://github.com/micropython/micropython) of `MicroPython` and the [README](https://github.com/micropython/micropython/tree/master/ports/esp32) of `MicroPython port for ESP32` first.
6+
7+
## Build and flash
8+
9+
To build the project, please follow the steps below:
10+
11+
* Clone `ESP-ADF`, `ESP-IDF`, into your workspace and update all the submodules.
12+
* MicroPython support HASH 'f301170c7cb50fdb9250a6e4b89682f9c0e543cb'.
13+
* ESP-IDF support HASH '6ccb4cf5b7d1fdddb8c2492f9cbc926abaf230df'.
14+
* `export ADF_PATH={ your ESP-ADF's path }`
15+
* `export IDF_PATH={ your ESP-IDF's path }`
16+
* `cd ${ADF_PATH}/micropyton_adf/micropython`, and apply the patch of `${ADF_PATH}/micropyton_adf/mpmake.patch`.
17+
* select your audio board in `${ADF_PATH}/micropyton_adf/sdkconfig.adf`.
18+
* `cd ${ADF_PATH}/micropyton_adf/micropython/mpy-cross`, run `make -j8`.
19+
* `cd ${ADF_PATH}/micropyton_adf/micropython/ports/esp32`, run `make deploy -j8 BOARD=GENERIC_SPIRAM BAUD=921600 PORT={ your uart port }`.
20+
21+
## Libraries
22+
23+
### `audio` - player and recorder
24+
25+
This module implements player and recorder
26+
27+
- player: encapsulates the ADF esp_audio, support local and online resources.
28+
- recorder: record and encoding the voice into file.
29+
30+
NOTICE:
31+
> This a beta release as version '0.5-beta1',
32+
> Classes and methods may be changed by further release.
33+
34+
#### Constructors
35+
36+
- `class audio.player([state_callback])`
37+
38+
Create a player, `state_callback` is a monitor of player state, when state changed, this callback will be invoked.
39+
40+
```python
41+
def cb(state):
42+
print(state) #'{{'media_src': 0, 'err_msg': 0, 'status': 1}}', media_src is reserved.
43+
44+
p = audio.player(cb)
45+
# play music
46+
```
47+
48+
- `class audio.recorder()`
49+
50+
Create recorder.
51+
52+
```python
53+
r = audio.recorder()
54+
```
55+
56+
#### Functions
57+
58+
- `audio.verno()`
59+
60+
version of audio module
61+
62+
```python
63+
>>> audio.verno()
64+
'0.5-beta1'
65+
```
66+
67+
- `audio.mem_info()`
68+
69+
Show memory usage.
70+
71+
```Python
72+
>>> audio.mem_info()
73+
{'dram': 176596, 'inter': 212472, 'mem_total': 1197704}
74+
```
75+
76+
#### Constants
77+
78+
Audio error type:
79+
80+
- `audio.AUDIO_OK`: ESP_ERR_AUDIO_NO_ERROR
81+
- `audio.AUDIO_FAIL` : ESP_ERR_AUDIO_FAIL
82+
- `audio.AUDIO_NO_INPUT_STREAM` : ESP_ERR_AUDIO_NO_INPUT_STREAM
83+
- `audio.AUDIO_NO_OUTPUT_STREAM` : ESP_ERR_AUDIO_NO_OUTPUT_STREAM
84+
- `audio.AUDIO_NO_CODEC` : ESP_ERR_AUDIO_NO_CODEC
85+
- `audio.AUDIO_HAL_FAIL` : ESP_ERR_AUDIO_HAL_FAIL
86+
- `audio.AUDIO_MEMORY_LACK` : ESP_ERR_AUDIO_MEMORY_LACK
87+
- `audio.AUDIO_INVALID_URI` : ESP_ERR_AUDIO_INVALID_URI
88+
- `audio.AUDIO_INVALID_PARAMETER` : ESP_ERR_AUDIO_INVALID_PARAMETER
89+
- `audio.AUDIO_NOT_READY` : ESP_ERR_AUDIO_NOT_READY
90+
- `audio.AUDIO_TIMEOUT` : ESP_ERR_AUDIO_TIMEOUT
91+
- `audio.AUDIO_ALREADY_EXISTS` : ESP_ERR_AUDIO_ALREADY_EXISTS
92+
- `audio.AUDIO_LINK_FAIL` : ESP_ERR_AUDIO_LINK_FAIL
93+
- `audio.AUDIO_OPEN` : ESP_ERR_AUDIO_OPEN
94+
- `audio.AUDIO_INPUT` : ESP_ERR_AUDIO_INPUT
95+
- `audio.AUDIO_PROCESS` : ESP_ERR_AUDIO_PROCESS
96+
- `audio.AUDIO_OUTPUT` : ESP_ERR_AUDIO_OUTPUT
97+
- `audio.AUDIO_CLOSE` : ESP_ERR_AUDIO_CLOSE
98+
99+
### class `audio.player`
100+
101+
Audio player now can support `mp3`,`amr` and `wav`, if more types are needed, please add the decoder in function `audio_player_create`.
102+
103+
#### Methods
104+
105+
- `player.play(uri, pos=0, sync=False)`
106+
Play the `uri` from specified position. if `sync = True`, the thread will be suspended until the music finished. `uri` formated as "file://sdcard/test.wav" or "https://dl.espressif.com/dl/audio/ff-16b-2c-44100hz.mp3".
107+
108+
- `player.stop(termination=player.TERMINATION_NOW)`
109+
Stop the player
110+
111+
- `player.pause()`
112+
Pause the player
113+
114+
- `player.resume()`
115+
Resume the player
116+
117+
- `player.set_vol(vol)`
118+
Set the volume of the audio board.
119+
120+
- `player.get_vol()`
121+
Get the volume of the audio board.
122+
123+
- `player.get_state()`
124+
Get the state of the player.
125+
126+
```python
127+
>>> p = audio.player(None)
128+
>>> p.get_state()
129+
{'media_src': 0, 'err_msg': 0, 'status': 0}
130+
```
131+
132+
- `player.pos()`
133+
Get the position in bytes of currently played music.
134+
135+
- `player.time()`
136+
Get the position in microseconds of currently played music.
137+
138+
#### Constants
139+
140+
Player termination type:
141+
142+
- `TERMINATION_NOW`: TERMINATION_TYPE_NOW
143+
- `TERMINATION_DONE`: TERMINATION_TYPE_DONE
144+
145+
Player status type:
146+
147+
- `STATUS_UNKNOWN`: AUDIO_STATUS_UNKNOWN
148+
- `STATUS_RUNNING`: AUDIO_STATUS_RUNNING
149+
- `STATUS_PAUSED`: AUDIO_STATUS_PAUSED
150+
- `STATUS_STOPPED`: AUDIO_STATUS_STOPPED
151+
- `STATUS_FINISHED`: AUDIO_STATUS_FINISHED
152+
- `STATUS_ERROR`: AUDIO_STATUS_ERROR
153+
154+
### class `audio.recorder`
155+
156+
#### Methods
157+
158+
- `recorder.start(path, format=PCM, maxtime=0, endcb=None)`
159+
Start the recorder and save the voice into `path` with encoding the date as `format`.
160+
If `maxtime` > 0, recorder will stop automatic when the duration passed, `endcb` will be invoked if not `None`.
161+
If `maxtime` == 0, recorder will work until `recorder.stop()` is called.
162+
163+
- `recorder.stop()`
164+
Stop recording.
165+
166+
- `recorder.is_running()`
167+
Get the state of recorder, `Bool`.
168+
169+
#### Constants
170+
171+
format of final data:
172+
173+
- `recorder.PCM`
174+
- `recorder.AMR`
175+
- `recorder.MP3`
176+
- `recorder.WAV`

0 commit comments

Comments
 (0)