Reading Header file of a (RIFF) Wav File to get Attributes

Started by bmclellan, November 05, 2018, 12:27:36 PM

Previous topic - Next topic

bmclellan

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

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.   
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

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
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

bmclellan

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

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

Hi Chuck,

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


Barry

ChuckC

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

Thanks Chuck! Where is the link to your extenders?



Barry

ChuckC

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

Thanks Tony,

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


Barry

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade