Mark Witt
Mark Witt

Reputation: 43

NAudio splitting wav file produces 0 second wav files

I am using NAudio to split an full Album into several audio files. The program runs smooth and theres no exceptions, but in the end, the output audio files are not listenable and they get shown as 0 seconds.

Here is my code; albumPath describes the path to a folder where the single files will be stored, filePath is the path to the input file.

        private void splitWavPrep(string albumPath,string filePath)
    {

        MediaFoundationReader reader = new MediaFoundationReader(filePath);
        int bytesPerMilliSecond = reader.WaveFormat.AverageBytesPerSecond / 1000;
        for (int i = 0; i < tracks.Count; i++)
        {
            string trackName = tracks[i].Name;
            int startMilliSeconds = (int) tracks[i].StartSeconds * 1000;
            int duration;
            if (i< tracks.Count - 1)
            {
                //if there's a next track
                duration = (int)(tracks[i + 1].StartSeconds*1000) - startMilliSeconds;
            }
            else
            {
                //this is the last track
                duration = (int) reader.TotalTime.TotalMilliseconds - startMilliSeconds;
            }
            int startPos = startMilliSeconds * bytesPerMilliSecond;
            startPos = startPos - startPos % reader.WaveFormat.BlockAlign;
            int endMilliSeconds = startMilliSeconds + duration;
            int endBytes = (endMilliSeconds - startMilliSeconds) * bytesPerMilliSecond;
            endBytes = endBytes - endBytes % reader.WaveFormat.BlockAlign;

            int endPos = startPos + endBytes;

            string trackPath = Path.Combine(albumPath, trackName + ".wav");
            splitWav(trackPath, startPos, endPos, reader);

        }
    }

    private async void splitWav(string trackPath, int startPos, int endPos, MediaFoundationReader reader)
    {

        int progress = 0;
        WaveFileWriter writer = new WaveFileWriter(trackPath, reader.WaveFormat); 
        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)
                {
                    await writer.WriteAsync(buffer, 0, bytesRead);
                    progress += bytesRead;
                }
            }
        }
    }

I never dealt with audio files before and I don't know about bitrates and all that stuff. If you have an idea or a suggestion, please tell me because I'm stranded here.

Upvotes: 0

Views: 453

Answers (1)

Mark Heath
Mark Heath

Reputation: 49482

You need to Dispose the WaveFileWriter for the WAV header to get formatted correctly.

Update your splitWav method:

using(WaveFileWriter writer = new WaveFileWriter(trackPath, reader.WaveFormat)) 
{
   // your code here
}

Also, you need to use the Write method instead of WriteAsync, as Write will keep track of the number of bytes written to the data chunk

Upvotes: 2

Related Questions