Why do these two methods produce diffrent output

  • Thread starter Thread starter Chad Miller
  • Start date Start date
C

Chad Miller

Public mLength As Integer
Public mChannels As Short
Public mSampleRate As Integer
Public mDataLength As Integer
Public mBitsPerSample As Short

Public Sub CreateWaveHeader(ByRef sPath As String)
Dim fileNumber As Integer

Try
fileNumber = FreeFile()

FileOpen(fileNumber, sPath, OpenMode.Binary, OpenAccess.Write)

'Write the RIFF bit of the header first.
FilePut(fileNumber, "RIFF", 1)
FilePut(fileNumber, mLength, 5)

'Then do the wave header.
FilePut(fileNumber, "WAVEfmt ", 9)
FilePut(fileNumber, CInt(16), 17)
FilePut(fileNumber, CShort(1), 21)
FilePut(fileNumber, mChannels, 23)
FilePut(fileNumber, mSampleRate, 25)
FilePut(fileNumber, CInt(mSampleRate * ((mBitsPerSample *
mChannels) / 8)), 29)
FilePut(fileNumber, CShort((mBitsPerSample * mChannels) / 8),
33)
FilePut(fileNumber, mBitsPerSample, 35)
FilePut(fileNumber, "data", 37)
FilePut(fileNumber, mDataLength, 41)
Catch ex As Exception
Throw ex
Finally
FileClose(fileNumber)
End Try
End Sub

Public Sub CreateWaveHeaderEx(ByRef sPath As String)
Try
Using bw As New BinaryWriter(File.Open(sPath,
FileMode.OpenOrCreate, FileAccess.Write))
'Write the RIFF bit of the header first.
bw.Write("RIFF")
bw.Write(mLength)

'Then the wave header.
bw.Write("WAVEfmt ")
bw.Write(CInt(16))
bw.Write(CShort(1))
bw.Write(mChannels)
bw.Write(mSampleRate)
bw.Write(CInt(mSampleRate * ((mBitsPerSample * mChannels) /
8)))
bw.Write(CShort((mBitsPerSample * mChannels) / 8))
bw.Write(mBitsPerSample)
bw.Write("data")
bw.Write(mDataLength)
bw.Flush()
End Using
Catch ex As Exception
Throw ex
End Try
End Sub
 
Chad,

In my idea is your sample to long for a newsgroup, can you bring it back to
the essentials?

Cor
 
Thanks Cor,

I need to leave the two methods in my post. I am writting a wav header out
to a file. They should both create the same result. The only diffrence is
that one uses a binary writer and the other uses the older FilePut method.

- Chad
 
Chad said:
FilePut(fileNumber, "RIFF", 1)
bw.Write("RIFF")

I suspect you might need the StringIsFixedLength parameter for the first one
of those lines to produce the same output as the second.

Andrew
 
Chad Miller wrote:
Public Sub CreateWaveHeader(ByRef sPath As String)
FileOpen(fileNumber, sPath, OpenMode.Binary, OpenAccess.Write)

'Write the RIFF bit of the header first.
FilePut(fileNumber, "RIFF", 1)
Public Sub CreateWaveHeaderEx(ByRef sPath As String)
Using bw As New BinaryWriter(File.Open(sPath, _
FileMode.OpenOrCreate, FileAccess.Write))
'Write the RIFF bit of the header first.
bw.Write("RIFF")

Because BinaryWriter.Write(String) puts a length prefix before the
actual chars. You need to get the chars from the string and write
*them*.

<aircode>
'Force Ansi encoding (chars are 1 byte,
'non-ascii chars are valid
Dim Ansi As System.Text.Encoding = _
System.Text.Encoding.GetEncoding(1252)

Try
Using bw As New BinaryWriter( _
File.Open(sPath, FileMode.OpenOrCreate, FileAccess.Write), _
Ansi)
'Write the RIFF bit of the header first.
bw.Write("RIFF".ToCharArray)

'I guess you can takje from here...
</aircode>

HTH.

Regards,

Branco.
 
Back
Top