Hi everyone,
I am new here and fairly new to coding. I am working on a project which takes a .wav file and performs an FFT for analysis. I decided to use an Adafruit Metro M4 Express board to record the .wav file and I have had some trouble getting things to work.
I am currently attempting to save the analog data in a temporary file on the SD and then have the code read this data and write it to the .wav file but I cant seem to figure out how to get the code to take what's in the .txt and send it to the .wav file.
I originally tried to send the analog data directly to the .wav but I could not get it to work. I also heard that sending data directly to the .wav is not the best idea.
If anyone has any suggestions on how to do this I would really appreciate it.
my code for this section of the project is here:
#include <SD.h>
#include <SPI.h>
File wavFile;
File tempFile;
int datatemp = 0;
const char* fileName = "test1.wav";
const char TempfileName = "datatemp.txt";
const int chipSelect = 10;
int data = 0;
int buttonState = 0;
const int buttonPin = 6;
const int ledPin1 = 9;
const int ledPin2 = 13;
const int pulsePin = 7; // Input signal connected to Pin 7 of Arduino
int pulseHigh; // Integer variable to capture High time of the incoming pulse
int pulseLow; // Integer variable to capture Low time of the incoming pulse
float pulseTotal; // Float variable to capture Total time of the incoming pulse
char data1 = data;
int MIN_DATA_VALUE;
int MAX_DATA_VALUE;
/// The first 4 byte of a wav file should be the characters "RIFF" */
char chunkID[4] = {'R', 'I', 'F', 'F'};
/// 36 + SubChunk2Size
uint32_t chunkSize = 36; // You Don't know this until you write your data but at a minimum it is 36 for an empty file
/// "should be characters "WAVE"
char format[4] = {'W', 'A', 'V', 'E'};
/// " This should be the letters "fmt ", note the space character
char subChunk1ID[4] = {'f', 'm', 't', ' '};
///: For PCM == 16, since audioFormat == uint16_t
uint32_t subChunk1Size = 16;
///: For PCM this is 1, other values indicate compression
uint16_t audioFormat = 1;
///: Mono = 1, Stereo = 2, etc.
uint16_t numChannels = 1;
///: Sample Rate of file
uint32_t sampleRate = 44100;
///: SampleRate * NumChannels * BitsPerSample/8
uint32_t byteRate = 44100 * 2;
///: The number of byte for one frame NumChannels * BitsPerSample/8
uint16_t blockAlign = 2;
///: 8 bits = 8, 16 bits = 16
uint16_t bitsPerSample = 16;
///: Contains the letters "data"
char subChunk2ID[4] = {'d', 'a', 't', 'a'};
///: == NumSamples * NumChannels * BitsPerSample/8 i.e. number of byte in the data.
uint32_t subChunk2Size = 0; // You Don't know this until you write your data
unsigned long previousMillis = 0;
const long interval = 10000;
// *** from final
int writeWavHeader()
{
wavFile.write(chunkID,4);
wavFile.write((byte*)&chunkSize,4);
wavFile.write(format,4);
wavFile.write(subChunk1ID,4);
wavFile.write((byte*)&subChunk1Size,4);
wavFile.write((byte*)&audioFormat,2);
wavFile.write((byte*)&numChannels,2);
wavFile.write((byte*)&sampleRate,4);
wavFile.write((byte*)&byteRate,4);
wavFile.write((byte*)&blockAlign,2);
wavFile.write((byte*)&bitsPerSample,2);
wavFile.write(subChunk2ID,4);
wavFile.write((byte*)&subChunk2Size,4);
return 0;
}
int writeDataToWavFile(int signal)
{
tempFile = SD.open(datatemp)
if (tempFile) {
while(datatemp.Available()){
int signal = Serial.read
readint += signal
delay(3)
}
}
int16_t sampleValue = map(signal, MIN_DATA_VALUE, MAX_DATA_VALUE,-32767,32767);
subChunk2Size += numChannels * bitsPerSample/8;
wavFile.seek(40);
wavFile.write((byte*)&subChunk2Size,4);
wavFile.seek(4);
chunkSize = 36 + subChunk2Size;
wavFile.write((byte*)&chunkSize,4);
wavFile.seek(wavFile.size()-1);
wavFile.write((byte*)&sampleValue,2);
return 0;
}
void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(pulsePin, INPUT);
Serial.begin(9600);
Serial.println("starting project");
if (SD.begin(chipSelect))
{
Serial.println("SD card is present");
}
else
{
Serial.println("SD card missing");
while(1); //wait here forever
}
writeWavHeader();
wavFile.close();
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin1, HIGH);
delay(500);
digitalWrite(ledPin1, LOW);
delay(500);
digitalWrite(ledPin1, HIGH);
delay(500);
digitalWrite(ledPin1, LOW);
delay(500);
digitalWrite(ledPin1, HIGH);
delay(500);
digitalWrite(ledPin1, LOW);
delay(500);
digitalWrite(ledPin1, HIGH);
Serial.println("start of for sect");
// writeWavHeader();
for (int i = 0; i <= 500; i++) {
SD.open(fileName, FILE_WRITE);
if (wavFile);
int data = analogRead(A3);
Serial.println(data);
writeDataToWavFile(data);
}
wavFile.close();
digitalWrite(ledPin1, LOW);
Serial.print("done");
}
}
Sorry if this code is sort of a horror show. I'm not much of a coder. more of a hardware guy.