write wav byte data to file and play from that

Jan 2, 2011 at 7:50 AM

I am trying to load wav file to stream then i want to write that byte data to file and then i want to read that file and play it,

but when before writing to file i need to take to buffer, when i am trying to load it to buffer my computer become hang, my code is given below:

 

WaveStream wavReader = new WaveFileReader (fileName);

WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(wavReader);

 WaveStream blockAlignedStream = new BlockAlignReductionStream (pcmStream);

WaveChannel32 waveChannel = new WaveChannel32 (blockAlignedStream);

 

WaveFormat format = new WaveFormat (waveChannel.WaveFormat.SampleRate, waveChannel.WaveFormat.BitsPerSample, waveChannel.WaveFormat.Channels);

 

byte[] buffer1 = new byte [waveChannel.WaveFormat.SampleRate];

BufferedWaveProvider inputProvider = new BufferedWaveProvider (waveChannel.WaveFormat);

 

int bytesRead;

for (int i = 0; i <waveChannel.Length; i++) {

   bytesRead = waveChannel.Read(buffer1, 0, waveChannel.WaveFormat.SampleRate);

    inputProvider.AddSamples(buffer1, 0, bytesRead);

}

waveOutDevice.Init(inputProvider);

waveOutDevice.Play();

///end of code

my logic is if I can play the inputProvider which is created from bytesRead,buffer1, and if I will be able to write the value of bytesRead,buffer1 to a file then  I

will be able to read these values from file and play it by inputProvider.

in this example I tested by  files which length is 1-2sec. my computer becomes hang when it is in for loop

I also increase the value of

MaxQueuedBuffers

in the

BufferedWaveProvider class

Coordinator
Jan 2, 2011 at 11:31 AM

your for loop is going round far too many times.

try a while (waveChannel.Position < waveChannel.Length) instead

Mark

Jan 2, 2011 at 3:05 PM

Thanks.

Thank u very much. its working now,

Jan 2, 2011 at 6:03 PM
Edited Jan 3, 2011 at 8:39 AM
 

Now I have another problem. I can write buffer data to file. code as follow:

 

WaveFormat format = new WaveFormat(waveChannel.WaveFormat.SampleRate, waveChannel.WaveFormat.BitsPerSample, waveChannel.WaveFormat.Channels);
            BufferedWaveProvider inputProvider = new BufferedWaveProvider(waveChannel.WaveFormat);
            byte[] buffer1 = new byte[waveChannel.WaveFormat.SampleRate];
            int bytesRead;
            FileStream fs = new FileStream("c1.txt", FileMode.CreateNew);
            // Create the writer for data.
            BinaryWriter w = new BinaryWriter(fs);
            // Write data
            while (waveChannel.Position < waveChannel.Length)
            {
                bytesRead = waveChannel.Read(buffer1, 0, waveChannel.WaveFormat.SampleRate);
                w.Write((int)bytesRead);
                for (int j = 0; j < 4800; j++)
                    w.Write((int)buffer1[j]);
                inputProvider.AddSamples(buffer1, 0, bytesRead);
            }
            w.Close();
            fs.Close();
            waveOutDevice.Init(inputProvider);
            waveOutDevice.Play();
            waveChannel.Dispose();
            blockAlignedStream.Dispose();
            pcmStream.Dispose();
            wavReader.Dispose();

  But I can't play that audio from reading byte data from that file. I can't understand how to start. Please Help

 

Coordinator
Jan 4, 2011 at 10:38 AM

your best bet is to use the WaveFileWriter class to make a WAV file

Mark