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