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

Last change on this file since 14628 was 14153, checked in by Don-vip, 6 years ago

see #15229 - deprecate Main.parent and Main itself

  • Property svn:eol-style set to native
File size: 2.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.audio;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GraphicsEnvironment;
7import java.io.File;
8import java.io.IOException;
9import java.net.URL;
10
11import javax.sound.sampled.AudioFormat;
12import javax.sound.sampled.AudioInputStream;
13import javax.sound.sampled.AudioSystem;
14import javax.sound.sampled.UnsupportedAudioFileException;
15import javax.swing.JOptionPane;
16
17import org.openstreetmap.josm.gui.MainApplication;
18import org.openstreetmap.josm.spi.preferences.Config;
19import 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 */
28public 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}
Note: See TracBrowser for help on using the repository browser.