source: josm/trunk/src/org/openstreetmap/josm/tools/AudioUtil.java@ 8073

Last change on this file since 8073 was 7037, checked in by Don-vip, 10 years ago

see #8465 - last batch of try-with-resources

  • Property svn:eol-style set to native
File size: 1.6 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[2512]2package org.openstreetmap.josm.tools;
3
4import java.io.File;
[7025]5import java.io.IOException;
[2512]6import java.net.URL;
7
8import javax.sound.sampled.AudioFormat;
9import javax.sound.sampled.AudioInputStream;
10import javax.sound.sampled.AudioSystem;
[7025]11import javax.sound.sampled.UnsupportedAudioFileException;
[2512]12
13import org.openstreetmap.josm.Main;
14
15/**
[5871]16 * Utils functions for audio.
[2512]17 *
[6830]18 * @author David Earl <david@frankieandshadow.com>
[5871]19 * @since 1462
[2512]20 */
[6362]21public final class AudioUtil {
[6070]22
[6362]23 private AudioUtil() {
24 // Hide default constructor for utils classes
25 }
[6830]26
[5871]27 /**
28 * Returns calibrated length of recording in seconds.
29 * @param wavFile the recording file (WAV format)
30 * @return the calibrated length of recording in seconds.
31 */
[6889]32 public static double getCalibratedDuration(File wavFile) {
[7033]33 try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
34 new URL("file:".concat(wavFile.getAbsolutePath())))) {
[2512]35 AudioFormat audioFormat = audioInputStream.getFormat();
36 long filesize = wavFile.length();
37 double bytesPerSecond = audioFormat.getFrameRate() /* frames per second */
38 * audioFormat.getFrameSize() /* bytes per frame */;
39 double naturalLength = filesize / bytesPerSecond;
[5871]40 double calibration = Main.pref.getDouble("audio.calibration", 1.0 /* default, ratio */);
[2512]41 return naturalLength / calibration;
[7025]42 } catch (UnsupportedAudioFileException | IOException e) {
[2512]43 return 0.0;
44 }
45 }
46}
Note: See TracBrowser for help on using the repository browser.