I record some sound with NAudio to float array and after getting the recorded audio I want to convert it back from float array to wav to check it.
I wrote a method but I'm not sure about it because: I try to record a 15Khz tone of 5 seconds and pass it through fft to check for the spectrum to ensure the 15k was catched. These are my steps:
I generate this tone with matlab firstly and record with Audacity that it is the right tone. Audacity shows a nice spectrum with a peak in 15k with -30db.
I try to record with Naudio the tone and then converting it back from the recorded float array to wav to check it in Audacity. unfortunately, it show nothing but some noise up to 12k and no more(-80db at 12k).
Here is the regular recording convertion when data is available:
void myWaveIn_DataAvailable(object sender, WaveInEventArgs e)
{
myMemoryStream.Write(e.Buffer, 0, e.BytesRecorded);//this is for playing the myMemoryStream
for (int index = 0; index < e.BytesRecorded; index += 2)//Here I convert in a loop the stream into floating number samples
{
short sample = (short)((e.Buffer[index + 1] << 8) |
e.Buffer[index + 0]);
samples32.Add(sample / 32768f);//IEEE 32 floating number
}
}
This is my code for converting back:
void floatBackToStream(float[] myfloatArray)
{
short[] myShort = new short [myfloatArray.Length];
myMemoryStream2 = new MemoryStream();
for (int i = 0; i < myfloatArray.Length; i++)
{
myShort[i] = (short)(myfloatArray[i] * 32768f);
myMemoryStream2.WriteByte((byte)(myShort[i] & 255));
myMemoryStream2.WriteByte((byte)(myShort[i] >> 8));
}
myMemoryStream2.Position = 0;
myRaw2 = new RawSourceWaveStream(myMemoryStream2, new WaveFormat(44100,1));
WaveFileWriter.CreateWaveFile("C:/Users/alon/Desktop/myRecordings/myCalibration.wav", myRaw2);
}
WaveInEvent
? By default, it uses a 8kHz sampling rate. – SleuthEye