| 1 | // Adafruit Circuit Playground microphone library |
|---|
| 2 | // by Phil Burgess / Paint Your Dragon. |
|---|
| 3 | // Fast Fourier transform section is derived from |
|---|
| 4 | // ELM-ChaN FFT library (see comments in ffft.S). |
|---|
| 5 | |
|---|
| 6 | #include <Arduino.h> |
|---|
| 7 | #include "Adafruit_CPlay_Mic.h" |
|---|
| 8 | |
|---|
| 9 | #if defined(ARDUINO_ARCH_SAMD) |
|---|
| 10 | |
|---|
| 11 | #define SAMPLERATE_HZ 22000 |
|---|
| 12 | #define DECIMATION 64 |
|---|
| 13 | |
|---|
| 14 | Adafruit_ZeroPDM Adafruit_CPlay_Mic::pdm = Adafruit_ZeroPDM(34, 35); |
|---|
| 15 | |
|---|
| 16 | // a windowed sinc filter for 44 khz, 64 samples |
|---|
| 17 | uint16_t sincfilter[DECIMATION] = {0, 2, 9, 21, 39, 63, 94, 132, 179, 236, 302, 379, 467, 565, 674, 792, 920, 1055, 1196, 1341, 1487, 1633, 1776, 1913, 2042, 2159, 2263, 2352, 2422, |
|---|
| 18 | 2474, 2506, 2516, 2506, 2474, 2422, 2352, 2263, 2159, 2042, 1913, 1776, 1633, 1487, 1341, 1196, 1055, 920, 792, 674, 565, 467, 379, 302, 236, 179, 132, 94, 63, 39, 21, 9, 2, 0, 0}; |
|---|
| 19 | |
|---|
| 20 | // a manual loop-unroller! |
|---|
| 21 | #define ADAPDM_REPEAT_LOOP_16(X) X X X X X X X X X X X X X X X X |
|---|
| 22 | |
|---|
| 23 | static bool pdmConfigured = false; |
|---|
| 24 | |
|---|
| 25 | #elif defined(ARDUINO_CIRCUITPLAY_NRF52840) |
|---|
| 26 | |
|---|
| 27 | #include <PDM.h> |
|---|
| 28 | // The default Circuit Playground Bluefruit pins |
|---|
| 29 | // data pin, clock pin, power pin (-1 if not used) |
|---|
| 30 | PDMClass PDM(33, 34, -1); |
|---|
| 31 | #define SAMPLERATE_HZ 16000 |
|---|
| 32 | |
|---|
| 33 | static bool pdmConfigured = false; |
|---|
| 34 | |
|---|
| 35 | #endif |
|---|
| 36 | |
|---|
| 37 | #define DC_OFFSET (1023 / 3) |
|---|
| 38 | #define NOISE_THRESHOLD 3 |
|---|
| 39 | |
|---|
| 40 | /**************************************************************************/ |
|---|
| 41 | /*! |
|---|
| 42 | @brief Reads ADC for given interval (in milliseconds, 1-65535). Uses ADC free-run mode w/polling on AVR. |
|---|
| 43 | Any currently-installed ADC interrupt handler will be temporarily |
|---|
| 44 | disabled while this runs. |
|---|
| 45 | @param ms the number of milliseconds to sample |
|---|
| 46 | @return max deviation from DC_OFFSET (e.g. 0-341) |
|---|
| 47 | @deprecated THIS FUNCTION IS DEPRECATED AND WILL BE REMOVED IN A FUTURE RELEASE. |
|---|
| 48 | please use soundPressureLevel(ms) instead |
|---|
| 49 | @note THIS FUNCTION IS DEPRECATED AND WILL BE REMOVED IN A FUTURE RELEASE. |
|---|
| 50 | please use soundPressureLevel(ms) instead |
|---|
| 51 | */ |
|---|
| 52 | /**************************************************************************/ |
|---|
| 53 | int Adafruit_CPlay_Mic::peak(uint16_t ms) { |
|---|
| 54 | int val = soundPressureLevel(ms); |
|---|
| 55 | val = map(val, 56, 130, 0, 350); |
|---|
| 56 | return constrain(val, 0, 350); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /**************************************************************************/ |
|---|
| 60 | /*! |
|---|
| 61 | @brief capture the passed number of samples and place them in buf. |
|---|
| 62 | @param buf the buffer to store the samples in |
|---|
| 63 | @param nSamples the number of samples to take |
|---|
| 64 | |
|---|
| 65 | @note ON AVR: Captures ADC audio samples at maximum speed supported by 32u4 (9615 Hz). |
|---|
| 66 | Ostensibly for FFT code (below), but might have other uses. Uses ADC |
|---|
| 67 | free-run mode w/polling. Any currently-installed ADC interrupt handler |
|---|
| 68 | will be temporarily disabled while this runs. No other interrupts are |
|---|
| 69 | disabled; as long as interrupt handlers are minor (e.g. Timer/Counter 0 |
|---|
| 70 | handling of millis() and micros()), this isn't likely to lose readings. |
|---|
| 71 | */ |
|---|
| 72 | /**************************************************************************/ |
|---|
| 73 | void Adafruit_CPlay_Mic::capture(int16_t *buf, uint16_t nSamples) { |
|---|
| 74 | #ifdef __AVR__ |
|---|
| 75 | uint8_t admux_save, adcsra_save, adcsrb_save, timsk0_save, channel; |
|---|
| 76 | int16_t adc; |
|---|
| 77 | |
|---|
| 78 | channel = analogPinToChannel(4); // Pin A4 to ADC channel |
|---|
| 79 | admux_save = ADMUX; // Save ADC config registers |
|---|
| 80 | adcsra_save = ADCSRA; |
|---|
| 81 | adcsrb_save = ADCSRB; |
|---|
| 82 | |
|---|
| 83 | // Init ADC free-run mode; f = ( 8MHz/prescaler ) / 13 cycles/conversion |
|---|
| 84 | ADCSRA = 0; // Stop ADC interrupt, if any |
|---|
| 85 | ADMUX = _BV(REFS0) | channel; // Aref=AVcc, channel sel, right-adj |
|---|
| 86 | ADCSRB = 0; // Free run mode, no high MUX bit |
|---|
| 87 | ADCSRA = _BV(ADEN) | // ADC enable |
|---|
| 88 | _BV(ADSC) | // ADC start |
|---|
| 89 | _BV(ADATE) | // Auto trigger |
|---|
| 90 | _BV(ADIF) | // Reset interrupt flag |
|---|
| 91 | _BV(ADPS2) | _BV(ADPS1); // 64:1 / 13 = 9615 Hz |
|---|
| 92 | |
|---|
| 93 | // ADC conversion-ready bit is polled for each sample rather than using |
|---|
| 94 | // an interrupt; avoids contention with application or other library |
|---|
| 95 | // using ADC ISR for other things (there can be only one) while still |
|---|
| 96 | // providing the speed & precise timing of free-run mode (a loop of |
|---|
| 97 | // analogRead() won't get you that). |
|---|
| 98 | for(uint16_t i=0; i<nSamples; i++) { |
|---|
| 99 | while(!(ADCSRA & _BV(ADIF))); // Wait for ADC result |
|---|
| 100 | adc = ADC; |
|---|
| 101 | ADCSRA |= _BV(ADIF); // Clear bit |
|---|
| 102 | // FFT requires signed inputs; ADC output is unsigned. DC offset is |
|---|
| 103 | // NOT 512 on Circuit Playground because it uses a 1.1V OpAmp input |
|---|
| 104 | // as the midpoint, and may swing asymmetrically on the high side. |
|---|
| 105 | // Sign-convert and then clip range to +/- DC_OFFSET. |
|---|
| 106 | if(adc <= (DC_OFFSET - NOISE_THRESHOLD)) { |
|---|
| 107 | adc -= DC_OFFSET; |
|---|
| 108 | } else if(adc >= (DC_OFFSET + NOISE_THRESHOLD)) { |
|---|
| 109 | adc -= DC_OFFSET; |
|---|
| 110 | if(adc > (DC_OFFSET * 2)) adc = DC_OFFSET * 2; |
|---|
| 111 | } else { |
|---|
| 112 | adc = 0; // Below noise threshold |
|---|
| 113 | } |
|---|
| 114 | buf[i] = adc; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | ADMUX = admux_save; // Restore ADC config |
|---|
| 118 | ADCSRB = adcsrb_save; |
|---|
| 119 | ADCSRA = adcsra_save; |
|---|
| 120 | (void)analogRead(A4); // Purge residue from ADC register |
|---|
| 121 | #elif defined(ARDUINO_ARCH_SAMD) |
|---|
| 122 | if(!pdmConfigured){ |
|---|
| 123 | pdm.begin(); |
|---|
| 124 | pdm.configure(SAMPLERATE_HZ * DECIMATION / 16, true); |
|---|
| 125 | pdmConfigured = true; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | int16_t *ptr = buf; |
|---|
| 129 | while(ptr < (buf + nSamples)){ |
|---|
| 130 | uint16_t runningsum = 0; |
|---|
| 131 | uint16_t *sinc_ptr = sincfilter; |
|---|
| 132 | |
|---|
| 133 | for (uint8_t samplenum=0; samplenum < (DECIMATION/16) ; samplenum++) { |
|---|
| 134 | uint16_t sample = pdm.read() & 0xFFFF; // we read 16 bits at a time, by default the low half |
|---|
| 135 | |
|---|
| 136 | ADAPDM_REPEAT_LOOP_16( // manually unroll loop: for (int8_t b=0; b<16; b++) |
|---|
| 137 | { |
|---|
| 138 | // start at the LSB which is the 'first' bit to come down the line, chronologically |
|---|
| 139 | // (Note we had to set I2S_SERCTRL_BITREV to get this to work, but saves us time!) |
|---|
| 140 | if (sample & 0x1) { |
|---|
| 141 | runningsum += *sinc_ptr; // do the convolution |
|---|
| 142 | } |
|---|
| 143 | sinc_ptr++; |
|---|
| 144 | sample >>= 1; |
|---|
| 145 | } |
|---|
| 146 | ) |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | // since we wait for the samples from I2S peripheral, we dont need to delay, we will 'naturally' |
|---|
| 150 | // wait the right amount of time between analog writes |
|---|
| 151 | //Serial.println(runningsum); |
|---|
| 152 | |
|---|
| 153 | runningsum /= 64 ; // convert 16 bit -> 10 bit |
|---|
| 154 | runningsum -= 512; // make it close to 0-offset signed |
|---|
| 155 | |
|---|
| 156 | *ptr++ = runningsum; |
|---|
| 157 | } |
|---|
| 158 | #elif defined(ARDUINO_CIRCUITPLAY_NRF52840) |
|---|
| 159 | if(!pdmConfigured){ |
|---|
| 160 | PDM.begin(1, SAMPLERATE_HZ); |
|---|
| 161 | |
|---|
| 162 | pdmConfigured = true; |
|---|
| 163 | } |
|---|
| 164 | #else |
|---|
| 165 | #error "no compatible architecture defined." |
|---|
| 166 | #endif |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /**************************************************************************/ |
|---|
| 170 | /*! |
|---|
| 171 | @brief Returns somewhat-calibrated sound pressure level. |
|---|
| 172 | @param ms Milliseconds to continuously sample microphone over, 10ms is a good start. |
|---|
| 173 | @returns Floating point Sound Pressure Level, tends to range from 40-120 db SPL |
|---|
| 174 | */ |
|---|
| 175 | /**************************************************************************/ |
|---|
| 176 | float Adafruit_CPlay_Mic::soundPressureLevel(uint16_t ms){ |
|---|
| 177 | float gain; |
|---|
| 178 | int16_t *ptr; |
|---|
| 179 | uint16_t len; |
|---|
| 180 | int16_t minVal = 52; |
|---|
| 181 | #ifdef __AVR__ |
|---|
| 182 | gain = 1.3; |
|---|
| 183 | len = 9.615 * ms; |
|---|
| 184 | #elif defined(ARDUINO_ARCH_SAMD) |
|---|
| 185 | gain = 9; |
|---|
| 186 | len = (float)(SAMPLERATE_HZ/1000) * ms; |
|---|
| 187 | #elif defined(ARDUINO_CIRCUITPLAY_NRF52840) |
|---|
| 188 | gain = 9; |
|---|
| 189 | len = (float)(SAMPLERATE_HZ/1000) * ms; |
|---|
| 190 | #else |
|---|
| 191 | #error "no compatible architecture defined." |
|---|
| 192 | #endif |
|---|
| 193 | int16_t data[len]; |
|---|
| 194 | capture(data, len); |
|---|
| 195 | |
|---|
| 196 | int16_t *end = data + len; |
|---|
| 197 | float pref = 0.00002; |
|---|
| 198 | |
|---|
| 199 | /******************************* |
|---|
| 200 | * REMOVE DC OFFSET |
|---|
| 201 | ******************************/ |
|---|
| 202 | int32_t avg = 0; |
|---|
| 203 | ptr = data; |
|---|
| 204 | while(ptr < end) avg += *ptr++; |
|---|
| 205 | avg = avg/len; |
|---|
| 206 | |
|---|
| 207 | ptr = data; |
|---|
| 208 | while(ptr < end) *ptr++ -= avg; |
|---|
| 209 | |
|---|
| 210 | /******************************* |
|---|
| 211 | * GET MAX VALUE |
|---|
| 212 | ******************************/ |
|---|
| 213 | |
|---|
| 214 | int16_t maxVal = 0; |
|---|
| 215 | ptr = data; |
|---|
| 216 | while(ptr < end){ |
|---|
| 217 | int32_t v = abs(*ptr++); |
|---|
| 218 | if(v > maxVal) maxVal = v; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | float conv = ((float)maxVal)/1023 * gain; |
|---|
| 222 | |
|---|
| 223 | /******************************* |
|---|
| 224 | * CALCULATE SPL |
|---|
| 225 | ******************************/ |
|---|
| 226 | conv = 20 * log10(conv/pref); |
|---|
| 227 | |
|---|
| 228 | if(isfinite(conv)) return conv; |
|---|
| 229 | else return minVal; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | /**************************************************************************/ |
|---|
| 233 | /*! |
|---|
| 234 | @brief 16 bit complex data type |
|---|
| 235 | */ |
|---|
| 236 | /**************************************************************************/ |
|---|
| 237 | typedef struct { |
|---|
| 238 | int16_t r; ///< real portion |
|---|
| 239 | int16_t i; ///< imaginary portion |
|---|
| 240 | } complex_t; |
|---|
| 241 | |
|---|
| 242 | extern "C" { // In ffft.S |
|---|
| 243 | void fft_input(const int16_t *, complex_t *), |
|---|
| 244 | fft_execute(complex_t *), |
|---|
| 245 | fft_output(complex_t *, uint16_t *); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | /**************************************************************************/ |
|---|
| 249 | /*! |
|---|
| 250 | @brief AVR ONLY: Performs one cycle of fast Fourier transform (FFT) with audio captured |
|---|
| 251 | from mic on A4. Output is 32 'bins,' each covering an equal range of |
|---|
| 252 | frequencies from 0 to 4800 Hz (i.e. 0-150 Hz, 150-300 Hz, 300-450, etc). |
|---|
| 253 | Needs about 450 bytes free RAM to operate. |
|---|
| 254 | @param spectrum the buffer to store the results in. Must be 32 bytes in length. |
|---|
| 255 | |
|---|
| 256 | @note THIS FUNCTION IS DEPRECATED AND WILL BE REMOVED IN A FUTURE RELEASE. |
|---|
| 257 | */ |
|---|
| 258 | /**************************************************************************/ |
|---|
| 259 | void Adafruit_CPlay_Mic::fft( |
|---|
| 260 | uint16_t *spectrum) { // Spectrum output buffer, uint16_t[32] |
|---|
| 261 | if(spectrum) { |
|---|
| 262 | int16_t capBuf[64]; // Audio capture buffer |
|---|
| 263 | complex_t butterfly[64]; // FFT "butterfly" buffer |
|---|
| 264 | |
|---|
| 265 | capture(capBuf, 64); // Collect mic data into capBuf |
|---|
| 266 | fft_input(capBuf, butterfly); // Samples -> complex #s |
|---|
| 267 | fft_execute(butterfly); // Process complex data |
|---|
| 268 | fft_output(butterfly, spectrum); // Complex -> spectrum (32 bins) |
|---|
| 269 | } |
|---|
| 270 | } |
|---|