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

Read a Wave file from a memoryStream

Mar 28, 2011 at 12:29 PM

Hi

Inside the NAudio WaveFileWriter Class, I have implement this method.


        public static void CreateWaveFile(Stream waveMemoryStream, WaveStream stream)
        {
            using (WaveFileWriter writer = new WaveFileWriter(waveMemoryStream, stream.WaveFormat))
            {
                byte[] buffer = new byte[stream.WaveFormat.SampleRate * stream.WaveFormat.Channels * 16];
                while (true)
                {
                    int bytesRead = stream.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                        break;
                    writer.WriteData(buffer, 0, bytesRead);
                }
            }
        }

//-----------------------------------------------------------------------------------------------------------------------------

Inside my appplication I have this code


        public void TrimWavFile(string inPath, MemoryStream outPath, TimeSpan cutFromStart, TimeSpan cutFromEnd)
        {
            using (Mp3FileReader mp3reader = new Mp3FileReader(inPath))
            {
                WaveStream stream = WaveFormatConversionStream.CreatePcmStream(mp3reader);
                WaveFormat format = stream.WaveFormat;
                MemoryStream waveStream = new MemoryStream();
                WaveFileWriter.CreateWaveFile(waveStream, stream);
                Stream rawStream = new MemoryStream(waveStream.ToArray());

                using (WaveFileReader reader = new WaveFileReader(rawStream))
                {
                    using (WaveFileWriter writer = new WaveFileWriter(outPath, reader.WaveFormat))
                    {
                        int bytesPerMillisecond = reader.WaveFormat.AverageBytesPerSecond / 1000;
                        int startPos = (int)cutFromStart.TotalMilliseconds * bytesPerMillisecond;
                        startPos = startPos - startPos % reader.WaveFormat.BlockAlign;
                        int endBytes = (int)cutFromEnd.TotalMilliseconds * bytesPerMillisecond;
                        endBytes = endBytes - endBytes % reader.WaveFormat.BlockAlign;
                        int endPos = (int)reader.Length - endBytes; TrimWavFile(reader, writer, startPos, endPos);
                    }
                }
            }
        }
       
        private void TrimWavFile(WaveFileReader reader, WaveFileWriter writer, int startPos, int endPos)
        {
            reader.Position = startPos;
            byte[] buffer = new byte[1024];
           
            while (reader.Position < endPos)
            {
                int bytesRequired = (int)(endPos - reader.Position);
                if (bytesRequired > 0)
                {
                    int bytesToRead = Math.Min(bytesRequired, buffer.Length);
                    int bytesRead = reader.Read(buffer, 0, bytesToRead);
                    if (bytesRead > 0)
                    {
                        writer.WriteData(buffer, 0, bytesRead);
                    }
                }
            }
        }

 

        private void button5_Click(object sender, EventArgs e)
        {
            TimeSpan start = new TimeSpan(0, 3, 0);
            TimeSpan end = new TimeSpan(0, 2, 0);

            MemoryStream temp = new MemoryStream();

            TrimWavFile(filename, temp, start, end);

            Stream rawStream = new MemoryStream(temp.ToArray());
            RawSourceWaveStream rawSourceStream = new RawSourceWaveStream(rawStream,new WaveFormat());
            WaveStream wavestream = rawSourceStream;
            WaveStream stream = WaveFormatConversionStream.CreatePcmStream(new WaveFileReader(wavestream));
            waveOut.Init(stream);
            waveOut.Play();
        }

 

=====================================================================================

 

When I click the button, I want to read an MP3 file, convert it to a wavefile, Store the converted wavefile into a memorystream,

Split the converted wavefile, then play the wave file withought saving it to my Hardrive.

Coordinator
Mar 30, 2011 at 11:20 AM

Have a look at the voice recorder project which demonstrates using NAudio to chop up a WAV file and save part of it to disk

http://voicerecorder.codeplex.com/

see my article on coding 4 fun here:

http://channel9.msdn.com/coding4fun/articles/NET-Voice-Recorder

Mark