0

Iam trying to merge two .wav files into another file. but I could see only first file's data in the created file.

but the newly created file occupies the space which is equals the sum of the size of the source files.

foreach (string sourceFile in fileNamesList)
            {
              FileStream file=  File.Open(sourceFile, FileMode.Open);
              FileStream outFile = File.Open(output, FileMode.Append,FileAccess.Write);
                byte[] buffer = new byte[file.Length];
                int read;
                if ((read=file.Read(buffer, 0, (int)file.Length))>0)
                {

                    outFile.Write(buffer, 0, read);

                }

                file.Close();
                file.Dispose();
                outFile.Close();
                outFile.Dispose();

            }

thanks

Ramesh T
  • 106
  • 1
  • 7
  • What about metadata? Are you sure that it is possible to just merge those files? – rebeliagamer Jul 29 '13 at 14:48
  • I mean the duration of the resultant file is equals to the duration of the 1st file in fileNameList. But Its size=sum of size of all file in list. – Ramesh T Jul 29 '13 at 14:56

1 Answers1

1

You can't just concatenate two WAV files because they have a header which defines the format, number of channels, sample rate, length etc.

You will need to read and parse the header file for each separate WAV file and then write a new header to a new file with the correct data and then append the data contents from each WAV file.

You will not easily be able to concatenate two WAV files which have different sample rates or number of channels, but otherwise it's not too hard (once you've worked out the header format).

See here for details about the header format:

https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

http://en.wikipedia.org/wiki/WAV

Perhaps the easiest way will to be to use a third party tool such as Naudio to do this, as described here:

How to join 2 or more .WAV files together programatically?

Community
  • 1
  • 1
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Getting Argument Exception was unhandled,Must read complete blocks: requested 1024, block align is 65 at while loop. – Ramesh T Jul 30 '13 at 05:49