Click here to Skip to main content
15,078,444 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm trying to merge three .wav files, but always one of the files at the end is accelerated and distorted (as if it had been reproduced in an accelerated way). What could be happening?


C#
string[] files = new string[3] { @"Comprimento.wav", @"sample_Brazil.wav", @"autografo1.wav" };

WaveIO wa = new WaveIO();
wa.Merge(files, @"audioautografo.wav");


Join class:
<pre lang="c#">        
class WaveIO
        {
            public int length;
            public short channels;
            public int samplerate;
            public int DataLength;
            public short 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();

            }

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

                BinaryWriter bw = new BinaryWriter(fs);
                fs.Position = 0;
                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();
            }
            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;

                }

                //Recontruct 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();
                }
            }


        }


What I have tried:

I've tried, but to no avail:
- https://stackoverflow.com/questions/6777340/how-to-join-2-or-more-wav-files-together-programmatically
- https://stackoverflow.com/questions/340646/how-to-concatenate-wav-files-in-c-sharp
- https://www.codeproject.com/Articles/35725/C-WAV-file-class-audio-mixing-and-some-light-audio
Posted
Updated 4-Aug-20 16:54pm
Comments
Garth J Lancaster 4-Aug-20 22:27pm
   
Does it only happen if @"autografo1.wav" is on the end or ANY file on the end ?
José Madureira 4-Aug-20 22:52pm
   
If I remove autografo1.wa from the end, leaving only two audios, the Length.wav will be distorted but as if it had been extended in time

1 solution

Ok, you could try GitHub - naudio/NAudio: Audio and MIDI library for .NET[^] example NAudio/MixTwoAudioFilesToWav.md at master · naudio/NAudio · GitHub[^]

Else you're going to have to look at Merge() and WaveHeaderOUT and see if something in an individual file is corrupting the length or bitrate or such
   
v2
Comments
José Madureira 5-Aug-20 10:04am
   
Please, how do I use it? Do you have any examples?
Garth J Lancaster 5-Aug-20 23:31pm
   
the second link I included in the response was to the particular join wav files together example - they only had 2 files, trivial to use 3...n
José Madureira 6-Aug-20 19:39pm
   
Thank you! It works perfectly for audio recordings, but when you place generated audio APIs generated by Text to Speech (Azure).

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900