|
3 | 3 | #include "Arduino.h"
|
4 | 4 | #include "PDM.h"
|
5 | 5 | #include "OpenPDMFilter.h"
|
| 6 | +#include "mbed_interface.h" |
6 | 7 |
|
7 | 8 | extern "C" {
|
8 | 9 | #include "hardware/pio.h"
|
@@ -67,6 +68,18 @@ int PDMClass::begin(int channels, int sampleRate)
|
67 | 68 | int finalBufferLength = _doubleBuffer.availableForWrite() / sizeof(int16_t);
|
68 | 69 | _doubleBuffer.swap(0);
|
69 | 70 |
|
| 71 | + // The mic accepts an input clock from 1.2 to 3.25 Mhz |
| 72 | + // Setup the decimation factor accordingly |
| 73 | + if ((sampleRate * decimation * 2) > 3250000) { |
| 74 | + decimation = 64; |
| 75 | + } |
| 76 | + |
| 77 | + // Sanity check, abort if still over 3.25Mhz |
| 78 | + if ((sampleRate * decimation * 2) > 3250000) { |
| 79 | + mbed_error_printf("Sample rate too high, the mic would glitch\n"); |
| 80 | + mbed_die(); |
| 81 | + } |
| 82 | + |
70 | 83 | int rawBufferLength = RAW_BUFFER_SIZE / (decimation / 8);
|
71 | 84 | // Saturate number of samples. Remaining bytes are dropped.
|
72 | 85 | if (rawBufferLength > finalBufferLength) {
|
@@ -179,7 +192,11 @@ void PDMClass::IrqHandler(bool halftranfer)
|
179 | 192 | }
|
180 | 193 |
|
181 | 194 | // fill final buffer with PCM samples
|
182 |
| - Open_PDM_Filter_128(rawBuffer[rawBufferIndex], finalBuffer, 1, &filter); |
| 195 | + if (filter.Decimation == 128) { |
| 196 | + Open_PDM_Filter_128(rawBuffer[rawBufferIndex], finalBuffer, 1, &filter); |
| 197 | + } else { |
| 198 | + Open_PDM_Filter_64(rawBuffer[rawBufferIndex], finalBuffer, 1, &filter); |
| 199 | + } |
183 | 200 |
|
184 | 201 | if (cutSamples) {
|
185 | 202 | memset(finalBuffer, 0, cutSamples);
|
|
0 commit comments