1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.net.URL;
|
---|
7 |
|
---|
8 | import javax.sound.sampled.AudioFormat;
|
---|
9 | import javax.sound.sampled.AudioInputStream;
|
---|
10 | import javax.sound.sampled.AudioSystem;
|
---|
11 | import javax.sound.sampled.UnsupportedAudioFileException;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Utils functions for audio.
|
---|
17 | *
|
---|
18 | * @author David Earl <david@frankieandshadow.com>
|
---|
19 | * @since 1462
|
---|
20 | */
|
---|
21 | public 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 | Main.debug(e);
|
---|
44 | return 0.0;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|