-
Notifications
You must be signed in to change notification settings - Fork 5
Breakout PDM #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Breakout PDM #12
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
This example reads audio data from the connected PDM microphone, and prints | ||
out the RMS value of the samples to the Serial console. The Serial Plotter | ||
built into the Arduino IDE can be used to plot the audio data (Tools -> | ||
Serial Plotter) | ||
|
||
Circuit: | ||
- Portenta Breakout | ||
|
||
This example code is in the public domain. | ||
*/ | ||
|
||
#include <Arduino_PortentaBreakoutCarrier.h> | ||
|
||
// default number of output channels | ||
static const char channels = 1; | ||
|
||
// default PCM output frequency | ||
static const int frequency = 16000; | ||
|
||
// Buffer to read samples into, each sample is 16-bits. | ||
// Size in bytes must be a power of 2, grater or equal than PDM buffer size. | ||
short sampleBuffer[2048]; | ||
|
||
// Number of audio samples read | ||
volatile int samplesRead; | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
while (!Serial); | ||
|
||
// Configure the data receive callback | ||
Breakout.PDM.onReceive(onPDMdata); | ||
|
||
// Configure BufferSize to 4096 bytes | ||
Breakout.PDM.setBufferSize(4096); | ||
|
||
// Optionally set the gain, default value is 24 | ||
Breakout.PDM.setGain(24); | ||
|
||
// Initialize PDM with: | ||
// - one channel (mono mode) | ||
// - a 16 kHz sample rate | ||
if (!Breakout.PDM.begin(channels, frequency)) { | ||
Serial.println("Failed to start PDM!"); | ||
while (1); | ||
} | ||
} | ||
|
||
void loop() { | ||
// Wait for samples to be read | ||
if (samplesRead == 2048) { | ||
|
||
// Compute RMS value | ||
double rms = RMS(sampleBuffer, samplesRead); | ||
|
||
Serial.println(rms); | ||
|
||
// Clear the read count | ||
samplesRead = 0; | ||
} | ||
} | ||
|
||
/** | ||
* Callback function to process the data from the PDM microphone. | ||
* NOTE: This callback is executed as part of an ISR. | ||
* Therefore using `Serial` to print messages inside this function isn't supported. | ||
pennam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* */ | ||
void onPDMdata() { | ||
// Query the number of available bytes | ||
int bytesAvailable = Breakout.PDM.available(); | ||
|
||
if(samplesRead < 2048) { | ||
// Read into the sample buffer | ||
Breakout.PDM.read(sampleBuffer, bytesAvailable); | ||
|
||
// 16-bit, 2 bytes per sample | ||
samplesRead = bytesAvailable / 2; | ||
} | ||
} | ||
|
||
/** | ||
* Compute RMS value of the input samples block | ||
*/ | ||
double RMS(short Samples[], int Length) { | ||
double sum = 0; | ||
int i; | ||
for(i = 0; i < Length; i++) { | ||
sum += Samples[i] * Samples[i]; | ||
} | ||
return sqrt(sum / Length); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.