For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > May 2006 > How to get the length of wav file?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to get the length of wav file?
lgj573@gmail.com

2006-05-16, 3:58 am

I have tried Audio::Wav module,but it cann't get the length of a
compressed file such as u-law 8bit 8khz.Is there any other module will
do?

bdj

2006-05-16, 7:00 pm

Hi!
Maybe you can use
-s $filename

or

filestat?

/Bjoern

<lgj573@gmail.com> skrev i en meddelelse
news:1147755682.037544.231790@i39g2000cwa.googlegroups.com...
>I have tried Audio::Wav module,but it cann't get the length of a
> compressed file such as u-law 8bit 8khz.Is there any other module will
> do?
>



lgj573@gmail.com

2006-05-17, 3:58 am

Here is the code.But here will be some problem if I is called.
public static double getLength(String path) throws Exception {
AudioInputStream stream;
stream = AudioSystem.getAudioInputStream(new URL(path));
AudioFormat format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits() * 2,
format.getChannels(),
format.getFrameSize() * 2,
format.getFrameRate(),
true); // big endian
stream = AudioSystem.getAudioInputStream(format, stream);
}
DataLine.Info info = new DataLine.Info(
Clip.class, stream.getFormat(), ((int)
stream.getFrameLength() * format.getFrameSize()));
Clip clip = (Clip) AudioSystem.getLine(info);
clip.close();
return clip.getBufferSize() / (clip.getFormat().getFrameSize()
* clip.getFormat().getFrameRate());
}

Eric Schwartz

2006-05-17, 6:58 pm

"lgj573@gmail.com" <lgj573@gmail.com> writes:
> Here is the code.But here will be some problem if I is called.
> public static double getLength(String path) throws Exception {


This looks like Java. If you want help with Java code, you should
probably ask your question on a Java newsgroup. If you're using Perl,
File::Headerinfo::WAV will probably do what you want; I haven't used
it, but it claims to "extract useful information like their duration
and filesize" from WAV files. Audio::WAV also includes methods for
that, but it's more oriented towards working with the contents of WAV
files. If you need to do that sort of thing, it looks useful.

-=Eric
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com