Search code examples
vb.netaudiomemorystreambinarywritertone-generator

Why write to BinaryWriter twice?


I'm implementing this tone-generator program and it works great:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/c2b953b6-3c85-4eda-a478-080bae781319/beep-beep?forum=vbgeneral

What I can't figure out, is why the following two lines of code:

BW.Write(Sample)
BW.Write(Sample)

One "write" makes sense, but why the second "write"?


Solution

  • The example is a bit cryptic but the wave file is configured to be 2 channels thus the two writes are simply sending the same audio data to both channels.

    The wave header is this hardcoded bit:

    Dim Hdr() As Integer = {&H46464952, 36 + Bytes, &H45564157, _
                            &H20746D66, 16, &H20001, 44100, _
                            176400, &H100004, &H61746164, Bytes}
    

    Which decoded means:

        H46464952 = 'RIFF' (little endian)
        36+Bytes  = Length of header + length of data
        H45564157 = 'WAVE' (little endian)
        H20746D66 = 'fmt ' (little endian)
        16        = length of fmt chunk (always 16)
        H20001    = 0x0001: PCM, 
                    0x0002: 2 channels
        44100     = sampleRate
        176400    = sampleRate*numChannels*bytesPerSample = 44100*2*2
        H100004   = 0x0004: numChannels*bytesPerSample, 
                    0x0010: bitsPerSample (16)
        H61746164 = 'data'
        Bytes     = size of data chunk