source: josm/trunk/src/org/openstreetmap/josm/io/audio/AudioUtil.java@ 12326

Last change on this file since 12326 was 12326, checked in by Don-vip, 7 years ago

see #2089 - move audio classes to their own package (more to come) - AFAIK no plugin is concerned

  • Property svn:eol-style set to native
File size: 1.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.audio;
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 12326 (move to new package)
20 * @since 1462
21 */
22public final class AudioUtil {
23
24 private AudioUtil() {
25 // Hide default constructor for utils classes
26 }
27
28 /**
29 * Returns calibrated length of recording in seconds.
30 * @param wavFile the recording file (WAV format)
31 * @return the calibrated length of recording in seconds.
32 */
33 public static double getCalibratedDuration(File wavFile) {
34 try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
35 new URL("file:".concat(wavFile.getAbsolutePath())))) {
36 AudioFormat audioFormat = audioInputStream.getFormat();
37 long filesize = wavFile.length();
38 double bytesPerSecond = audioFormat.getFrameRate() /* frames per second */
39 * audioFormat.getFrameSize() /* bytes per frame */;
40 double naturalLength = filesize / bytesPerSecond;
41 double calibration = Main.pref.getDouble("audio.calibration", 1.0 /* default, ratio */);
42 return naturalLength / calibration;
43 } catch (UnsupportedAudioFileException | IOException e) {
44 Main.debug(e);
45 return 0.0;
46 }
47 }
48}
Note: See TracBrowser for help on using the repository browser.