Posts: 421
Threads: 22
Joined: Oct 2005
Is there an easy way to determine the length of a wav file, as in playback time, not file size?
In the beginning, there is darkness â the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
Posts: 6,419
Threads: 74
Joined: Mar 2002
Yeah, maths. But you have to know bit depth/number of channels/samples per second.
size_in_bytes = bytes_per_sample * channels * samples_per_second * seconds
So:
seconds = size_in_bytes / (bytes_per_sample * channels * samples_per_second).
For example, for a typical CD-quality file (44100 Hz, 16 bits, 2 channels):
seconds = size_in_bytes / (2 * 2 * 44100)
(more or less, there's a header in the file - I can't remember its size - assuming it's "X" just change that "size_in_bytes" in the above formulae for "(size_in_bytes - X)").
Posts: 838
Threads: 17
Joined: Jan 2002
Problem is that there can be a lot of different "chunks" in a wave file, besides the actual wave data - such as cues, notes, lyrics and what not. I guess they're rarely used (and if you made your own mp3's you could make sure they dont go there), but the potential's there.
Have a look here for the file format:
http://www.sonicspot.com/guide/wavefiles.html
The most secure way would be to read the header, look thru the chunks for data ones, and add up the size of those, then calculate with the bits/sec from the header.
But as I said, if you're only reading your own waves (or converted by you), going by nathan's suggestion you should be fine.
url=http://www.copy-pasta.com]CopyPasta[/url] -
FilePasta
Posts: 6,419
Threads: 74
Joined: Mar 2002
Yup, the optimal solution would be reading the header, extracting data about the different chunks in the file, and adding everything up. All the needed data I mentioned above (bit depth, n. of channels, etc...) is in there.