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