Click here to Skip to main content
Click here to Skip to main content

Concatenating Wave Files Using C# 2005

, 10 Mar 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
How to concatenate wave files in a single file

Introduction

The WAVE file format is a subset of Microsoft's RIFF specification for the storage of multimedia files. A RIFF file starts out with a file header followed by a sequence of data chunks. A WAVE file is often just a RIFF file with a single "WAVE" chunk which consists of two sub-chunks -- a "fmt" chunk specifying the data format and a "data" chunk containing the actual sample data. Call this form the "Canonical form".

The main idea is to create only one header for all WAV files that you want to concatenate and then write data of each file in a single file.

Wave file headers follow the standard RIFF file format structure. The first 8 bytes in the file are the standard RIFF chunk header which have a chunk ID of "RIFF" and a chunk size equal to the file size minus the 8 bytes used by the header.

So we need to know the total length of all files to define ChunkSize and read NumChannels, SampleRate and BitsPerSample.

private void WaveHeaderIN(string spath)
        {
            FileStream fs = new FileStream(spath, FileMode.Open, FileAccess.Read);

            BinaryReader br = new BinaryReader(fs);
            length = (int)fs.Length - 8;
            fs.Position = 22;
            channels = br.ReadInt16();
            fs.Position = 24;
            samplerate = br.ReadInt32();
            fs.Position = 34;

            BitsPerSample = br.ReadInt16();
            DataLength = (int)fs.Length - 44;
            br.Close ();
            fs.Close();
        }

As we know channels are stored in the WAV header in byte number 22, we move the current position of the file to this location and the size of it is 2 bytes so we use br.ReadInt16() to read only 2 bytes and so on....

Construct the Header of Merged File

private void WaveHeaderOUT(string sPath)
        {
            FileStream fs = new FileStream(sPath, FileMode.Create, FileAccess.Write );

            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(new char[4] { 'R', 'I', 'F', 'F' });

            bw.Write(length);

            bw.Write(new char[8] {'W','A','V','E','f','m','t',' '});

            bw.Write((int)16);

            bw.Write((short)1);
            bw.Write(channels);

            bw.Write(samplerate );

            bw.Write((int)(samplerate * ((BitsPerSample * channels) / 8)));

            bw.Write((short )((BitsPerSample * channels) / 8));

            bw.Write(BitsPerSample);

            bw.Write(new char[4] {'d','a','t','a'});
            bw.Write(DataLength);
            bw.Close();
            fs.Close();
        }

We must be careful when writing the header. If there is any small mistake, the merged file doesn't work, so we write "RIFF" as an array of char, not as string and use int type for storing 4 bytes and short type for storing 2 bytes.

Write Data of all Files in the Merged File

  public void Merge(string[] files, string outfile)
        {
            WaveIO wa_IN = new WaveIO();
            WaveIO wa_out = new WaveIO();

            wa_out.DataLength = 0;
            wa_out.length = 0;


            //Gather header data
            foreach (string path in files)
            {
                wa_IN.WaveHeaderIN(@path);
                wa_out.DataLength += wa_IN.DataLength;
                wa_out.length += wa_IN.length;

            }

            //Reconstruct new header
            wa_out.BitsPerSample = wa_IN.BitsPerSample;
            wa_out.channels = wa_IN.channels;
            wa_out.samplerate = wa_IN.samplerate;
            wa_out.WaveHeaderOUT(@outfile);

            foreach (string path in files)
            {
                FileStream fs = new FileStream(@path, FileMode.Open, FileAccess.Read);
                byte[] arrfile = new byte[fs.Length - 44];
                fs.Position = 44;
                fs.Read(arrfile, 0, arrfile.Length);
                fs.Close();

                FileStream fo =
                    new FileStream(@outfile, FileMode.Append, FileAccess.Write);
                BinaryWriter bw = new BinaryWriter(fo);
                bw.Write(arrfile);
                bw.Close();
                fo.Close();
            }
          }

First we need to calculate the total length and data length of all files and then specify the channels, SampleRate and BitsPerSample of the output file.The last thing is to start reading data that is stored after byte number 44 and append it to the merged file.

All we need to do is call the Merge method and specify the input files and output file.

string[] files = new string[2] { @"C:\WINDOWS\Media\Windows XP Startup.wav",
                @"C:\WINDOWS\Media\Windows XP Shutdown.wav" };

WaveIO wa = new WaveIO();
wa.Merge(files,@"c:\oou.wav");

Play the Merged File

Visual Studio 2005 provides a new class to play sound. Therefore, we don't need an API or anything else.

FileStream fs = new FileStream(@"c:\oou.wav", FileMode.Open,FileAccess.Read);
System.Media.SoundPlayer sp = new System.Media.SoundPlayer(fs);
sp.Play();
fs.Close();

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ehab M. M. Essa
Computer Science Department
Faculty of Computers and Information, Mansoura University, Egypt

Comments and Discussions

 
QuestionEditing small audio files , into a "song". Pinmemberiawiliams15-Sep-14 10:46 
QuestionHola, necesito hablar con FocusedWolf por sus comentarios Pinmemberlepachec5-May-14 19:06 
Suggestionminor improvements needed Pinmembermdanh20021-Oct-11 5:24 
GeneralLicense Pinmemberthumb2231-Jan-11 4:10 
GeneralI have figured out a solution to the noise problem... PinmemberAbhi2028-Aug-10 16:17 
QuestionHeavy distortion in output file [modified] PinmemberGolph4217-Nov-09 9:33 
Generalvb.net: read, concat in mem, output to file or stream PinmemberMember 425506423-Sep-09 1:37 
I reworked this example into a class which allows appending wav files and outputting it to a file or to a memory stream which can then be played like this:

Dim objPlayer As New Media.SoundPlayer
Dim wavFile As New wavFile

With wavFile
.loadFile("C:\wav\file1.wav")
.append("C:\wav\file2.wav")
.append("C:\wav\file3.wav")
objPlayer.Stream = .toStream
objPlayer.Play()
End With

Please note you can only append/concat files of plain PCM format without any additional chucks like 'fact'. This is working for me. Have fun.

This is the class:

Imports System.IO
 
Public Class wavFile
 
#Region "Properties"
    Dim _length As Integer = 0
    Dim _channels As Int16 = 0
    Dim _sampleRate As Int32 = 0
    Dim _dataLength As Int32 = 0
    Dim _bitsPerSample As Int16 = 0
    Dim _data(32768) As Byte
    Dim _wavFile As wavFile = Nothing
 
    Public Property length() As Integer
        Get
            Return _length
        End Get
        Set(ByVal value As Integer)
            _length = value
        End Set
    End Property
 

    Public Property channels() As Int16
        Get
            Return _channels
        End Get
        Set(ByVal value As Int16)
            _channels = value
        End Set
    End Property
 
    Public Property sampleRate() As Int32
        Get
            Return _sampleRate
        End Get
        Set(ByVal value As Int32)
            _sampleRate = value
        End Set
    End Property
 
    Public Property dataLength() As Int32
        Get
            Return _dataLength
        End Get
        Set(ByVal value As Int32)
            _dataLength = value
        End Set
    End Property
 
    Public Property bitsPerSample() As Int16
        Get
            Return _bitsPerSample
        End Get
        Set(ByVal value As Int16)
            _bitsPerSample = value
        End Set
    End Property
 
    Public Property data() As Byte()
        Get
            Return _data
        End Get
        Set(ByVal value As Byte())
            _data = value
        End Set
    End Property
 
#End Region
 
    Public Sub loadFile(ByVal path As String)
 
            Dim FS As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read)
            Dim BR As BinaryReader = New BinaryReader(FS)
 
            _length = CInt(FS.Length - 8)
            FS.Position = 22
            _channels = BR.ReadInt16()
            FS.Position = 24
            _sampleRate = BR.ReadInt32()
            FS.Position = 34
            _bitsPerSample = BR.ReadInt16()
            _dataLength = CInt(FS.Length - 44)
            FS.Position = 44
 
             ReDim _data(_dataLength - 1)
            FS.Read(_data, 0, _dataLength)
            BR.Close()
            FS.Close()
    End Sub
 
    Private Function createHeader() As MemoryStream
        Dim MS As New MemoryStream
        Dim BW As New BinaryWriter(MS)
 
            BW.Write(Convert.ToChar("R"))
            BW.Write(Convert.ToChar("I"))
            BW.Write(Convert.ToChar("F"))
            BW.Write(Convert.ToChar("F"))
            BW.Write(_length)
 
            BW.Write(Convert.ToChar("W"))
            BW.Write(Convert.ToChar("A"))
            BW.Write(Convert.ToChar("V"))
            BW.Write(Convert.ToChar("E"))
            BW.Write(Convert.ToChar("f"))
            BW.Write(Convert.ToChar("m"))
            BW.Write(Convert.ToChar("t"))
            BW.Write(Convert.ToChar(" "))
 
            BW.Write(CInt(16))
            BW.Write(CShort(1))
            BW.Write(_channels)
            BW.Write(_sampleRate)
            BW.Write(CInt(_sampleRate * ((_bitsPerSample * _channels) / 8)))
            BW.Write(CShort((_bitsPerSample * _channels) / 8))
            BW.Write(_bitsPerSample)
 
            BW.Write(Convert.ToChar("d"))
            BW.Write(Convert.ToChar("a"))
            BW.Write(Convert.ToChar("t"))
            BW.Write(Convert.ToChar("a"))
 
            BW.Write(_dataLength)
 
            MS.Position = 0
        Return MS
    End Function
 
    Private Sub appendToBase(ByVal path As String)
        Dim newFile As New wavFile
        Dim baseFile As New wavFile
        Dim MSMergedFiles As New MemoryStream
        Dim BW As New BinaryWriter(MSMergedFiles)
 
            With newFile
                .loadFile(path)
                _bitsPerSample = .bitsPerSample
                _channels = .channels
                _sampleRate = .sampleRate
            End With
 
            With baseFile
                .dataLength = _dataLength
                .length = _length
                .data = _data
            End With
 
            _dataLength = newFile.dataLength + baseFile.dataLength
            _length = newFile.length + baseFile.length
            BW.Write(baseFile.data)
            BW.Write(newFile.data)
 
            MSMergedFiles.Position = 0
            _data = ToByte(MSMergedFiles)
            BW.Close()
 
    End Sub
 
#Region "Members"
 
    Public Sub append(ByVal path As String)
        If (String.IsNullOrEmpty(path)) Then
            Throw New ArgumentNullException("path")
        End If
        If File.Exists(path) = False Then
            path = "C:\voice\ding.wav"
        End If
 
        If _dataLength = 0 Then
            loadFile(path)
        Else
            appendToBase(path)
        End If
 
    End Sub
 
    Private Function ToByte(ByVal MS As Stream) As Byte()
            Dim streamLength As Integer = Convert.ToInt32(MS.Length)
            Dim buffer As Byte() = New Byte(streamLength - 1) {}
            MS.Read(buffer, 0, streamLength)
            MS.Close()
            Return buffer
    End Function
 
    Public Sub ToFile(ByVal path As String)
            Dim FS As FileStream = New FileStream(path, FileMode.Create, FileAccess.Write)
            Dim BW As BinaryWriter = New BinaryWriter(FS)
 
            BW.Write(ToByte(createHeader()))
            BW.Write(_data)
 
            BW.Close()
            FS.Close()
    End Sub
 
    Public Function toStream() As Stream
        Dim MS As New MemoryStream
        Dim BW As New BinaryWriter(MS)
            BW.Write(ToByte(createHeader()))
            BW.Write(_data)
            MS.Position = 0
        Return MS
    End Function
 
#End Region
 

End Class

GeneralAll in memory Pinmembershem79-Mar-09 7:17 
QuestionThank & need haelp PinmemberMember 286830623-Jan-09 7:56 
QuestionStrange "clicks" ? Pinmemberjaph13-May-08 11:12 
AnswerRe: Strange "clicks" ? Pinmemberthebeekeeper13-May-08 13:25 
AnswerRe: Strange "clicks" ? Pinmemberwee_z_lee2-Oct-08 7:17 
QuestionGetting weird background noise with a 8000hz, 1 channel, 64kbps file PinmemberLosBear3-Apr-08 8:03 
AnswerRe: Getting weird background noise with a 8000hz, 1 channel, 64kbps file PinmemberGolph4217-Nov-09 9:30 
Generalwave files Pinmemberc21127-Sep-07 19:54 
GeneralThanks Pinmembermikhail_z21-Sep-07 11:24 
GeneralSimple Awesome.......! Pinmemberamiashu17-Jul-07 12:25 
JokeRefactored a bit :P [modified] PinmemberFocusedWolf17-Jul-07 10:40 
GeneralVery Inspiring PinmemberSujoy G15-Jul-07 19:30 
Generalsignal reduced Pinmemberakok011127-Mar-07 6:43 
GeneralGreat thank you PinmemberA.J.Bauer21-Mar-07 1:16 
GeneralRe: Great thank you PinmemberA.J.Bauer21-Mar-07 3:58 
Generalprob with this PinmemberSwapnil173321-Feb-07 20:43 
GeneralRe: prob with this Pinmembermav.northwind10-Mar-07 23:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.1 | Last Updated 10 Mar 2007
Article Copyright 2006 by Ehab Mohamed Essa
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid