WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: bmclellan on November 05, 2018, 12:27:36 PM

Title: Reading Header file of a (RIFF) Wav File to get Attributes
Post by: bmclellan 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
Title: Re: Reading Header file of a (RIFF) Wav File to get Attributes
Post by: td 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.   
Title: Re: Reading Header file of a (RIFF) Wav File to get Attributes
Post by: td 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
Title: Re: Reading Header file of a (RIFF) Wav File to get Attributes
Post by: bmclellan 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.")
Title: Re: Reading Header file of a (RIFF) Wav File to get Attributes
Post by: ChuckC 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.
Title: Re: Reading Header file of a (RIFF) Wav File to get Attributes
Post by: bmclellan 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
Title: Re: Reading Header file of a (RIFF) Wav File to get Attributes
Post by: ChuckC 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.

Title: Re: Reading Header file of a (RIFF) Wav File to get Attributes
Post by: bmclellan on November 06, 2018, 10:03:42 AM
Thanks Chuck! Where is the link to your extenders?



Barry
Title: Re: Reading Header file of a (RIFF) Wav File to get Attributes
Post by: ChuckC 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

Title: Re: Reading Header file of a (RIFF) Wav File to get Attributes
Post by: bmclellan 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
Title: Re: Reading Header file of a (RIFF) Wav File to get Attributes
Post by: td 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.