This project has moved. For the latest updates, please go here.

Concatenating 2 or more .wav file in vb.net

Jan 3, 2015 at 7:20 PM
I updated a answer found at http://stackoverflow.com/a/6778791/7532 from C# to VB but I get a strange result. The audio is faster than it should be and the 3 wav files (5 seconds, 1 second and 6 second) amount to a file of just over 6 seconds. All the files have the same waveformat (22050Hz Rate, mono, 32bit float).

In this project I am using NAudio.

My code:
Public Shared Sub Concatenate(outputFile As String, sourceFiles As IEnumerable(Of String))
    Dim buffer As Byte() = New Byte(1023) {}
    Dim waveFileWriter As WaveFileWriter = Nothing
    Try
        For Each sourceFile As String In sourceFiles
            Using reader As New WaveFileReader(sourceFile)
                If waveFileWriter Is Nothing Then
                    ' first time in create new Writer
                    waveFileWriter = New WaveFileWriter(outputFile, reader.WaveFormat)
                Else
                    If Not reader.WaveFormat.Equals(waveFileWriter.WaveFormat) Then
                        Throw New InvalidOperationException("Can't concatenate WAV Files that don't share the same format")
                    End If
                End If
                Dim read As Integer
                While (reader.Read(buffer, 0, buffer.Length) > 0)
                    read = reader.Read(buffer, 0, buffer.Length)
                    waveFileWriter.Write(buffer, 0, read)
                End While
            End Using
        Next
    Finally
        If waveFileWriter IsNot Nothing Then
            waveFileWriter.Dispose()
        End If
    End Try
End Sub
The function will be called like this:
Dim m_oEnum As IEnumerable(Of String) = New List(Of String)() From {"c:\1.wav", "c:\2.wav", "c:\3.wav"}
Concatenate("c:\joined.wav", m_oEnum)
Can anyone help me with this? I have a suspicion that it may have something to do with the sample format being 32 bit float.
Coordinator
Jan 9, 2015 at 10:12 PM
the main problem with this conversion into VB.NET is that you are calling reader.Read twice in the while loop and ignoring the results of the first. Just do While true, and then if read = 0 break out of the while loop