#include "OpenAudio_ArduinoLibrary.h"
#include <Wire.h>
//#include <SPI.h>
#include <SD.h>
//#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioInputI2S_F32 i2s1; //xy=231.25,278.25
AudioRecordQueue_F32 queue1; //xy=471.25,262.25
AudioRecordQueue_F32 queue2; //xy=483.25,306.25
AudioConnection_F32 patchCord1(i2s1, 0, queue1, 0);
AudioConnection_F32 patchCord2(i2s1, 1, queue2, 0);
// GUItool: end automatically generated code
FsFile file1;
FsFile file2;
void setup() {
AudioMemory(6);
AudioMemory_F32(200);
delay(10);
Wire2.begin();
delay(500);
// Wake-up output block (default state after powering)
// The first line sends the addressing packet "0x9C" (see TLV320ADC6120 datasheet)
// The address is indeed 0x4E in 7 bits, the write bit transforms it to 0x9C
Wire2.beginTransmission(0x4E);
Wire2.write(0x02);
Wire2.write(0x81);
Wire2.endTransmission();
delay(300); // Allows board to wake up
// Board configuration
// I2S output configuration
Wire2.beginTransmission(0x4E);
Wire2.write(0x07);
Wire2.write(0x38);
Wire2.endTransmission();
delay(300);
// Enable inputs 1 and 2
Wire2.beginTransmission(0x4E);
Wire2.write(0x73);
Wire2.write(0xC0);
Wire2.endTransmission();
delay(300);
// Enable audio output
Wire2.beginTransmission(0x4E);
Wire2.write(0x74);
Wire2.write(0xC0);
Wire2.endTransmission();
delay(300);
// Power up the microphones, internal ADC, and PLL
Wire2.beginTransmission(0x4E);
Wire2.write(0x75);
Wire2.write(0xE0);
Wire2.endTransmission();
delay(300);
// Apply gain of 1dB to ch1
Wire2.beginTransmission(0x4E);
Wire2.write(0x3D);
Wire2.write(0x80);
Wire2.endTransmission();
delay(300);
// Apply gain of 1dB to ch2
Wire2.beginTransmission(0x4E);
Wire2.write(0x42);
Wire2.write(0x80);
Wire2.endTransmission();
delay(300);
Serial.begin(115200);
delay(10);
// Here, we are monitoring the I2S input and have sound.
// Currently set to 44.1kHz and 32-bit precision
// BCLK 6.144MHz for 96kHz 32-bit, studio quality
bool ok;
Serial.print("Initializing SD card...");
ok = SD.sdfs.begin(SdioConfig(DMA_SDIO)); // sdfat library
if (!ok) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
Serial.println();
if (SD.exists("test_1.raw")) {
// The SD library writes new data to the end of the
// file, so to start a new recording, the old file
// must be deleted before new data is written.
SD.remove("test_1.raw");
}
if (SD.exists("test_2.raw")) {
// The SD library writes new data to the end of the
// file, so to start a new recording, the old file
// must be deleted before new data is written.
SD.remove("test_2.raw");
}
file1 = SD.sdfs.open("test_1.raw", FILE_WRITE | O_CREAT);
file2 = SD.sdfs.open("test_2.raw", FILE_WRITE | O_CREAT);
delay(10);
Serial.println("test.raw opened");
Serial.println();
queue1.begin();
queue2.begin();
}
void loop() {
// put your main code here, to run repeatedly:
if (queue1.available() >= 16) {
byte buffer1[8192]; // Increase buffer size to accommodate 8Kbytes (16*128*4)
float* floatBuffer = (float*)buffer1; // Treat buffer1 as float array
for (int i = 0; i < 16; i++) {
float* readBuffer = queue1.readBuffer(); // Assume queue1.readBuffer() returns float*
memcpy(floatBuffer + (i * 128), readBuffer, 128 * sizeof(float));
queue1.freeBuffer(); // Corrected the queue number to 1
}
//Serial.println("done4");
// write all 512 bytes to the SD card
//elapsedMicros usec = 0;
//Serial.println();
// print max audio memory AudioMemoryUsageMax();
// Serial.print("AudioMemoryUsageMax32 = ");
// Serial.println(AudioMemoryUsageMax_F32());
// Serial.print("AudioMemoryUsageMax = ");
// Serial.println(AudioMemoryUsageMax());
file1.write(buffer1, 8192);
file1.sync();
// Uncomment these lines to see how long SD writes
// are taking. A pair of audio blocks arrives every
// 5802 microseconds, so hopefully most of the writes
// take well under 5802 us. Some will take more, as
// the SD library also must write to the FAT tables
// and the SD card controller manages media erase and
// wear leveling. The queue1 object can buffer
// approximately 301700 us of audio, to allow time
// for occasional high SD card latency, as long as
// the average write time is under 5802 us.
//Serial.print("SD write, us=");
//Serial.println(usec);
}
if (queue2.available() >= 16) {
byte buffer2[8192]; // Increase buffer size to accommodate 8Kbytes (16*128*4)
float* floatBuffer = (float*)buffer2; // Treat buffer2 as float array
for (int i = 0; i < 16; i++) {
float* readBuffer = queue2.readBuffer(); // Assume queue1.readBuffer() returns float*
memcpy(floatBuffer + (i * 128), readBuffer, 128 * sizeof(float));
queue2.freeBuffer(); // Corrected the queue number to 1
}
// write all 512 bytes to the SD card
//elapsedMicros usec = 0;
file2.write(buffer2, 8192);
file2.sync();
// Uncomment these lines to see how long SD writes
// are taking. A pair of audio blocks arrives every
// 5802 microseconds, so hopefully most of the writes
// take well under 5802 us. Some will take more, as
// the SD library also must write to the FAT tables
// and the SD card controller manages media erase and
// wear leveling. The queue1 object can buffer
// approximately 301700 us of audio, to allow time
// for occasional high SD card latency, as long as
// the average write time is under 5802 us.
//Serial.print("SD write, us=");
//Serial.println(usec);
}
delay(5);
}