|
| 1 | +/* Example of reading audio from SD card and sending over radio |
| 2 | + * Note: This example uses I2S output. Modify the USE_I2S #define |
| 3 | + * below to enable PWM output. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <SPI.h> |
| 7 | +#include <SD.h> |
| 8 | +#include <AutoAnalogAudio.h> |
| 9 | +#include <nrf_to_nrf.h> |
| 10 | + |
| 11 | +AutoAnalog aaAudio; |
| 12 | +nrf_to_nrf radio; |
| 13 | + |
| 14 | +uint8_t address[][6] = { "1Node", "2Node" }; |
| 15 | +#define BUFFER_SIZE 125 |
| 16 | +#define USE_I2S 1 // Change to 0 to enable PWM output instead of I2S |
| 17 | + |
| 18 | +/*********************************************************/ |
| 19 | +/* Tested with MAX98357A I2S breakout on XIAO 52840 Sense |
| 20 | +/* BCLK connected to Arduino D1 (p0.03) |
| 21 | +/* LRCK connected to Arduino D3 (p0.29) |
| 22 | +/* DIN connected to Arduino D5 (p0.05) |
| 23 | +/* SD connected to Arduino D6 (p1.11) |
| 24 | +/*********************************************************/ |
| 25 | + |
| 26 | +void setup() { |
| 27 | + |
| 28 | + Serial.begin(115200); |
| 29 | + |
| 30 | + radio.begin(); |
| 31 | + radio.setPayloadSize(BUFFER_SIZE); |
| 32 | + radio.setAutoAck(0); |
| 33 | + radio.setDataRate(NRF_2MBPS); |
| 34 | + radio.openWritingPipe(address[1]); |
| 35 | + radio.stopListening(); |
| 36 | + |
| 37 | + Serial.print("Init SD card..."); |
| 38 | + if (!SD.begin(10000000, 2)) { |
| 39 | + Serial.println("init failed!"); |
| 40 | + return; |
| 41 | + } |
| 42 | + Serial.println("init ok"); |
| 43 | + Serial.println("Analog Audio Begin"); |
| 44 | + aaAudio.begin(0, 1, USE_I2S); //Setup aaAudio using DAC and I2S |
| 45 | + |
| 46 | + //Setup for audio: Use 8-bit, mono WAV files @ 16kHz |
| 47 | + aaAudio.dacBitsPerSample = 8; // 8-bit |
| 48 | + aaAudio.setSampleRate(16000, 0); // 16khz, mono |
| 49 | + |
| 50 | + pinMode(6, OUTPUT); //Connected to SD pin of MAX98357A |
| 51 | + digitalWrite(6, HIGH); |
| 52 | + |
| 53 | + playAudio("guitar/g8b16km.wav"); // 16kHz @ 8-bits is about the maximum when reading from SD card & streaming over radio |
| 54 | +} |
| 55 | + |
| 56 | +void loop() { |
| 57 | + |
| 58 | + loadBuffer(); |
| 59 | +} |
| 60 | + |
| 61 | +/*********************************************************/ |
| 62 | +/* A simple function to handle playing audio files |
| 63 | +/*********************************************************/ |
| 64 | + |
| 65 | +File myFile; |
| 66 | + |
| 67 | +void playAudio(const char *audioFile) { |
| 68 | + |
| 69 | + if (myFile) { |
| 70 | + myFile.close(); |
| 71 | + } |
| 72 | + //Open the designated file |
| 73 | + myFile = SD.open(audioFile); |
| 74 | + //Skip past the WAV header |
| 75 | + myFile.seek(44); |
| 76 | +} |
| 77 | + |
| 78 | +void loadBuffer() { |
| 79 | + |
| 80 | + if (myFile.available()) { |
| 81 | + myFile.read(aaAudio.dacBuffer, BUFFER_SIZE); // Change this to dacBuffer16 for 16-bit samples |
| 82 | + aaAudio.feedDAC(0, BUFFER_SIZE); |
| 83 | + radio.startWrite(&aaAudio.dacBuffer[0], BUFFER_SIZE, 1); |
| 84 | + } else { |
| 85 | + myFile.seek(44); |
| 86 | + } |
| 87 | +} |
0 commit comments