|
| 1 | +/* |
| 2 | + This example reads audio data from the connected PDM microphone, and prints |
| 3 | + out the RMS value of the samples to the Serial console. The Serial Plotter |
| 4 | + built into the Arduino IDE can be used to plot the audio data (Tools -> |
| 5 | + Serial Plotter) |
| 6 | +
|
| 7 | + Circuit: |
| 8 | + - Portenta Breakout |
| 9 | +
|
| 10 | + This example code is in the public domain. |
| 11 | +*/ |
| 12 | + |
| 13 | +#include <Arduino_PortentaBreakoutCarrier.h> |
| 14 | + |
| 15 | +// default number of output channels |
| 16 | +static const char channels = 1; |
| 17 | + |
| 18 | +// default PCM output frequency |
| 19 | +static const int frequency = 16000; |
| 20 | + |
| 21 | +// Buffer to read samples into, each sample is 16-bits. |
| 22 | +// Size in bytes must be a power of 2, grater or equal than PDM buffer size. |
| 23 | +short sampleBuffer[2048]; |
| 24 | + |
| 25 | +// Number of audio samples read |
| 26 | +volatile int samplesRead; |
| 27 | + |
| 28 | +void setup() { |
| 29 | + Serial.begin(9600); |
| 30 | + while (!Serial); |
| 31 | + |
| 32 | + // Configure the data receive callback |
| 33 | + Breakout.PDM.onReceive(onPDMdata); |
| 34 | + |
| 35 | + // Configure BufferSize to 4096 bytes |
| 36 | + Breakout.PDM.setBufferSize(4096); |
| 37 | + |
| 38 | + // Optionally set the gain, default value is 24 |
| 39 | + Breakout.PDM.setGain(24); |
| 40 | + |
| 41 | + // Initialize PDM with: |
| 42 | + // - one channel (mono mode) |
| 43 | + // - a 16 kHz sample rate |
| 44 | + if (!Breakout.PDM.begin(channels, frequency)) { |
| 45 | + Serial.println("Failed to start PDM!"); |
| 46 | + while (1); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void loop() { |
| 51 | + // Wait for samples to be read |
| 52 | + if (samplesRead == 2048) { |
| 53 | + |
| 54 | + // Compute RMS value |
| 55 | + double rms = RMS(sampleBuffer, samplesRead); |
| 56 | + |
| 57 | + Serial.println(rms); |
| 58 | + |
| 59 | + // Clear the read count |
| 60 | + samplesRead = 0; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Callback function to process the data from the PDM microphone. |
| 66 | + * NOTE: This callback is executed as part of an ISR. |
| 67 | + * Therefore using `Serial` to print messages inside this function isn't supported. |
| 68 | + * */ |
| 69 | +void onPDMdata() { |
| 70 | + // Query the number of available bytes |
| 71 | + int bytesAvailable = Breakout.PDM.available(); |
| 72 | + |
| 73 | + if(samplesRead < 2048) { |
| 74 | + // Read into the sample buffer |
| 75 | + Breakout.PDM.read(sampleBuffer, bytesAvailable); |
| 76 | + |
| 77 | + // 16-bit, 2 bytes per sample |
| 78 | + samplesRead = bytesAvailable / 2; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Compute RMS value of the input samples block |
| 84 | + */ |
| 85 | +double RMS(short Samples[], int Length) { |
| 86 | + double sum = 0; |
| 87 | + int i; |
| 88 | + for(i = 0; i < Length; i++) { |
| 89 | + sum += Samples[i] * Samples[i]; |
| 90 | + } |
| 91 | + return sqrt(sum / Length); |
| 92 | +} |
0 commit comments