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

Last change on this file since 10178 was 9144, checked in by Don-vip, 8 years ago

see #12229 - add unit tests for audio playback

  • Property svn:eol-style set to native
File size: 1.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.io.File;
5import java.io.IOException;
6import java.net.URL;
7
8import javax.sound.sampled.AudioFormat;
9import javax.sound.sampled.AudioInputStream;
10import javax.sound.sampled.AudioSystem;
11import javax.sound.sampled.UnsupportedAudioFileException;
12
13import org.openstreetmap.josm.Main;
14
15/**
16 * Utils functions for audio.
17 *
18 * @author David Earl <david@frankieandshadow.com>
19 * @since 1462
20 */
21public final class AudioUtil {
22
23 private AudioUtil() {
24 // Hide default constructor for utils classes
25 }
26
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 */
32 public static double getCalibratedDuration(File wavFile) {
33 try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
34 new URL("file:".concat(wavFile.getAbsolutePath())))) {
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;
40 double calibration = Main.pref.getDouble("audio.calibration", 1.0 /* default, ratio */);
41 return naturalLength / calibration;
42 } catch (UnsupportedAudioFileException | IOException e) {
43 if (Main.isDebugEnabled()) {
44 Main.debug(e.getMessage());
45 }
46 return 0.0;
47 }
48 }
49}
Note: See TracBrowser for help on using the repository browser.