Why do these two methods produce diffrent output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chad Miller

    #1

    Why do these two methods produce diffrent output

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

    Public Sub CreateWaveHeade r(ByRef sPath As String)
    Dim fileNumber As Integer

    Try
    fileNumber = FreeFile()

    FileOpen(fileNu mber, sPath, OpenMode.Binary , OpenAccess.Writ e)

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

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

    Public Sub CreateWaveHeade rEx(ByRef sPath As String)
    Try
    Using bw As New BinaryWriter(Fi le.Open(sPath,
    FileMode.OpenOr Create, FileAccess.Writ e))
    'Write the RIFF bit of the header first.
    bw.Write("RIFF" )
    bw.Write(mLengt h)

    'Then the wave header.
    bw.Write("WAVEf mt ")
    bw.Write(CInt(1 6))
    bw.Write(CShort (1))
    bw.Write(mChann els)
    bw.Write(mSampl eRate)
    bw.Write(CInt(m SampleRate * ((mBitsPerSampl e * mChannels) /
    8)))
    bw.Write(CShort ((mBitsPerSampl e * mChannels) / 8))
    bw.Write(mBitsP erSample)
    bw.Write("data" )
    bw.Write(mDataL ength)
    bw.Flush()
    End Using
    Catch ex As Exception
    Throw ex
    End Try
    End Sub


  • Cor Ligthert [MVP]

    #2
    Re: Why do these two methods produce diffrent output

    Chad,

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

    Cor

    "Chad Miller" <chadm@predicti veconcepts.coms chreef in bericht
    news:460d817d$0 $17199$4c368faf @roadrunner.com ...
    Public mLength As Integer
    Public mChannels As Short
    Public mSampleRate As Integer
    Public mDataLength As Integer
    Public mBitsPerSample As Short
    >
    Public Sub CreateWaveHeade r(ByRef sPath As String)
    Dim fileNumber As Integer
    >
    Try
    fileNumber = FreeFile()
    >
    FileOpen(fileNu mber, sPath, OpenMode.Binary , OpenAccess.Writ e)
    >
    'Write the RIFF bit of the header first.
    FilePut(fileNum ber, "RIFF", 1)
    FilePut(fileNum ber, mLength, 5)
    >
    'Then do the wave header.
    FilePut(fileNum ber, "WAVEfmt ", 9)
    FilePut(fileNum ber, CInt(16), 17)
    FilePut(fileNum ber, CShort(1), 21)
    FilePut(fileNum ber, mChannels, 23)
    FilePut(fileNum ber, mSampleRate, 25)
    FilePut(fileNum ber, CInt(mSampleRat e * ((mBitsPerSampl e *
    mChannels) / 8)), 29)
    FilePut(fileNum ber, CShort((mBitsPe rSample * mChannels) / 8),
    33)
    FilePut(fileNum ber, mBitsPerSample, 35)
    FilePut(fileNum ber, "data", 37)
    FilePut(fileNum ber, mDataLength, 41)
    Catch ex As Exception
    Throw ex
    Finally
    FileClose(fileN umber)
    End Try
    End Sub
    >
    Public Sub CreateWaveHeade rEx(ByRef sPath As String)
    Try
    Using bw As New BinaryWriter(Fi le.Open(sPath,
    FileMode.OpenOr Create, FileAccess.Writ e))
    'Write the RIFF bit of the header first.
    bw.Write("RIFF" )
    bw.Write(mLengt h)
    >
    'Then the wave header.
    bw.Write("WAVEf mt ")
    bw.Write(CInt(1 6))
    bw.Write(CShort (1))
    bw.Write(mChann els)
    bw.Write(mSampl eRate)
    bw.Write(CInt(m SampleRate * ((mBitsPerSampl e * mChannels) /
    8)))
    bw.Write(CShort ((mBitsPerSampl e * mChannels) / 8))
    bw.Write(mBitsP erSample)
    bw.Write("data" )
    bw.Write(mDataL ength)
    bw.Flush()
    End Using
    Catch ex As Exception
    Throw ex
    End Try
    End Sub
    >

    Comment

    • Chad Miller

      #3
      Re: Why do these two methods produce diffrent output

      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]" <notmyfirstname @planet.nlwrote in message
      news:%23b1qPC1c HHA.1244@TK2MSF TNGP04.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" <chadm@predicti veconcepts.coms chreef in bericht
      news:460d817d$0 $17199$4c368faf @roadrunner.com ...
      >Public mLength As Integer
      >Public mChannels As Short
      >Public mSampleRate As Integer
      >Public mDataLength As Integer
      >Public mBitsPerSample As Short
      >>
      >Public Sub CreateWaveHeade r(ByRef sPath As String)
      > Dim fileNumber As Integer
      >>
      > Try
      > fileNumber = FreeFile()
      >>
      > FileOpen(fileNu mber, sPath, OpenMode.Binary , OpenAccess.Writ e)
      >>
      > 'Write the RIFF bit of the header first.
      > FilePut(fileNum ber, "RIFF", 1)
      > FilePut(fileNum ber, mLength, 5)
      >>
      > 'Then do the wave header.
      > FilePut(fileNum ber, "WAVEfmt ", 9)
      > FilePut(fileNum ber, CInt(16), 17)
      > FilePut(fileNum ber, CShort(1), 21)
      > FilePut(fileNum ber, mChannels, 23)
      > FilePut(fileNum ber, mSampleRate, 25)
      > FilePut(fileNum ber, CInt(mSampleRat e * ((mBitsPerSampl e *
      >mChannels) / 8)), 29)
      > FilePut(fileNum ber, CShort((mBitsPe rSample * mChannels) / 8),
      >33)
      > FilePut(fileNum ber, mBitsPerSample, 35)
      > FilePut(fileNum ber, "data", 37)
      > FilePut(fileNum ber, mDataLength, 41)
      > Catch ex As Exception
      > Throw ex
      > Finally
      > FileClose(fileN umber)
      > End Try
      > End Sub
      >>
      > Public Sub CreateWaveHeade rEx(ByRef sPath As String)
      > Try
      > Using bw As New BinaryWriter(Fi le.Open(sPath,
      >FileMode.OpenO rCreate, FileAccess.Writ e))
      > 'Write the RIFF bit of the header first.
      > bw.Write("RIFF" )
      > bw.Write(mLengt h)
      >>
      > 'Then the wave header.
      > bw.Write("WAVEf mt ")
      > bw.Write(CInt(1 6))
      > bw.Write(CShort (1))
      > bw.Write(mChann els)
      > bw.Write(mSampl eRate)
      > bw.Write(CInt(m SampleRate * ((mBitsPerSampl e * mChannels)
      >/ 8)))
      > bw.Write(CShort ((mBitsPerSampl e * mChannels) / 8))
      > bw.Write(mBitsP erSample)
      > bw.Write("data" )
      > bw.Write(mDataL ength)
      > bw.Flush()
      > End Using
      > Catch ex As Exception
      > Throw ex
      > End Try
      > End Sub
      >>
      >
      >

      Comment

      • RobinS

        #4
        Re: Why do these two methods produce diffrent output

        So what's different about the output?

        Robin S.
        --------------------
        "Chad Miller" <chadm@predicti veconcepts.comw rote in message
        news:460e5367$0 $17203$4c368faf @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]" <notmyfirstname @planet.nlwrote in message
        news:%23b1qPC1c HHA.1244@TK2MSF TNGP04.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" <chadm@predicti veconcepts.coms chreef in bericht
        >news:460d817d$ 0$17199$4c368fa f@roadrunner.co m...
        >>Public mLength As Integer
        >>Public mChannels As Short
        >>Public mSampleRate As Integer
        >>Public mDataLength As Integer
        >>Public mBitsPerSample As Short
        >>>
        >>Public Sub CreateWaveHeade r(ByRef sPath As String)
        >> Dim fileNumber As Integer
        >>>
        >> Try
        >> fileNumber = FreeFile()
        >>>
        >> FileOpen(fileNu mber, sPath, OpenMode.Binary ,
        >>OpenAccess.Wr ite)
        >>>
        >> 'Write the RIFF bit of the header first.
        >> FilePut(fileNum ber, "RIFF", 1)
        >> FilePut(fileNum ber, mLength, 5)
        >>>
        >> 'Then do the wave header.
        >> FilePut(fileNum ber, "WAVEfmt ", 9)
        >> FilePut(fileNum ber, CInt(16), 17)
        >> FilePut(fileNum ber, CShort(1), 21)
        >> FilePut(fileNum ber, mChannels, 23)
        >> FilePut(fileNum ber, mSampleRate, 25)
        >> FilePut(fileNum ber, CInt(mSampleRat e * ((mBitsPerSampl e *
        >>mChannels) / 8)), 29)
        >> FilePut(fileNum ber, CShort((mBitsPe rSample * mChannels) /
        >>8), 33)
        >> FilePut(fileNum ber, mBitsPerSample, 35)
        >> FilePut(fileNum ber, "data", 37)
        >> FilePut(fileNum ber, mDataLength, 41)
        >> Catch ex As Exception
        >> Throw ex
        >> Finally
        >> FileClose(fileN umber)
        >> End Try
        >> End Sub
        >>>
        >> Public Sub CreateWaveHeade rEx(ByRef sPath As String)
        >> Try
        >> Using bw As New BinaryWriter(Fi le.Open(sPath,
        >>FileMode.Open OrCreate, FileAccess.Writ e))
        >> 'Write the RIFF bit of the header first.
        >> bw.Write("RIFF" )
        >> bw.Write(mLengt h)
        >>>
        >> 'Then the wave header.
        >> bw.Write("WAVEf mt ")
        >> bw.Write(CInt(1 6))
        >> bw.Write(CShort (1))
        >> bw.Write(mChann els)
        >> bw.Write(mSampl eRate)
        >> bw.Write(CInt(m SampleRate * ((mBitsPerSampl e *
        >>mChannels) / 8)))
        >> bw.Write(CShort ((mBitsPerSampl e * mChannels) / 8))
        >> bw.Write(mBitsP erSample)
        >> bw.Write("data" )
        >> bw.Write(mDataL ength)
        >> bw.Flush()
        >> End Using
        >> Catch ex As Exception
        >> Throw ex
        >> End Try
        >> End Sub
        >>>
        >>
        >>
        >
        >

        Comment

        • Andrew Morton

          #5
          Re: Why do these two methods produce diffrent output

          Chad Miller wrote:
          FilePut(fileNum ber, "RIFF", 1)
          bw.Write("RIFF" )
          I suspect you might need the StringIsFixedLe ngth parameter for the first one
          of those lines to produce the same output as the second.

          Andrew


          Comment

          • Branco Medeiros

            #6
            Re: Why do these two methods produce diffrent output

            Chad Miller wrote:
            <snip>
            >Public Sub CreateWaveHeade r(ByRef sPath As String)
            <snip'n'edit>
            FileOpen(fileNu mber, sPath, OpenMode.Binary , OpenAccess.Writ e)
            >
            'Write the RIFF bit of the header first.
            FilePut(fileNum ber, "RIFF", 1)
            <snip>
            >End Sub
            >Public Sub CreateWaveHeade rEx(ByRef sPath As String)
            <snip'm'edit>
            Using bw As New BinaryWriter(Fi le.Open(sPath, _
            FileMode.OpenOr Create, FileAccess.Writ e))
            'Write the RIFF bit of the header first.
            bw.Write("RIFF" )
            <snip>
            >End Sub
            Because BinaryWriter.Wr ite(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.Enc oding = _
            System.Text.Enc oding.GetEncodi ng(1252)

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

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

            HTH.

            Regards,

            Branco.

            Comment

            Working...