tags:

views:

36

answers:

1

I'm trying to use the NAudio library to do some InLine conversion of some soundeffects for Playback with my application using the Media Soundplayer for the actual playback. (I find NAudio playback is a bit jittery for direct playback, hence the Conversion)

I really don't want to be extracting the files to disk to playback instead i'd like to convert them to WAV's in memory using a memoryStream.

Looking at the NAudio source it appears that it doesn't write some of the data to the Stream until it is disposed, but this therefore invalidates the MemoryStream. Should I rework that or am I doing it all wrong?

using the following code I get an invalidheader.

If I replace the code and write the file to disk and dispose then reload to a stream everything is fine (but it kinda defeats the purpose)

Private Sub PlaySound(ByVal ms As Stream)
    Dim ss As System.Media.SoundPlayer
    ss = New System.Media.SoundPlayer
    ss.Stream = ms
    ss.Load()
    Try
        ss.PlaySync()
    Catch ex As InvalidOperationException
        Debug.Print(ex.ToString)
    End Try
End Sub

Private Sub ConvertToStream()
    Using reader As New Mp3FileReader("C:\KMSounds\sound12.mp3")
        Using convertedStream As WaveStream = WaveFormatConversionStream.CreatePcmStream(reader)
            'File.Delete("c:\test2.wav")
            'Using ms As New FileStream("c:\test2.wav", FileMode.Create)
            Using ms As New MemoryStream
                Using w As New WaveFileWriter(ms, convertedStream.WaveFormat)
                    Dim buffer() As Byte = CType(Array.CreateInstance(GetType(Byte), convertedStream.GetReadSize(4000)), Byte())
                    While True
                        Dim bytesRead As Integer = convertedStream.Read(buffer, 0, buffer.Length)
                        If (bytesRead = 0) Then
                            w.Flush()
                            Exit While
                        Else
                            w.WriteData(buffer, 0, bytesRead)
                        End If
                    End While
                    PlaySound(ms)
                End Using
            End Using
        End Using
    End Using
End Sub

System.InvalidOperationException: The wave header is corrupt. at System.Media.SoundPlayer.ValidateSoundData(Byte[] data) at System.Media.SoundPlayer.LoadAndPlay(Int32 flags) at System.Media.SoundPlayer.PlaySync() at NAudioConvert.ConvertAudio.Playfile(Stream ms) in C:\Development\Spikes\NAudioConvert\ConvertAudio.vb:line 78

Any tips regarding this? or maybe another library that I could use to convert some very small MP3 files to WAV for playback would be great.

A: 

it is only when you dispose the WaveFileWriter that it goes back to the WAV header and fills in the correct number of data bytes (saves doing it on every call to WriteData). However, for your needs, you'd be best off making a custom WaveFileWriter that does keep the data bytes value up to date. I'm considering adding this as an option for a future version of NAudio

Mark Heath
Thanks Mark, i'll have a look at how I can go about doing that :-)
Paul Farry