Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: Concatenate WAV files together brings out a file with longer duration

$
0
0
Hi,

I'm a newbie for NAudio, so please be patient with me... :-)

I'm concatenating many WAV files together using the code below.
My problem is that after the operation finishes, the result WAV file has longer duration (!) than all files together...
I'm checking the duration of a WAV file
public static bool Concatenate(string outputFile, IEnumerable<string> sourceFiles, bool checkFormat)
{
    byte[] buffer = new byte[1024];
    WaveFileWriter writer = null;

    try
    {
        foreach (string sourceFile in sourceFiles)
        {
            using (WaveFileReader reader = new WaveFileReader(sourceFile))
            {
                if (writer == null)
                    writer = new WaveFileWriter(outputFile, reader.WaveFormat);
                else if (checkFormat && !reader.WaveFormat.Equals(writer.WaveFormat))
                    return false;

                int read;
                while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
                    writer.Write(buffer, 0, read);
            }
        }
    }
    finally
    {
        if (writer != null)
        {
            writer.Dispose();
        }
    }

    return true;
}
I'm getting a file duration using:
public static TimeSpan GetDuration(string fileName)
{
    using (WaveFileReader wf = new WaveFileReader(fileName))
        return wf.TotalTime;
}
And I'm reading all files duration using:
public static void GetFolderTotalDuration(string folderPath, out TimeSpan totalDuration, out int countFiles)
{
    totalDuration = TimeSpan.Zero;
    countFiles = 0;
    foreach (var s in Directory.GetFiles(folderPath, "*.wav"))
    {
        countFiles++;
        totalDuration += GetDuration(s);
    }
}
What am I doing wrong? Why am I getting a longer file?

Viewing all articles
Browse latest Browse all 5831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>