001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.tools;
003
004import java.io.File;
005import java.net.URL;
006
007import javax.sound.sampled.AudioFormat;
008import javax.sound.sampled.AudioInputStream;
009import javax.sound.sampled.AudioSystem;
010
011import org.openstreetmap.josm.Main;
012
013/**
014 * Utils functions for audio.
015 *
016 * @author David Earl <david@frankieandshadow.com>
017 * @since 1462
018 */
019public final class AudioUtil {
020
021    private AudioUtil() {
022        // Hide default constructor for utils classes
023    }
024    
025    /**
026     * Returns calibrated length of recording in seconds.
027     * @param wavFile the recording file (WAV format)
028     * @return the calibrated length of recording in seconds.
029     */
030    static public double getCalibratedDuration(File wavFile) {
031        try {
032            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
033                new URL("file:".concat(wavFile.getAbsolutePath())));
034            AudioFormat audioFormat = audioInputStream.getFormat();
035            long filesize = wavFile.length();
036            double bytesPerSecond = audioFormat.getFrameRate() /* frames per second */
037                * audioFormat.getFrameSize() /* bytes per frame */;
038            double naturalLength = filesize / bytesPerSecond;
039            Utils.close(audioInputStream);
040            double calibration = Main.pref.getDouble("audio.calibration", 1.0 /* default, ratio */);
041            return naturalLength / calibration;
042        } catch (Exception e) {
043            return 0.0;
044        }
045    }
046}