001    // License: GPL. Copyright 2009 by David Earl and others
002    package org.openstreetmap.josm.tools;
003    
004    import java.io.File;
005    import java.net.URL;
006    
007    import javax.sound.sampled.AudioFormat;
008    import javax.sound.sampled.AudioInputStream;
009    import javax.sound.sampled.AudioSystem;
010    
011    import org.openstreetmap.josm.Main;
012    
013    /**
014     * Returns calibrated length of recording in seconds.
015     *
016     * @author David Earl <david@frankieandshadow.com>
017     *
018     */
019    public class AudioUtil {
020        static public double getCalibratedDuration(File wavFile) {
021            try {
022                AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
023                    new URL("file:".concat(wavFile.getAbsolutePath())));
024                AudioFormat audioFormat = audioInputStream.getFormat();
025                long filesize = wavFile.length();
026                double bytesPerSecond = audioFormat.getFrameRate() /* frames per second */
027                    * audioFormat.getFrameSize() /* bytes per frame */;
028                double naturalLength = filesize / bytesPerSecond;
029                audioInputStream.close();
030                double calibration = Main.pref.getDouble("audio.calibration", "1.0" /* default, ratio */);
031                return naturalLength / calibration;
032            } catch (Exception e) {
033                return 0.0;
034            }
035        }
036    }