I'm reading a WAV file with non-standard data in the chunk (basically, the WAV file has, rightly or wrongly, ID tags in the chunk - which is something I want my code to handle so that it can clean up these kinds of files).
Sometimes, the following code is working fine when processing a non-stand chunk in a WAV file. The first two ReadChars statements always work, but in the first DO WHILE loop where I'm searching for 'fmt ', the binaryreader statement (
SubChunk1ID = brObject.ReadChars(4)
) sometimes (not always, depending on which file I'm processing) generates this error:
The output char buffer is too small to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback 'System.Text.DecoderReplacementFallback' Parameter name: chars
The DO WHILE loop does one successful ReadChars, bringing back the text "JUNK" (which is actually there in the file). On the second pass, it generates the error. The hex values that it attempts to read in the second pass are "F0 00 00 00".
Obviously there is some unusual encoding that is exceeding the length of a standard string variable (that's how it looks to me). No surprise, I guess, because this is binary data, not text.
I'm thinking I need to do something else besides ReadChar, but I'm not very experience in VB.Net and don't know what alternative would be the best to work with in getting encoded data out of a filestream.
Or perhaps there's something I'm missing about how ReadChars behaves in C# vs VB.Net, which I'm not accounting for.
Any thoughts and comments would be welcomed!
Class WaveIO
Public IsWaveFile As Boolean = False
Public ChunkID As String
Public ChunkSize As Integer
Public Format As String
Public SubChunk1ID As String
Public Subchunk1Size As UInt32
Public AudioFormat As UInt16
Public Channels As UInt16
Public SampleRate As UInt32
Public ByteRate As UInt32
Public BlockAlign As UInt16
Public BitsPerSample As UInt16
Public SubChunk2ID As String
Public Subchunk2Size As UInt32
Public FmtSubChunkOffset As Integer
Public DataSubChunkOffset As Integer
Public FileLength As UInt32
Public DataLength As UInt32
Public Sub WaveHeader(ByVal strPath As String)
Dim fsObject As FileStream
Dim brObject As BinaryReader
fsObject = New FileStream(strPath, FileMode.Open, FileAccess.Read)
brObject = New BinaryReader(fsObject)
Dim byteData() As Byte
Dim charData() As Char
ChunkID = brObject.ReadChars(4)
If ChunkID <> "RIFF" Then
IsWaveFile = False
Exit Sub
End If
ChunkSize = brObject.ReadInt32
Format = brObject.ReadChars(4)
If Format <> "WAVE" Then
IsWaveFile = False
Exit Sub
End If
ReDim byteData(3)
ReDim charData(3)
Do While 1 = 1
Try
SubChunk1ID = brObject.ReadChars(4)
Catch Ex As ArgumentException
MessageBox.Show(Ex.Message, "ArgumentException", MessageBoxButton.OK, MessageBoxImage.Information)
Catch ex As Exception
MessageBox.Show(ex.Message, "Unexpected", MessageBoxButton.OK, MessageBoxImage.Information)
Exit Sub
End Try
MessageBox.Show("SubChunk1ID = " & SubChunk1ID, "Test", MessageBoxButton.OK, MessageBoxImage.Information)
MessageBox.Show("Position = " & fsObject.Position.ToString, "Test", MessageBoxButton.OK, MessageBoxImage.Information)
MessageBox.Show("Length = " & fsObject.Length.ToString, "Test", MessageBoxButton.OK, MessageBoxImage.Information)
If SubChunk1ID = "fmt " Then
Exit Do
End If
fsObject.Position -= 3
If fsObject.Position >= fsObject.Length Then
IsWaveFile = False
Exit Sub
End If
Loop