1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.audio;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GraphicsEnvironment;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.net.URL;
|
---|
10 |
|
---|
11 | import javax.sound.sampled.AudioFormat;
|
---|
12 | import javax.sound.sampled.AudioInputStream;
|
---|
13 | import javax.sound.sampled.AudioSystem;
|
---|
14 | import javax.sound.sampled.UnsupportedAudioFileException;
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Utils functions for audio.
|
---|
21 | *
|
---|
22 | * @author David Earl <david@frankieandshadow.com>
|
---|
23 | * @since 12326 (move to new package)
|
---|
24 | * @since 1462
|
---|
25 | */
|
---|
26 | public final class AudioUtil {
|
---|
27 |
|
---|
28 | private AudioUtil() {
|
---|
29 | // Hide default constructor for utils classes
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Returns calibrated length of recording in seconds.
|
---|
34 | * @param wavFile the recording file (WAV format)
|
---|
35 | * @return the calibrated length of recording in seconds.
|
---|
36 | */
|
---|
37 | public static double getCalibratedDuration(File wavFile) {
|
---|
38 | try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
|
---|
39 | new URL("file:".concat(wavFile.getAbsolutePath())))) {
|
---|
40 | AudioFormat audioFormat = audioInputStream.getFormat();
|
---|
41 | long filesize = wavFile.length();
|
---|
42 | double bytesPerSecond = audioFormat.getFrameRate() /* frames per second */
|
---|
43 | * audioFormat.getFrameSize() /* bytes per frame */;
|
---|
44 | double naturalLength = filesize / bytesPerSecond;
|
---|
45 | double calibration = Main.pref.getDouble("audio.calibration", 1.0 /* default, ratio */);
|
---|
46 | return naturalLength / calibration;
|
---|
47 | } catch (UnsupportedAudioFileException | IOException e) {
|
---|
48 | Main.debug(e);
|
---|
49 | return 0.0;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Shows a popup audio error message for the given exception.
|
---|
55 | * @param ex The exception used as error reason. Cannot be {@code null}.
|
---|
56 | * @since 12328
|
---|
57 | */
|
---|
58 | public static void audioMalfunction(Exception ex) {
|
---|
59 | String msg = ex.getMessage();
|
---|
60 | if (msg == null)
|
---|
61 | msg = tr("unspecified reason");
|
---|
62 | else
|
---|
63 | msg = tr(msg);
|
---|
64 | Main.error(msg);
|
---|
65 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
66 | JOptionPane.showMessageDialog(Main.parent,
|
---|
67 | "<html><p>" + msg + "</p></html>",
|
---|
68 | tr("Error playing sound"), JOptionPane.ERROR_MESSAGE);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|