|
| 1 | +PDM Unit |
| 2 | +========== |
| 3 | + |
| 4 | +.. sku: U089 |
| 5 | +
|
| 6 | +.. include:: ../refs/unit.pdm.ref |
| 7 | + |
| 8 | +This is the driver library of PDM Unit, which is provides a set of methods to control the PDM microphone. Through the |
| 9 | +I2S interface, the module can record audio data and save it as WAV files. |
| 10 | + |
| 11 | +Support the following products: |
| 12 | + |
| 13 | + |PDM| |
| 14 | + |
| 15 | + |
| 16 | +UiFlow2 Example |
| 17 | +--------------- |
| 18 | + |
| 19 | +record voice and play voice |
| 20 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 21 | + |
| 22 | +Open the |pdm_cores3_example.m5f2| project in UiFlow2. |
| 23 | + |
| 24 | +This example records voice and plays voice. |
| 25 | + |
| 26 | +UiFlow2 Code Block: |
| 27 | + |
| 28 | + |example.png| |
| 29 | + |
| 30 | +Example output: |
| 31 | + |
| 32 | + None |
| 33 | + |
| 34 | +MicroPython Example |
| 35 | +------------------- |
| 36 | + |
| 37 | +record voice and play voice |
| 38 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 39 | + |
| 40 | +This example records voice and plays voice. |
| 41 | + |
| 42 | +MicroPython Code Block: |
| 43 | + |
| 44 | + .. literalinclude:: ../../../examples/unit/pdm/pdm_cores3_example.py |
| 45 | + :language: python |
| 46 | + :linenos: |
| 47 | + |
| 48 | +Example output: |
| 49 | + |
| 50 | + None |
| 51 | + |
| 52 | + |
| 53 | +**API** |
| 54 | +------- |
| 55 | + |
| 56 | +PDMUnit |
| 57 | +^^^^^^^^^ |
| 58 | + |
| 59 | +.. autoclass:: unit.pdm.PDMUnit |
| 60 | + :members: |
| 61 | + |
| 62 | + .. py:method:: begin() |
| 63 | +
|
| 64 | + Initialize the PDM microphone. |
| 65 | + |
| 66 | + :return: Returns True if initialization was successful, False otherwise. |
| 67 | + :rtype: bool |
| 68 | + |
| 69 | + UiFlow2 Code Block: |
| 70 | + |
| 71 | + |begin.png| |
| 72 | + |
| 73 | + MicroPython Code Block: |
| 74 | + |
| 75 | + .. code-block:: python |
| 76 | +
|
| 77 | + pdm.begin() |
| 78 | +
|
| 79 | + .. py:method:: end() |
| 80 | +
|
| 81 | + Stop the PDM microphone. |
| 82 | + |
| 83 | + UiFlow2 Code Block: |
| 84 | + |
| 85 | + |end.png| |
| 86 | + |
| 87 | + MicroPython Code Block: |
| 88 | + |
| 89 | + .. code-block:: python |
| 90 | +
|
| 91 | + pdm.end() |
| 92 | +
|
| 93 | + .. py:method:: record(buffer, sample_rate=16000, stereo=False) |
| 94 | +
|
| 95 | + Record audio data into the provided buffer. |
| 96 | + |
| 97 | + :param bytearray buffer: Buffer to store the recorded audio data |
| 98 | + :param int sample_rate: Sample rate in Hz (default: 16000) |
| 99 | + :param bool stereo: True for stereo recording, False for mono (default: False) |
| 100 | + :return: True if recording started successfully |
| 101 | + :rtype: bool |
| 102 | + |
| 103 | + UiFlow2 Code Block: |
| 104 | + |
| 105 | + |record.png| |
| 106 | + |
| 107 | + MicroPython Code Block: |
| 108 | + |
| 109 | + .. code-block:: python |
| 110 | +
|
| 111 | + rec_data = bytearray(16000 * 5) # 5 seconds buffer |
| 112 | + pdm.record(rec_data, 16000, False) |
| 113 | +
|
| 114 | + .. py:method:: isRecording() |
| 115 | +
|
| 116 | + Check if recording is in progress. |
| 117 | + |
| 118 | + :return: Returns the number of bytes recorded. |
| 119 | + |
| 120 | + * ``0`` - Not recording |
| 121 | + * ``1`` - Recording (Queue has available space) |
| 122 | + * ``2`` - Recording (Queue is full) |
| 123 | + |
| 124 | + :rtype: int |
| 125 | + |
| 126 | + UiFlow2 Code Block: |
| 127 | + |
| 128 | + |isRecording.png| |
| 129 | + |
| 130 | + MicroPython Code Block: |
| 131 | + |
| 132 | + .. code-block:: python |
| 133 | +
|
| 134 | + pdm.isRecording() |
| 135 | +
|
| 136 | + .. py:method:: recordWavFile(path, rate=16000, time=5, stereo=False) |
| 137 | +
|
| 138 | + Record audio directly to a WAV file. |
| 139 | + |
| 140 | + :param str path: Path to save the WAV file |
| 141 | + :param int rate: Sample rate in Hz (default: 16000) |
| 142 | + :param int time: Recording duration in seconds (default: 5) |
| 143 | + :param bool stereo: True for stereo recording, False for mono (default: False) |
| 144 | + :return: True if recording was successful |
| 145 | + :rtype: bool |
| 146 | + |
| 147 | + UiFlow2 Code Block: |
| 148 | + |
| 149 | + |recordWavFile.png| |
| 150 | + |
| 151 | + MicroPython Code Block: |
| 152 | + |
| 153 | + .. code-block:: python |
| 154 | +
|
| 155 | + pdm.recordWavFile("/sd/test.wav", 16000, 5, False) |
| 156 | +
|
| 157 | + .. py:method:: config(**kwargs) |
| 158 | +
|
| 159 | + Configure the PDM microphone parameters. |
| 160 | + |
| 161 | + :param kwargs: Configuration parameters |
| 162 | + |
| 163 | + - pin_data_in: Data input pin |
| 164 | + - pin_ws: Word select pin |
| 165 | + - sample_rate: Sample rate in Hz |
| 166 | + - stereo: Stereo mode |
| 167 | + - over_sampling: Over sampling rate |
| 168 | + - noise_filter_level: Noise filter level |
| 169 | + - magnification: Audio magnification |
| 170 | + - dma_buf_len: DMA buffer length |
| 171 | + - dma_buf_count: DMA buffer count |
| 172 | + - task_priority: Task priority |
| 173 | + - task_pinned_core: Task core pinning |
| 174 | + - i2s_port: I2S port number |
| 175 | + |
| 176 | + UiFlow2 Code Block: |
| 177 | + |
| 178 | + |get_config_boolean.png| |
| 179 | + |
| 180 | + |get_config_int.png| |
| 181 | + |
| 182 | + |set_config_boolean.png| |
| 183 | + |
| 184 | + |set_config_int.png| |
| 185 | + |
| 186 | + MicroPython Code Block: |
| 187 | + |
| 188 | + .. code-block:: python |
| 189 | +
|
| 190 | + pdm.config( |
| 191 | + dma_buf_count=3, |
| 192 | + dma_buf_len=256, |
| 193 | + over_sampling=2, |
| 194 | + noise_filter_level=0, |
| 195 | + sample_rate=16000, |
| 196 | + pin_data_in=1, |
| 197 | + pin_ws=2, |
| 198 | + pin_bck=-1, |
| 199 | + pin_mck=-1, |
| 200 | + use_adc=False, |
| 201 | + stereo=False, |
| 202 | + magnification=1, |
| 203 | + task_priority=2, |
| 204 | + task_pinned_core=255, |
| 205 | + i2s_port=i2s_port, |
| 206 | + ) |
0 commit comments