Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to learn .wav duration in Java media frame work?

Tags:

java

audio

wav

jmf

I am tyring to merge an .mov file with a .wav file using java media framework, thus I need to know their duration. How can I do this? Any ideas would be appreciated..

like image 555
yasemin Avatar asked Dec 13 '22 22:12

yasemin


2 Answers

you can learn the duration of sound files using this way(that is VitalyVal's second way):

  import java.net.URL;

        import javax.sound.sampled.AudioFormat;
        import javax.sound.sampled.AudioInputStream;
        import javax.sound.sampled.AudioSystem;
        import javax.sound.sampled.Clip;
        import javax.sound.sampled.DataLine;

        public class SoundUtils {
            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());
            }

            public static void main(String[] args) {
                try {

                    System.out
                            .println(getLength("..."));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
like image 61
ibrahimyilmaz Avatar answered Dec 28 '22 21:12

ibrahimyilmaz


I tried the accepted answer, but after getting exceptions with it, I decided to just do it the simple way.

If you have a few basic pieces of information, you can figure audio data length. The main pieces are:

  • Sample rate
  • Sample size (bits per sample)
  • File size
  • Number of channels (mono/stereo)

If you have these, you can find out duration. Java is nice because it provides libraries to easily retrieve all of this information with minimal effort. Equation is below:

sample-rate * sample-size * duration * channel-count = file-size

See the code below that does this calculation in java:

public static double getDurationOfWavInSeconds(File file)
{   
    AudioInputStream stream = null;

    try 
    {
        stream = AudioSystem.getAudioInputStream(file);

        AudioFormat format = stream.getFormat();

        return file.length() / format.getSampleRate() / (format.getSampleSizeInBits() / 8.0) / format.getChannels();
    }
    catch (Exception e) 
    {
        // log an error
        return -1;
    }
    finally
    {
        try { stream.close(); } catch (Exception ex) { }
    }
}

Hope this helps someone out there! I've only tested it with WAV files, though.

like image 33
Ben Avatar answered Dec 28 '22 21:12

Ben