PDA

View Full Version : The Lengthof a WAV file



Unregistered
10-27-2003, 10:38 PM
I work at San Francisco's PBS station, where we use many "broadcast wav" files. Fool that I am I volunteered to write a VB program that would rename them, using as part of that name their length. Both work and home use W2K.

I have not found aor been able to write any code that gives me accurate length. dbPowerAmp's scripting abilities is my latest attempt. I put AOL's "You've Got Mail" file on my desktop, and then copied the following from the developer page and put put it also on my desktop:

"
Set Multiplayer = CreateObject("dBpowerAMP.Multiplayer") ' Access Multiplayer Object

Multiplayer.FilePlaying = "gotmail.wav"

le = Multiplayer.Length
msgbox (le)
"

It gave me a length of zero. Then I noticed that when I dropped the gotmail.wav" file into the MUltiplayer with drag and drop directly, it played but there was no length shown.

The same happened when I tried this code for VB's MMControl.ocx:

"
Public Function GetLength(fn As String) As Integer
'microsoft multimedia control (provided with vb )
Dim howlong As Integer

Form1.MMControl1.FileName = fn
Form1.MMControl1.DeviceType = "Waveaudio"

Form1.MMControl1.TimeFormat = mciFormatMilliseconds
Form1.MMControl1.Command = "open"
howlong = Form1.MMControl1.Length
Form1.MMControl1.Command = "close"

End Function
"

Could someone tell me just how I screwing up?

Thanks!

Spoon
10-28-2003, 05:10 PM
Your best bet - load all the Waves into My Music Collection, then Collection >> Export

depending upon how the files are named, just export Track Title, and Length. Then use some code to rename from there.

daren
10-31-2003, 08:00 AM
Hi,

It's a bit more complex, but I would read the BWF header from the file and
calculate the length in mins and secs based on the data length, sample rate,
(bitrate for MPEG) and bit depth values.

The bonus is that the program will be completely 'stand-alone'.

If you're interested I can dig-up the formula for you...

Best regards,
Daren.

Unregistered
11-02-2003, 09:31 PM
Hi,

It's a bit more complex, but I would read the BWF header from the file and
calculate the length in mins and secs based on the data length, sample rate,
(bitrate for MPEG) and bit depth values.

The bonus is that the program will be completely 'stand-alone'.

If you're interested I can dig-up the formula for you...

Best regards,
Daren.

I would appreciate that a lot! I have a .pdf file with the broadcast wave specification, which I could forward to you if you send me your email. Mine is birrell@well.com.

A stand-alone program is exactly what we need to get out of this project.

And thanks!

Birrell Walsh
San Francisco

daren
11-03-2003, 04:22 AM
Hi Birrell,

Thanks for the offer, but I already have the BWF specs docs.

You will need to parse the file for the 'fmt' chunk and read the data which
follows into the following data structure:

// "fmt" chunk
short wFormatTag; //audio data type
short nChannels; //mono or stereo
long nSamplesPerSec; //sample freq
long nAvgBytesPerSec; //bit rate/8 (not always used)
short nBlockAlign; //frame size
short wBitsPerSample; //Bit depth (only for linear files)
short cbSize; //22 - size of extra info
short fwHeadLayer;
long dwHeadBitRate; //bit rate
short fwHeadMode;
short fwHeadModeExt;
short wHeadEmphasis;
short fwHeadFlags;
long dwPTSLow;
long dwPTSHigh;

I couldn't find the formula (lost it somewhere), but this should work for linear
files:

length in secs=(datalength*8)/(SamplesPerSec*nChannels*wBitsPerSample)

For MPEG files you will have to use a multiplication factor based on the bitrate:
factor=1536/bitrate in kbps.

The "datalength" is the total filelength - size of the header, or the amount
of bytes given at the start of the "data" chunk.

The parsing is necessary due to the fact that the chunks can be in any
order in the file (or some chunks could be missing).

Sorry the above isn't in VB. You'll have to work that bit out ;-)

I find the best way to experiment is to examine sample files with a HEX editor
(with character representation of the values, for identifying the chunk tags).

Hope this helps,
Daren.

edgar23
12-11-2003, 09:38 PM
Hi. I'm not really familiar when it comes to working with file objects. How do i actually get the values of the datalength, SamplesPerSec, nChannels, wBitsPerSample?

By the way, Im using VB .NET.

Thanks so much. Hope you guys could help. :)

daren
12-12-2003, 11:06 AM
Hi. I'm not really familiar when it comes to working with file objects. How do i actually get the values of the datalength, SamplesPerSec, nChannels, wBitsPerSample?

By the way, Im using VB .NET.

Thanks so much. Hope you guys could help. :)

Hi Edgar,

Have a look here:

http://www.jwcs.net/developers/code/code_show.pl?l=vb&cat=Digital.Audio&aid=1

Best regards,
Daren.