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

Last change on this file since 12652 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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