Author Topic: Reading Header file of a (RIFF) Wav File to get Attributes  (Read 78 times)

bmclellan

  • Newbie
  • *
  • Posts: 20
Reading Header file of a (RIFF) Wav File to get Attributes
« on: November 05, 2018, 12:27:36 pm »
Hello,

I was looking at some VB.NET examples and one was a cool way to get the header information out of a .wav file.

I posted the full link in below, however here is a snippet of code that I'm not sure how to do in Winbatch.

Has anyone tried reading a .wav file and looking at particular bytes like they have below? I looked at the BinaryConvert function but I don't think that will work for me.
            wavFileStream.Read(header, 0, 44)
            myHeader = header
            myFormat = System.BitConverter.ToInt16(header, 20)
            myChannels = System.BitConverter.ToInt16(header, 22)
            mySampleRate = System.BitConverter.ToInt32(header, 24)
            myByteRate = System.BitConverter.ToInt32(header, 28)
            myBlockAlign = System.BitConverter.ToInt16(header, 32)
            myBitsPerSample = System.BitConverter.ToInt16(header, 34)
            myDataSize = System.BitConverter.ToInt32(header, 40)

https://social.msdn.microsoft.com/Forums/vstudio/en-US/35850506-8771-45d4-99fd-01a74856a4ec/wave-file-attributes

Thanks!
Barry

td

  • Tech Support
  • *****
  • Posts: 2618
    • WinBatch
Re: Reading Header file of a (RIFF) Wav File to get Attributes
« Reply #1 on: November 05, 2018, 01:18:23 pm »
The example you reverence is just using the "System.BitConverter" dotNet framework (FCL) class to convert raw binary data to the appropriate type representation.  The documentation for the class can be found here:

https://docs.microsoft.com/en-us/dotnet/api/system.bitconverter?view=netframework-4.7.2

Since WIL .a.k.a. WinBatch supports CLR hosting you can likely just create and call the class members using WIL CLR related functions.

The easier approach may be to use the WIL binary functions to read the file into a binary buffer and just reading the various locations with the appropriate "peek" functions.   
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


td

  • Tech Support
  • *****
  • Posts: 2618
    • WinBatch
Re: Reading Header file of a (RIFF) Wav File to get Attributes
« Reply #2 on: November 05, 2018, 01:36:50 pm »
The example does not address your specific needs but it does show the use of the BinaryPeek* functions to obtain binary header information (all be it a different type of file and header.)

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/Bitmaps~and~Clipboard+How~to~Return~Bitmap~Dimensions.txt

and here is an example using the "WMPlayer.OCX" COM Automation object that may or may not be useful:

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/OLE~COM~ADO~CDO~ADSI~LDAP+Get~Audio~File~Information.txt
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


bmclellan

  • Newbie
  • *
  • Posts: 20
Re: Reading Header file of a (RIFF) Wav File to get Attributes
« Reply #3 on: November 05, 2018, 03:56:55 pm »
Thanks for your help Tony!

Below is a not so fancy example of gathering that information out of a wav file.

soundfile = "C:\Windows\Media\Windows Critical Stop.wav"

fs=FileSize(soundfile)
binbuf = BinaryAlloc(fs+100)
If binbuf == 0
   Message("Error", "BinaryAlloc Failed")
Else
   BinaryRead(binbuf, soundfile)

   ChunkID       = BinaryPeekStr(binbuf,0,4)
   FileLength    = BinaryPeek4(binbuf,4)    ;In Bytes
   Format       = BinaryPeekStr(binbuf,8,4)
   if ChunkID == "RIFF" then
      Subchunk1ID = BinaryPeekStr(binbuf,12,4)
      
      ;vb.net - System.BitConverter.ToInt16(binbuf, 20) - WB BinaryPeek2
      ;vb.net - System.BitConverter.ToInt32(binbuf, 20) - WB BinaryPeek4
      Subchunk1Size     = BinaryPeek4(binbuf, 16)
      AudioFormat       = BinaryPeek2(binbuf, 20)    ;1 = PCM; values other than 1 indicate some form of compression (linear quantization)
      NumChannels       = BinaryPeek2(binbuf, 22)    ;Mono = 1, Stereo = 2, etc.
      SampleRate        = BinaryPeek4(binbuf, 24)    ;8000, 44100, kHz etc.
      ByteRate          = BinaryPeek4(binbuf, 28)    ;= SampleRate * NumChannels * BitsPerSample/8
      BlockAlign        = BinaryPeek4(binbuf, 32)    ;= NumChannels * BitsPerSample/8
      BitsPerSample     = BinaryPeek2(binbuf, 34)    ;8 bits = 8, 16 bits = 16, etc.
      DataSize          = BinaryPeek4(binbuf, 40)   ;length of the data block
   end if
   ;Bit Rate = 60 ( seconds in a minute ) X SampleRate X NumChannels X (BitsPerSample / 8 ) (8 bits in a byte - 2 Bytes for 16 bit)

   binbuf=BinaryFree(binbuf)
EndIf
Message("BinaryRead", "Done.")

ChuckC

  • Full Member
  • ***
  • Posts: 162
Re: Reading Header file of a (RIFF) Wav File to get Attributes
« Reply #4 on: November 06, 2018, 05:57:34 am »
If this same information appears in the "Details" property page on the file properties panel in the Windows Explorer, then Property Store Utility extender can also obtain the information without any need for the decoding of binary information read directly from the file.

bmclellan

  • Newbie
  • *
  • Posts: 20
Re: Reading Header file of a (RIFF) Wav File to get Attributes
« Reply #5 on: November 06, 2018, 06:56:04 am »
Hi Chuck,

I don't think it does, but I have not heard of this extender, is it new? Sounds very interesting!


Barry

ChuckC

  • Full Member
  • ***
  • Posts: 162
Re: Reading Header file of a (RIFF) Wav File to get Attributes
« Reply #6 on: November 06, 2018, 07:10:54 am »
It's a 3rd party extender that I wrote some time ago.  It leverages the built-in Property Store feature of the Windows Explorer shell.

Just did a quick test with the "PSExplorer.wbt" sample script that's included in the help file for the Property Store Utility extender, and loaded up the list of properties that are accessible for a WAV file that's included with MS Office.  It appears that most, if not all, of the audio metadata that you were trying to decode is already supported as well-known properties, with names such as "System.Audio.ChannelCount", "System.Audio.Encodingbitrate", "System.Audio.Format", "System.Audio.SampleRate", "System.Audio.StreamNumber", etc...

Apparently, there are more properties defined than are shown in the "Details" property page on the Explorer's Properties dialog box.  Given that your mileage may vary, the best way to find out if properties are already defined for the file type in question is to simply run the PSExplorer.wbt script and have it use the extender to access a file of the desired type and see what you get.


bmclellan

  • Newbie
  • *
  • Posts: 20
Re: Reading Header file of a (RIFF) Wav File to get Attributes
« Reply #7 on: November 06, 2018, 10:03:42 am »
Thanks Chuck! Where is the link to your extenders?



Barry

ChuckC

  • Full Member
  • ***
  • Posts: 162
Re: Reading Header file of a (RIFF) Wav File to get Attributes
« Reply #8 on: November 06, 2018, 10:23:59 am »
A bit of searching around in the tech support database under Extenders / 3rd party Extenders turned up an article about the extender as well as a download link for it:

http://techsupt.winbatch.com/techsupt/WWPSU45001.zip


bmclellan

  • Newbie
  • *
  • Posts: 20
Re: Reading Header file of a (RIFF) Wav File to get Attributes
« Reply #9 on: November 06, 2018, 10:52:45 am »
Thanks Tony,

I was checking the Winbatch download link and thought it would be under there.


Barry

td

  • Tech Support
  • *****
  • Posts: 2618
    • WinBatch
Re: Reading Header file of a (RIFF) Wav File to get Attributes
« Reply #10 on: November 06, 2018, 01:29:31 pm »
I suspect it was an oversite and you actually meant "Thanks, Chuck."  He wrote the extender and offered up the link to its location in the Tech Database.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates