-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
14 additions
and
11 deletions.
There are no files selected for viewing
25 changes: 14 additions & 11 deletions
25
examples/Platforms/NRF52/NRF52_PDM_PWMTest/NRF52_PDM_PWMTest.ino
This file contains 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 |
---|---|---|
@@ -1,24 +1,27 @@ | ||
#include <AutoAnalogAudio.h> | ||
AutoAnalog aaAudio; | ||
|
||
// REQUIRES MBed Enabled Core for NRF52 (XIAO 52840 Sense) | ||
#if defined(USE_TINYUSB) | ||
// Needed for Serial.print on non-MBED enabled or adafruit-based nRF52 cores | ||
#include "Adafruit_TinyUSB.h" | ||
#endif | ||
|
||
void setup() { | ||
|
||
//Startup the PDM Microphone and PWM(pseudo DAC) on pin 5 | ||
aaAudio.begin(1, 1); | ||
aaAudio.autoAdjust = 0; | ||
aaAudio.adcBitsPerSample = 16; // 16-bit audio at 16khz is the default on NRF52 and cannot be modified currently (in progress) | ||
Serial.begin(115200); | ||
aaAudio.begin(1, 1); //Startup the PDM Microphone and PWM(pseudo DAC) on pin 5 | ||
aaAudio.adcBitsPerSample = 16; // 16-bit audio at 16khz is the default on NRF52 and cannot be modified currently (in progress) | ||
aaAudio.dacBitsPerSample = 16; | ||
aaAudio.setSampleRate(16000); | ||
|
||
} | ||
|
||
void loop() { | ||
|
||
aaAudio.getADC(320); // Get 320 Samples from the ADC | ||
for (int i = 0; i < 320; i++) { // Copy them into the DAC Buffer | ||
aaAudio.dacBuffer16[i] = (uint16_t)(aaAudio.adcBuffer16[i]); | ||
aaAudio.getADC(320); // Get 320 16-bit signed samples from the ADC | ||
for (int i = 0; i < 320; i++) { // Copy them into the DAC Buffer. The dac buffer is 16-bits unsigned | ||
int16_t sample = aaAudio.adcBuffer16[i]; // Copy to signed 16-bit | ||
sample += 0x8000; // Convert to unsigned 16-bit | ||
aaAudio.dacBuffer16[i] = sample; // Copy back to unsigned 16-bit buffer | ||
aaAudio.dacBuffer16[i] >>= 6; // Downsample to 10-bits for PWM output. With higher sample rates PWM can only handle 8-bits | ||
} | ||
aaAudio.feedDAC(0,320); // Feed the DAC with the ADC data | ||
aaAudio.feedDAC(0, 320); // Feed the DAC with the ADC data | ||
} |