Skip to content
Merged
36 changes: 36 additions & 0 deletions examples/Platforms/UnoR4/ADCtoDAC_UnoR4/ADCtoDAC_UnoR4.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#include <AutoAnalogAudio.h>
AutoAnalog aaAudio;

#define bufferSize 256

void setup() {
Serial.begin(115200);
Serial.println("Analog Audio Begin");

//Set pins low for MAX9814 microphone AR and GAIN pins
pinMode(7,OUTPUT);
digitalWrite(7,LOW);

pinMode(8,OUTPUT);
digitalWrite(8,LOW);


aaAudio.begin(3, 3); //Setup aaAudio using DAC and ADC
aaAudio.dacBitsPerSample = 16;
aaAudio.adcBitsPerSample = 16;
aaAudio.setSampleRate(44100);

}

void loop() {

aaAudio.getADC(bufferSize);

// Copy our 14-bit ADC data to the DAC buffer which is only 12-bit
// The dac input expects signed 16-bit samples, and the ADC will output signed 16-bit samples
for(int i=0; i<bufferSize; i++){
aaAudio.dacBuffer16[i] = aaAudio.adcBuffer16[i];
}
aaAudio.feedDAC(0,bufferSize);
}
42 changes: 42 additions & 0 deletions examples/Platforms/UnoR4/RadioRX_UnoR4/RadioRX_UnoR4.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#include <AutoAnalogAudio.h>
#include <RF24.h>

AutoAnalog aaAudio;
RF24 radio(7,8);

uint8_t address[6] = { "1Node" };
#define bufferSize 16

void setup() {
Serial.begin(115200);
while(!Serial){}
Serial.println("Analog Audio Begin");

radio.begin();
radio.setChannel(23);
radio.setPayloadSize(32);
radio.setAutoAck(0);
radio.openReadingPipe(1, address);
radio.startListening();

aaAudio.begin(0, 3); //Setup aaAudio using DAC and ADC
aaAudio.dacBitsPerSample = 16;
aaAudio.adcBitsPerSample = 16;
aaAudio.setSampleRate(44100);

}

uint32_t timer = millis();

void loop() {

if(radio.available()){
if(millis() - timer > 1000){
Serial.println("Data received");
timer = millis();
}
radio.read(aaAudio.dacBuffer16, bufferSize * 2); // Each 16-bit sample takes up 2 bytes
aaAudio.feedDAC(0,bufferSize);
}
}
42 changes: 42 additions & 0 deletions examples/Platforms/UnoR4/RadioTX_UnoR4/RadioTX_UnoR4.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#include <AutoAnalogAudio.h>
#include <RF24.h>

AutoAnalog aaAudio;
RF24 radio(7,8);

uint8_t address[6] = { "1Node" };

#define bufferSize 16

void setup() {
Serial.begin(115200);
Serial.println("Analog Audio Begin");

//Set pins low for MAX9814 microphone AR and GAIN pins
pinMode(5,OUTPUT);
digitalWrite(5,LOW);

pinMode(6,OUTPUT);
digitalWrite(6,LOW);

radio.begin();
radio.setChannel(23);
radio.setPayloadSize(32);
radio.setAutoAck(0);
radio.openWritingPipe(address);
radio.stopListening();

aaAudio.begin(3, 0); //Setup aaAudio using DAC and ADC
aaAudio.dacBitsPerSample = 16;
aaAudio.adcBitsPerSample = 16;
aaAudio.setSampleRate(44100);
radio.printDetails();
}

void loop() {

aaAudio.getADC(bufferSize); // Get buffersize of 16-bit samples
radio.writeFast(aaAudio.adcBuffer16, bufferSize * 2); // Each 16-bit sample takes up 2 bytes

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
AutoAnalogAudio streaming via DAC & ADC by TMRh20
Copyright (C) 2016 TMRh20 - tmrh20@gmail.com, github.com/TMRh20

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

Auto Analog Audio (Automatic DAC, ADC & Timer) library

Auto Analog Audio Library Information:
http://github.com/TMRh20
http://tmrh20.blogspot.com

This example automatically determines the format of WAV files &
attempts to play them.
*/

#include <SPI.h>
#include <SD.h>
#include <AutoAnalogAudio.h>

AutoAnalog aaAudio;


float volumeControl = 0.2;
#define BUFFER_SIZE 256

void setup() {

Serial.begin(115200);

Serial.print("Init SD card...");
if (!SD.begin(2)) {
Serial.println("init failed!");
return;
}
Serial.println("init ok");
Serial.println("Analog Audio Begin");
aaAudio.begin(0, 3); //Setup aaAudio using DAC and I2S

pinMode(6, OUTPUT); //Connected to SD pin of MAX98357A
digitalWrite(6, HIGH);

playAudio("brick/brick24.wav"); // 24kHz @ 16-bits is about the maximum when reading from SD card

}

void loop() {

loadBuffer();


if (Serial.available()) {
char c = Serial.read();
if (c == '=') {
volumeControl += 0.1;
} else if (c == '-') {
volumeControl -= 0.1;
volumeControl = max(0.0, volumeControl);
} else if (c == 'p') {
playAudio("libra/lb16b44k.wav");
}
Serial.println(volumeControl);
}
}

/*********************************************************/
/* A simple function to handle playing audio files
/*********************************************************/

File myFile;

void playAudio(char *audioFile) {

if (myFile) {
myFile.close();
}
//Open the designated file
myFile = SD.open(audioFile);

myFile.seek(22);
uint16_t var;
uint32_t var2;
myFile.read(&var, 2); // Get channels (Stereo or Mono)
myFile.read(&var2, 4); // Get Sample Rate
aaAudio.setSampleRate(var2, var - 1);
myFile.seek(34);
myFile.read(&var, 2); // Get Bits Per Sample
aaAudio.dacBitsPerSample = var;

myFile.seek(44); //Skip past the WAV header
}

void loadBuffer() {

if (myFile.available()) {

if (aaAudio.dacBitsPerSample == 8) {
myFile.read(aaAudio.dacBuffer, BUFFER_SIZE); // Change this to dacBuffer16 for 16-bit samples
for (uint32_t i = 0; i < BUFFER_SIZE; i++) {
aaAudio.dacBuffer[i] *= volumeControl;
}
aaAudio.feedDAC(0, BUFFER_SIZE); // change this to 3200 for 16-bit samples
} else {
myFile.read(aaAudio.dacBuffer16, BUFFER_SIZE); // Change this to dacBuffer16 for 16-bit samples
for (uint32_t i = 0; i < BUFFER_SIZE / 2; i++) {
int16_t sample = aaAudio.dacBuffer16[i];
sample *= volumeControl;
aaAudio.dacBuffer16[i] = (uint16_t)sample;
}
aaAudio.feedDAC(0, BUFFER_SIZE / 2); // change this to 3200 for 16-bit samples
}

} else {
myFile.seek(44);
}
}
Loading
Loading