/    Sign up×
Community /Pin to ProfileBookmark

Getting Audio length

Hi all

Is there any way to get the length of the audio file (wav file). I am in need of showing the approximate time the wav file will be played.

Thanks,

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@netbuddyDec 12.2006 — yeah, if you dig deep on the internet, you will find a php class for wav audio.
Copy linkTweet thisAlerts:
@pcthugDec 13.2006 — Well I search around for a while and couldn't find anything, so I ended up putting this together. It will return a 2 decimal float value in seconds.
[code=php]
/*
float getWavFilesize ( string file )

*/
function getWavFilesize($file)
{
if (strtoupper(pathinfo($file, PATHINFO_EXTENSION)) !== 'WAV')
{
trigger_error('File is not a Wav File.');
return false;
}
elseif (!$bin = @file_get_contents($file))
{
trigger_error('File either does not exist or is not readable.');
return false;
}

$wav['SubChunk1Size'] = unpack('V*', substr($bin, 16 ,4));
$wav['NumChannels'] = unpack('v*', substr($bin, 22, 2));
$wav['SampleRate'] = unpack('V*', substr($bin, 24, 4));
$wav['BitsPerSample'] = unpack('v*', substr($bin, 34, 2));

if ($wav['SubChunk1Size'][1] == 16)
{
$wav['SubChunk2Size'] = unpack('V*', substr($bin, 40, 4));
}
elseif ($wav['SubChunk1Size'][1] == 18)
{
$wav['SubChunk2Size'] = unpack('V*', substr($bin, 42, 4));
}

$wav['NumberSamples'] = ($wav['SubChunk2Size'][1] * 8) / ($wav['NumChannels'][1] * $wav['BitsPerSample'][1]);

return round($wav['NumberSamples'] / $wav['SampleRate'][1], 2);
}[/code]
Copy linkTweet thisAlerts:
@hifibeeauthorDec 13.2006 — Hi,

Thanks for ur reply. I've checked ur function with a sample audio file but it returned 0. i have also attached the sample audio file which i tested.

Thanks,

[upl-file uuid=ab80752b-4024-483f-a6c3-d0aef748ec4f size=59kB]wav1.zip[/upl-file]
Copy linkTweet thisAlerts:
@pcthugDec 14.2006 — Here is an updated version. It is more scalable as it does not have defined SubChunkSize's.[code=php]
/*
float getWavFilesize ( string file )

*/
function getWavFilesize($file)
{
if (strtoupper(pathinfo($file, PATHINFO_EXTENSION)) !== 'WAV')
{
trigger_error('File is not a Wav File.');
return false;
}
elseif (!$bin = @file_get_contents($file))
{
trigger_error('File either does not exist or is not readable.');
return false;
}

$wav['SubChunk1Size'] = unpack('V*', substr($bin, 16 ,4));
$wav['NumChannels'] = unpack('v*', substr($bin, 22, 2));
$wav['SampleRate'] = unpack('V*', substr($bin, 24, 4));
$wav['BitsPerSample'] = unpack('v*', substr($bin, 34, 2));
$wav['SubChunk2Size'] = unpack('V*', substr($bin, 24 + $wav['SubChunk1Size'][1], 4));
$wav['NumberSamples'] = ($wav['SubChunk2Size'][1] * 8) / ($wav['NumChannels'][1] * $wav['BitsPerSample'][1]);

return round($wav['NumberSamples'] / $wav['SampleRate'][1], 2);
}[/code]
Copy linkTweet thisAlerts:
@hifibeeauthorDec 14.2006 — Great

Its working fine. Thanks for your valuable support. thanks to netbuddy too.
×

Success!

Help @hifibee spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...