I have been trying to work out how to get my vs1003 module board to encode sound to wav, unfortunately its not working, I have been working a library that is written in C, for the Arduino IDE
Here is the whole function but for some reason it gets stuck in the first do/while loop (only got 1 so should be easy to find, hopefully ),
even though i am only reading the buffer to see how many words are ready to be read, i get very weird readings
Code: Select all
//Used to record sound to a wav file
void VS1003::wavRecord()
{
File record;
const unsigned char RIFFHeader0[] = {
'R' , 'I' , 'F' , 'F' , // Chunk ID (RIFF)
0x70, 0x70, 0x70, 0x70, // Chunk payload size (calculate after rec!)
'W' , 'A' , 'V' , 'E' , // RIFF resource format type
'f' , 'm' , 't' , ' ' , // Chunk ID (fmt )
0x14, 0x00, 0x00, 0x00, // Chunk payload size (0x14 = 20 bytes)
0x11, 0x00, // Format Tag (IMA ADPCM)
0x01, 0x00, // Channels (1)
0x80, 0x3e, 0x00, 0x00, // Sample Rate, 0x3e80 = 16.0kHz
0xd7, 0x0f, 0x00, 0x00, // Average Bytes Per Second
0x00, 0x01, // Data Block Size (256 bytes)
0x04, 0x00, // ADPCM encoded bits per sample (4 bits)
0x02, 0x00, // Extra data (2 bytes)
0xf9, 0x01, // Samples per Block (505 samples)
'f' , 'a' , 'c' , 't' , // Chunk ID (fact)
0xc8, 0x01, 0x00, 0x00, // Chunk payload size (456 bytes (zeropad!))
0xff, 0xff, 0xff, 0xff // Number of Samples (calculate after rec!)
};
const unsigned char RIFFHeader504[] = {
'd' , 'a' , 't' , 'a' , // Chunk ID (data)
0x70, 0x70, 0x70, 0x70 // Chunk payload size (calculate after rec!)
};
//data buffer for saving to disk
unsigned char db[512];
int dataToRead = 0;
int idx = 0;
if (!(record = SD.open("RECORD1.WAV", FILE_WRITE)))
{
Serial.println("Failed to open record file in write mode");
}
record.seek(0);
// Create a wav file and create the necessary header
record.write(RIFFHeader0,sizeof(RIFFHeader0));
for (int i = 0; i<448; i++)
{
record.write('0');
}
record.seek(504);
record.write(RIFFHeader504,sizeof(RIFFHeader504));
record.flush();
Serial.println(record.size());
//Set sample rate divider=9
writeRegister(SCI_AICTRL0, 0x00, 0x09);
delay(100);
//AutoGain
writeRegister(SCI_AICTRL1, 0x10, 0x00);
delay(100);
//RECORD,NEWMODE,RESET
writeRegister(SCI_MODE, 0x18, 0x04);
delay(1000);
int i = 0;
while (i<100)
{
//wait until 512 bytes become available
do {
dataToRead = readRegister(SCI_HDAT1);
Serial.print("data words to read: ");
Serial.println(dataToRead);
delay(10);
} while (dataToRead < 256 || dataToRead >= 896);
Serial.println("Reading data...");
while (idx < 512)
{
uint16_t w = readRegister(SCI_HDAT0);
db[idx++] = w >> 8;
db[idx++] = w & 0xFF;
}
Serial.println("Finished reading data...");
idx = 0;
Serial.println("Writing to disk..");
record.write(db,sizeof(db));
Serial.println("Finished writing to disk..");
record.flush();
i++;
}
Serial.println("Recording finished :)");
record.close();
}
https://github.com/zigon15/VS1003_Libar ... VS1003.cpp is the full class with all the setting up etc
Thanks, Zigon