This project has moved. For the latest updates, please go here.

Save raw bytes to file then save them to wav

Jan 15, 2011 at 3:20 PM

Hi , I try to save the input stream without wav-headers and metadata into a binary file and after save these bytes from this file as a wav-file

I use the following code

public partial class Form1 : Form
{
WaveIn waveIn;
FileStream fs;
RawSourceWaveStream rsv;
WaveFileReader wfr;
BinaryWriter bw;

private void button1_Click(object sender, EventArgs e)
{
waveIn = new WaveIn();
waveIn.WaveFormat = new WaveFormat(8000, 16, 1);
fs = new FileStream("D:\\some_file.ddd", FileMode.OpenOrCreate);
waveIn.DeviceNumber = 0;
waveIn.DataAvailable += waveIn_DataAvailable;
waveIn.RecordingStopped += new EventHandler(waveIn_RecordingStopped);
bw = new BinaryWriter(fs);
MessageBox.Show("Start Recording");
waveIn.StartRecording();
}
//Write bytes by BinaryWriter
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{

bw.Write(e.Buffer, 0, e.BytesRecorded);
}

private void button2_Click(object sender, EventArgs e)
{
if (waveIn != null)
{
StopRecording();
}
}

private void StopRecording()
{
waveIn.StopRecording();

MessageBox.Show("StopRecording");
}

private void waveIn_RecordingStopped(object sender, EventArgs e)
{
if (bw != null)
{
bw.Dispose();
bw = null;
}
waveIn.Dispose();
waveIn = null;
fs.Close();
}
//Convert bytes to wav-file
private void button3_Click(object sender, EventArgs e)
{
fs = new FileStream("D:\\some_file.ddd", FileMode.OpenOrCreate);
WaveFormat waveFormat = WaveFormat.CreateMuLawFormat(8000, 1);
RawSourceWaveStream reader = new RawSourceWaveStream(fs, waveFormat);
using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))
{
WaveFileWriter.CreateWaveFile(@"D:\ddd.wav", convertedStream);
}
}

But finally I get some terrible sound and wav-file usually twice more then original recording length. What should I do?

public partial class Form1 : Form
{
WaveIn waveIn;
FileStream fs;
RawSourceWaveStream rsv;
WaveFileReader wfr;
BinaryWriter bw;

private void button1_Click(object sender, EventArgs e)
{
waveIn = new WaveIn();
waveIn.WaveFormat = new WaveFormat(8000, 16, 1);
fs = new FileStream("D:\\some_file.ddd", FileMode.OpenOrCreate);
waveIn.DeviceNumber = 0;
waveIn.DataAvailable += waveIn_DataAvailable;
waveIn.RecordingStopped += new EventHandler(waveIn_RecordingStopped);
bw = new BinaryWriter(fs);
MessageBox.Show("Start Recording");
waveIn.StartRecording();
}
//Write bytes by BinaryWriter
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{

bw.Write(e.Buffer, 0, e.BytesRecorded);
}

private void button2_Click(object sender, EventArgs e)
{
if (waveIn != null)
{
StopRecording();
}
}

private void StopRecording()
{
waveIn.StopRecording();

MessageBox.Show("StopRecording");
}

private void waveIn_RecordingStopped(object sender, EventArgs e)
{
if (bw != null)
{
bw.Dispose();
bw = null;
}
waveIn.Dispose();
waveIn = null;
fs.Close();
}
//Convert bytes to wav-file
private void button3_Click(object sender, EventArgs e)
{
fs = new FileStream("D:\\some_file.ddd", FileMode.OpenOrCreate);
WaveFormat waveFormat = WaveFormat.CreateMuLawFormat(8000, 1);
RawSourceWaveStream reader = new RawSourceWaveStream(fs, waveFormat);
using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))
{
WaveFileWriter.CreateWaveFile(@"D:\ddd.wav", convertedStream);
}
}
Coordinator
Jan 17, 2011 at 11:36 AM

you aren't recording in mulaw format but 16 bit pcm, so you don't need the WaveFormatConversionStream and shouldn't use WaveFormat.CreateMuLawFormat

Mark