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

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

fix #2089 - Add support for MP3, AIFF and AAC audio codecs (.mp3, .aac, .aif, .aiff files) if Java FX is on the classpath (i.e. Windows, macOS, nearly all major Linux distributions). The classes are not public on purpose, as the whole system will have to be simplified when all Linux distributions propose Java FX and so we can get rid of old Java Sound implementation.

  • 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;
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 */
26public 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}
Note: See TracBrowser for help on using the repository browser.