|
1 | 1 | #include <AutoAnalogAudio.h> |
2 | 2 | AutoAnalog aaAudio; |
3 | 3 |
|
4 | | -// REQUIRES MBed Enabled Core for NRF52 (XIAO 52840 Sense) |
| 4 | +#if defined(USE_TINYUSB) |
| 5 | +// Needed for Serial.print on non-MBED enabled or adafruit-based nRF52 cores |
| 6 | +#include "Adafruit_TinyUSB.h" |
| 7 | +#endif |
5 | 8 |
|
6 | 9 | void setup() { |
7 | | - |
8 | | - //Startup the PDM Microphone and PWM(pseudo DAC) on pin 5 |
9 | | - aaAudio.begin(1, 1); |
10 | | - aaAudio.autoAdjust = 0; |
11 | | - aaAudio.adcBitsPerSample = 16; // 16-bit audio at 16khz is the default on NRF52 and cannot be modified currently (in progress) |
| 10 | + Serial.begin(115200); |
| 11 | + aaAudio.begin(1, 1); //Startup the PDM Microphone and PWM(pseudo DAC) on pin 5 |
| 12 | + aaAudio.adcBitsPerSample = 16; // 16-bit audio at 16khz is the default on NRF52 and cannot be modified currently (in progress) |
12 | 13 | aaAudio.dacBitsPerSample = 16; |
13 | 14 | aaAudio.setSampleRate(16000); |
14 | | - |
15 | 15 | } |
16 | 16 |
|
17 | 17 | void loop() { |
18 | 18 |
|
19 | | - aaAudio.getADC(320); // Get 320 Samples from the ADC |
20 | | - for (int i = 0; i < 320; i++) { // Copy them into the DAC Buffer |
21 | | - aaAudio.dacBuffer16[i] = (uint16_t)(aaAudio.adcBuffer16[i]); |
| 19 | + aaAudio.getADC(320); // Get 320 16-bit signed samples from the ADC |
| 20 | + for (int i = 0; i < 320; i++) { // Copy them into the DAC Buffer. The dac buffer is 16-bits unsigned |
| 21 | + int16_t sample = aaAudio.adcBuffer16[i]; // Copy to signed 16-bit |
| 22 | + sample += 0x8000; // Convert to unsigned 16-bit |
| 23 | + aaAudio.dacBuffer16[i] = sample; // Copy back to unsigned 16-bit buffer |
| 24 | + aaAudio.dacBuffer16[i] >>= 6; // Downsample to 10-bits for PWM output. With higher sample rates PWM can only handle 8-bits |
22 | 25 | } |
23 | | - aaAudio.feedDAC(0,320); // Feed the DAC with the ADC data |
| 26 | + aaAudio.feedDAC(0, 320); // Feed the DAC with the ADC data |
24 | 27 | } |
0 commit comments