0

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"?

swabygw
  • 813
  • 1
  • 10
  • 22
  • You could answer this yourself: comment out one of them and run it and see what the result is – Ňɏssa Pøngjǣrdenlarp Jan 08 '16 at 18:55
  • I tried this and got strange results. If I comment out the first one, I get an error on the SP.Play() command. If I comment out the second one, I get the tone, but one octave higher. Can't explain these results... – swabygw Jan 08 '16 at 19:32
  • It's possible that the second write is just flushing the first write – Camilo Terevinto Jan 08 '16 at 21:10
  • There's actually a Flush command after these two statements which clears out the BinaryWriter (BW) and pushes the data into the MemoryStream (MS). But, it could be, I suppose...I'm thinking there may be a more specific purpose. – swabygw Jan 08 '16 at 21:16
  • 1
    It's possible that it is a stereo stream and the two writes just send the same samples to both channels - interleaved. – jaket Jan 10 '16 at 18:02

1 Answers1

1

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
jaket
  • 9,140
  • 2
  • 25
  • 44
  • Great explanation. Where can I find some documentation on these values? Oh, and what did "endian" mean? Was that supposed to be "median" or something else... – swabygw Jan 11 '16 at 18:32
  • http://soundfile.sapp.org/doc/WaveFormat/. Endian refers to the byte ordering - big endian 0x1234 is little endian 0x3412. If you look at the ascii codes for the strings you'll see it spells them out in reverse. – jaket Jan 11 '16 at 20:10
  • Awesome - thanks. I found some documentation online. – swabygw Jan 12 '16 at 22:09