|
| 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