Using BitConverter
 Private Sub GetDat(ByVal fn As String)
      Dim idx1 As Integer
      Dim chunksize, wavechunksize As Int32
      Dim samplerate, bytespersecond As Int32
      Dim subchunk2size As Int32
      Dim wavetype, numchannels As Int16
      Dim blockalign, bitspersample As Int16
      Dim rif, wav, fmt, subchunk2id As String
      Dim buffer(100000) As Byte
      ' "RIFF"                   bytes 0 to 3
      ' filesize-8 (normally)    bytes 4 to 7
      ' "WAVE"                   bytes 8 to 11 
      ' "fmt "                   bytes 12 to 15
      ' size of wave chunk       bytes 16 to 19
      ' wave type format         bytes 20 to 21
      ' number of channels       bytes 22 to 23
      ' sample rate              bytes 24 to 27
      ' bytes per second         bytes 28 to 31    
      ' block align              bytes 32 and 33
      ' bits per sample          bytes 34 to 35
      ' sub chunk 2 id           bytes 36 to 39
      ' sub chunk 2 size         bytes 40 to 43
      Dim fs As New FileStream(fn, FileMode.Open)
      Dim br As New BinaryReader(fs)
      br.Read(buffer, 0, 44)
      rif = BitConverter.ToString(buffer, 0, 4) 
      chunksize = BitConverter.ToInt32(buffer, 4) 
      wav = BitConverter.ToString(buffer, 8, 4)  
      fmt = BitConverter.ToString(buffer, 12, 4) 
      wavechunksize = BitConverter.ToInt32(buffer, 16)  
      wavetype = BitConverter.ToInt16(buffer, 20)     
      numchannels = BitConverter.ToInt16(buffer, 22) 
      samplerate = BitConverter.ToInt32(buffer, 24)  
      bytespersecond = BitConverter.ToInt32(buffer, 28)  
      blockalign = BitConverter.ToInt16(buffer, 32)      
      bitspersample = BitConverter.ToInt16(buffer, 34)   
      subchunk2id = BitConverter.ToString(buffer, 36, 4) 
      subchunk2size = BitConverter.ToInt32(buffer, 40)  
      br.Read(buffer, 44, 256)
    
 End Sub
 
 This is the first part of the wav file that was loaded
 This is a standard wave file
 : 49 : 46 : 46 : C0 : 5F : 01 : 00 : 57 : 41 : 56 : 45 : 66 : 6D : 74 : 20 : 10
 : 00 : 00 : 00 : 01 : 00 : 01 : 00 : 44 : AC : 00 : 00 : 88 : 58 : 01 : 00 : 02
 : 00 : 10 : 00 : 64 : 61 : 74 : 61 : 9C : 5F : 01 : 00 : 6F : 00 : 83 : 00 : 84
 : 00 : 89 : 00 : 85 : 00 : 82 : 00 : 7B : 00 : 78 : 00 : 71 : 00 : 71 : 00 : 71
 : 00 : 74 : 00 : 76 : 00 : 7B : 00 : 7C : 00 : 7D : 00 : 7F : 00 : 7B : 00 : 7B
 : 00 : 78 : 00 : 77 : 00 : 73 : 00 : 79 : 00 : 72 : 00 : 7A : 00 : 78 : 00 : 7A
 : 00 : 7C : 00 : 7A : 00 : 7B : 00 : 7B : 00 : 77 : 00 : 79 : 00 : 76 : 00 : 77
 : 00 : 78 : 00 : 76 : 00 : 7B : 00 : 77 : 00 : 7C : 00 : 78 : 00 : 7B : 00 : 7A
   

This example opens a basic wave file and explores the contents. Bitconverter is ideal for exploring byte data.

Bitconverter.ToInt32 is passed an array of bytes and the position within the array from which to read. Since Int32 is 32 bits long it reads exactly 4 bytes (8 bits). 8 * 4 = 32. Bitconverter.ToInt16 is likewise passed an array of bytes and the position from which to read. Since Int16 is 16 bits long it knows to read exactly two bytes.

BitConverter.ToString is passed the array, the starting posistion within the array, and the number of bytes to read. The returned result is not an ASCII text string representing the text itself, but a string with the hexidecimal values of each byte of the requested bytes separated with dashes "-". The wave file should return 4 strings of interest in order to verify that it is indeed a simple wave file. These are "RIFF", "fmt ", "WAVE", and "DATA". I did no validation in this example for brevity's sake. But if you don't find those four strings in the locations that it is expected, then it is probably not a simple or canonical wave file.

Once you have established the validity of the file you can inspect the other data within the header, which in this case is 44 bytes long. When you know the number of channels, bytes alignment, amount of data, you can examine the data itself which is stored in location 44 and coninues to the end of the file.



Now back to bitconverter. Part of the benefit of Bitconverter is that is reformats little endian format to human readable format. Little endian format stores its four bytes of data in the reverse order of what you would expect. So if you read four bytes as (in hex) AA, BB, CC, DD in that order, it really means the hexidecimal value CCDDAABB, not AABBCCDD as you may expect. BitConverter.ToInt32 turns this around into what you would as a human expect.

10/11/2015
TheLonelyProgrammer.Net
Alone in a sea of non-programmers
Home Bit Converter Write/Read Bytes