By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,972 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,972 IT Pros & Developers. It's quick & easy.

Why do these two methods produce diffrent output

P: n/a
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
Mar 30 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Chad,

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

Cor

"Chad Miller" <ch***@predictiveconcepts.comschreef in bericht
news:46***********************@roadrunner.com...
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

Mar 31 '07 #2

P: n/a
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
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Chad,

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

Cor

"Chad Miller" <ch***@predictiveconcepts.comschreef in bericht
news:46***********************@roadrunner.com...
>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


Mar 31 '07 #3

P: n/a
So what's different about the output?

Robin S.
--------------------
"Chad Miller" <ch***@predictiveconcepts.comwrote in message
news:46***********************@roadrunner.com...
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
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>Chad,

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

Cor

"Chad Miller" <ch***@predictiveconcepts.comschreef in bericht
news:46***********************@roadrunner.com.. .
>>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



Apr 1 '07 #4

P: n/a
Chad Miller wrote:
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
Apr 2 '07 #5

P: n/a
Chad Miller wrote:
<snip>
>Public Sub CreateWaveHeader(ByRef sPath As String)
<snip'n'edit>
FileOpen(fileNumber, sPath, OpenMode.Binary, OpenAccess.Write)

'Write the RIFF bit of the header first.
FilePut(fileNumber, "RIFF", 1)
<snip>
>End Sub
>Public Sub CreateWaveHeaderEx(ByRef sPath As String)
<snip'm'edit>
Using bw As New BinaryWriter(File.Open(sPath, _
FileMode.OpenOrCreate, FileAccess.Write))
'Write the RIFF bit of the header first.
bw.Write("RIFF")
<snip>
>End Sub
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.

Apr 3 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.