[547] | 1 | // License: GPL. Copyright 2008 by David Earl and others
|
---|
[626] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[547] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[582] | 6 | import java.io.IOException;
|
---|
| 7 | import java.net.URL;
|
---|
| 8 |
|
---|
| 9 | import javax.sound.sampled.AudioFormat;
|
---|
| 10 | import javax.sound.sampled.AudioInputStream;
|
---|
| 11 | import javax.sound.sampled.AudioSystem;
|
---|
| 12 | import javax.sound.sampled.DataLine;
|
---|
[559] | 13 | import javax.sound.sampled.SourceDataLine;
|
---|
[547] | 14 | import javax.swing.JOptionPane;
|
---|
| 15 |
|
---|
| 16 | import org.openstreetmap.josm.Main;
|
---|
[626] | 17 |
|
---|
| 18 | /**
|
---|
| 19 | * Creates and controls a separate audio player thread.
|
---|
[1169] | 20 | *
|
---|
[626] | 21 | * @author David Earl <david@frankieandshadow.com>
|
---|
[5871] | 22 | * @since 547
|
---|
[626] | 23 | */
|
---|
| 24 | public class AudioPlayer extends Thread {
|
---|
| 25 |
|
---|
[1169] | 26 | private static AudioPlayer audioPlayer = null;
|
---|
[547] | 27 |
|
---|
[1169] | 28 | private enum State { INITIALIZING, NOTPLAYING, PLAYING, PAUSED, INTERRUPTED }
|
---|
| 29 | private State state;
|
---|
[549] | 30 | private enum Command { PLAY, PAUSE }
|
---|
[547] | 31 | private enum Result { WAITING, OK, FAILED }
|
---|
| 32 | private URL playingUrl;
|
---|
| 33 | private double leadIn; // seconds
|
---|
[557] | 34 | private double calibration; // ratio of purported duration of samples to true duration
|
---|
[1169] | 35 | private double position; // seconds
|
---|
| 36 | private double bytesPerSecond;
|
---|
| 37 | private static long chunk = 4000; /* bytes */
|
---|
| 38 | private double speed = 1.0;
|
---|
[553] | 39 |
|
---|
[1169] | 40 | /**
|
---|
| 41 | * Passes information from the control thread to the playing thread
|
---|
| 42 | */
|
---|
| 43 | private class Execute {
|
---|
| 44 | private Command command;
|
---|
| 45 | private Result result;
|
---|
| 46 | private Exception exception;
|
---|
| 47 | private URL url;
|
---|
| 48 | private double offset; // seconds
|
---|
| 49 | private double speed; // ratio
|
---|
[626] | 50 |
|
---|
[1169] | 51 | /*
|
---|
| 52 | * Called to execute the commands in the other thread
|
---|
| 53 | */
|
---|
| 54 | protected void play(URL url, double offset, double speed) throws Exception {
|
---|
| 55 | this.url = url;
|
---|
| 56 | this.offset = offset;
|
---|
| 57 | this.speed = speed;
|
---|
| 58 | command = Command.PLAY;
|
---|
| 59 | result = Result.WAITING;
|
---|
| 60 | send();
|
---|
| 61 | }
|
---|
| 62 | protected void pause() throws Exception {
|
---|
| 63 | command = Command.PAUSE;
|
---|
| 64 | send();
|
---|
| 65 | }
|
---|
| 66 | private void send() throws Exception {
|
---|
| 67 | result = Result.WAITING;
|
---|
| 68 | interrupt();
|
---|
| 69 | while (result == Result.WAITING) { sleep(10); /* yield(); */ }
|
---|
[1865] | 70 | if (result == Result.FAILED)
|
---|
| 71 | throw exception;
|
---|
[1169] | 72 | }
|
---|
| 73 | private void possiblyInterrupt() throws InterruptedException {
|
---|
| 74 | if (interrupted() || result == Result.WAITING)
|
---|
| 75 | throw new InterruptedException();
|
---|
| 76 | }
|
---|
| 77 | protected void failed (Exception e) {
|
---|
| 78 | exception = e;
|
---|
| 79 | result = Result.FAILED;
|
---|
| 80 | state = State.NOTPLAYING;
|
---|
| 81 | }
|
---|
| 82 | protected void ok (State newState) {
|
---|
| 83 | result = Result.OK;
|
---|
| 84 | state = newState;
|
---|
| 85 | }
|
---|
| 86 | protected double offset() {
|
---|
| 87 | return offset;
|
---|
| 88 | }
|
---|
| 89 | protected double speed() {
|
---|
| 90 | return speed;
|
---|
| 91 | }
|
---|
| 92 | protected URL url() {
|
---|
| 93 | return url;
|
---|
| 94 | }
|
---|
| 95 | protected Command command() {
|
---|
| 96 | return command;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
[547] | 99 |
|
---|
[1169] | 100 | private Execute command;
|
---|
[547] | 101 |
|
---|
[1169] | 102 | /**
|
---|
| 103 | * Plays a WAV audio file from the beginning. See also the variant which doesn't
|
---|
| 104 | * start at the beginning of the stream
|
---|
| 105 | * @param url The resource to play, which must be a WAV file or stream
|
---|
[5871] | 106 | * @throws Exception audio fault exception, e.g. can't open stream, unhandleable audio format
|
---|
[1169] | 107 | */
|
---|
| 108 | public static void play(URL url) throws Exception {
|
---|
| 109 | AudioPlayer.get().command.play(url, 0.0, 1.0);
|
---|
| 110 | }
|
---|
[563] | 111 |
|
---|
[1169] | 112 | /**
|
---|
| 113 | * Plays a WAV audio file from a specified position.
|
---|
| 114 | * @param url The resource to play, which must be a WAV file or stream
|
---|
| 115 | * @param seconds The number of seconds into the audio to start playing
|
---|
[5871] | 116 | * @throws Exception audio fault exception, e.g. can't open stream, unhandleable audio format
|
---|
[1169] | 117 | */
|
---|
| 118 | public static void play(URL url, double seconds) throws Exception {
|
---|
| 119 | AudioPlayer.get().command.play(url, seconds, 1.0);
|
---|
| 120 | }
|
---|
[547] | 121 |
|
---|
[1169] | 122 | /**
|
---|
| 123 | * Plays a WAV audio file from a specified position at variable speed.
|
---|
| 124 | * @param url The resource to play, which must be a WAV file or stream
|
---|
| 125 | * @param seconds The number of seconds into the audio to start playing
|
---|
| 126 | * @param speed Rate at which audio playes (1.0 = real time, > 1 is faster)
|
---|
[5871] | 127 | * @throws Exception audio fault exception, e.g. can't open stream, unhandleable audio format
|
---|
[1169] | 128 | */
|
---|
| 129 | public static void play(URL url, double seconds, double speed) throws Exception {
|
---|
| 130 | AudioPlayer.get().command.play(url, seconds, speed);
|
---|
| 131 | }
|
---|
[547] | 132 |
|
---|
[1169] | 133 | /**
|
---|
| 134 | * Pauses the currently playing audio stream. Does nothing if nothing playing.
|
---|
[5871] | 135 | * @throws Exception audio fault exception, e.g. can't open stream, unhandleable audio format
|
---|
[1169] | 136 | */
|
---|
| 137 | public static void pause() throws Exception {
|
---|
| 138 | AudioPlayer.get().command.pause();
|
---|
| 139 | }
|
---|
[547] | 140 |
|
---|
[1169] | 141 | /**
|
---|
| 142 | * To get the Url of the playing or recently played audio.
|
---|
| 143 | * @return url - could be null
|
---|
| 144 | */
|
---|
| 145 | public static URL url() {
|
---|
| 146 | return AudioPlayer.get().playingUrl;
|
---|
| 147 | }
|
---|
[547] | 148 |
|
---|
[1169] | 149 | /**
|
---|
| 150 | * Whether or not we are paused.
|
---|
| 151 | * @return boolean whether or not paused
|
---|
| 152 | */
|
---|
| 153 | public static boolean paused() {
|
---|
| 154 | return AudioPlayer.get().state == State.PAUSED;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /**
|
---|
| 158 | * Whether or not we are playing.
|
---|
| 159 | * @return boolean whether or not playing
|
---|
| 160 | */
|
---|
| 161 | public static boolean playing() {
|
---|
| 162 | return AudioPlayer.get().state == State.PLAYING;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /**
|
---|
| 166 | * How far we are through playing, in seconds.
|
---|
| 167 | * @return double seconds
|
---|
| 168 | */
|
---|
| 169 | public static double position() {
|
---|
| 170 | return AudioPlayer.get().position;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /**
|
---|
| 174 | * Speed at which we will play.
|
---|
| 175 | * @return double, speed multiplier
|
---|
| 176 | */
|
---|
| 177 | public static double speed() {
|
---|
| 178 | return AudioPlayer.get().speed;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /**
|
---|
| 182 | * gets the singleton object, and if this is the first time, creates it along with
|
---|
| 183 | * the thread to support audio
|
---|
| 184 | */
|
---|
| 185 | private static AudioPlayer get() {
|
---|
| 186 | if (audioPlayer != null)
|
---|
| 187 | return audioPlayer;
|
---|
| 188 | try {
|
---|
| 189 | audioPlayer = new AudioPlayer();
|
---|
| 190 | return audioPlayer;
|
---|
| 191 | } catch (Exception ex) {
|
---|
| 192 | return null;
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[5871] | 196 | /**
|
---|
| 197 | * Resets the audio player.
|
---|
| 198 | */
|
---|
[1685] | 199 | public static void reset() {
|
---|
| 200 | if(audioPlayer != null)
|
---|
| 201 | {
|
---|
| 202 | try {
|
---|
| 203 | pause();
|
---|
| 204 | } catch(Exception e) {}
|
---|
| 205 | audioPlayer.playingUrl = null;
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[1169] | 209 | private AudioPlayer() {
|
---|
| 210 | state = State.INITIALIZING;
|
---|
| 211 | command = new Execute();
|
---|
| 212 | playingUrl = null;
|
---|
[5871] | 213 | leadIn = Main.pref.getDouble("audio.leadin", 1.0 /* default, seconds */);
|
---|
| 214 | calibration = Main.pref.getDouble("audio.calibration", 1.0 /* default, ratio */);
|
---|
[1169] | 215 | start();
|
---|
| 216 | while (state == State.INITIALIZING) { yield(); }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /**
|
---|
| 220 | * Starts the thread to actually play the audio, per Thread interface
|
---|
| 221 | * Not to be used as public, though Thread interface doesn't allow it to be made private
|
---|
| 222 | */
|
---|
| 223 | @Override public void run() {
|
---|
| 224 | /* code running in separate thread */
|
---|
| 225 |
|
---|
| 226 | playingUrl = null;
|
---|
| 227 | AudioInputStream audioInputStream = null;
|
---|
| 228 | SourceDataLine audioOutputLine = null;
|
---|
| 229 | AudioFormat audioFormat = null;
|
---|
| 230 | byte[] abData = new byte[(int)chunk];
|
---|
| 231 |
|
---|
| 232 | for (;;) {
|
---|
| 233 | try {
|
---|
| 234 | switch (state) {
|
---|
[1865] | 235 | case INITIALIZING:
|
---|
| 236 | // we're ready to take interrupts
|
---|
| 237 | state = State.NOTPLAYING;
|
---|
| 238 | break;
|
---|
| 239 | case NOTPLAYING:
|
---|
| 240 | case PAUSED:
|
---|
| 241 | sleep(200);
|
---|
| 242 | break;
|
---|
| 243 | case PLAYING:
|
---|
[1169] | 244 | command.possiblyInterrupt();
|
---|
[1865] | 245 | for(;;) {
|
---|
| 246 | int nBytesRead = 0;
|
---|
| 247 | nBytesRead = audioInputStream.read(abData, 0, abData.length);
|
---|
| 248 | position += nBytesRead / bytesPerSecond;
|
---|
| 249 | command.possiblyInterrupt();
|
---|
| 250 | if (nBytesRead < 0) { break; }
|
---|
| 251 | audioOutputLine.write(abData, 0, nBytesRead); // => int nBytesWritten
|
---|
| 252 | command.possiblyInterrupt();
|
---|
| 253 | }
|
---|
| 254 | // end of audio, clean up
|
---|
| 255 | audioOutputLine.drain();
|
---|
| 256 | audioOutputLine.close();
|
---|
| 257 | audioOutputLine = null;
|
---|
[5874] | 258 | Utils.close(audioInputStream);
|
---|
[1865] | 259 | audioInputStream = null;
|
---|
| 260 | playingUrl = null;
|
---|
| 261 | state = State.NOTPLAYING;
|
---|
[1169] | 262 | command.possiblyInterrupt();
|
---|
[1865] | 263 | break;
|
---|
[1169] | 264 | }
|
---|
| 265 | } catch (InterruptedException e) {
|
---|
| 266 | interrupted(); // just in case we get an interrupt
|
---|
| 267 | State stateChange = state;
|
---|
| 268 | state = State.INTERRUPTED;
|
---|
| 269 | try {
|
---|
| 270 | switch (command.command()) {
|
---|
[1865] | 271 | case PLAY:
|
---|
| 272 | double offset = command.offset();
|
---|
| 273 | speed = command.speed();
|
---|
| 274 | if (playingUrl != command.url() ||
|
---|
| 275 | stateChange != State.PAUSED ||
|
---|
| 276 | offset != 0.0)
|
---|
| 277 | {
|
---|
| 278 | if (audioInputStream != null) {
|
---|
[5874] | 279 | Utils.close(audioInputStream);
|
---|
[1865] | 280 | audioInputStream = null;
|
---|
| 281 | }
|
---|
| 282 | playingUrl = command.url();
|
---|
| 283 | audioInputStream = AudioSystem.getAudioInputStream(playingUrl);
|
---|
| 284 | audioFormat = audioInputStream.getFormat();
|
---|
| 285 | long nBytesRead = 0;
|
---|
| 286 | position = 0.0;
|
---|
| 287 | offset -= leadIn;
|
---|
| 288 | double calibratedOffset = offset * calibration;
|
---|
| 289 | bytesPerSecond = audioFormat.getFrameRate() /* frames per second */
|
---|
[1169] | 290 | * audioFormat.getFrameSize() /* bytes per frame */;
|
---|
[1865] | 291 | if (speed * bytesPerSecond > 256000.0) {
|
---|
| 292 | speed = 256000 / bytesPerSecond;
|
---|
[1169] | 293 | }
|
---|
[1865] | 294 | if (calibratedOffset > 0.0) {
|
---|
| 295 | long bytesToSkip = (long)(
|
---|
| 296 | calibratedOffset /* seconds (double) */ * bytesPerSecond);
|
---|
| 297 | /* skip doesn't seem to want to skip big chunks, so
|
---|
| 298 | * reduce it to smaller ones
|
---|
| 299 | */
|
---|
| 300 | // audioInputStream.skip(bytesToSkip);
|
---|
| 301 | while (bytesToSkip > chunk) {
|
---|
| 302 | nBytesRead = audioInputStream.skip(chunk);
|
---|
| 303 | if (nBytesRead <= 0)
|
---|
| 304 | throw new IOException(tr("This is after the end of the recording"));
|
---|
| 305 | bytesToSkip -= nBytesRead;
|
---|
| 306 | }
|
---|
| 307 | if (bytesToSkip > 0) {
|
---|
| 308 | audioInputStream.skip(bytesToSkip);
|
---|
| 309 | }
|
---|
| 310 | position = offset;
|
---|
| 311 | }
|
---|
| 312 | if (audioOutputLine != null) {
|
---|
| 313 | audioOutputLine.close();
|
---|
| 314 | }
|
---|
| 315 | audioFormat = new AudioFormat(audioFormat.getEncoding(),
|
---|
[1169] | 316 | audioFormat.getSampleRate() * (float) (speed * calibration),
|
---|
| 317 | audioFormat.getSampleSizeInBits(),
|
---|
| 318 | audioFormat.getChannels(),
|
---|
| 319 | audioFormat.getFrameSize(),
|
---|
| 320 | audioFormat.getFrameRate() * (float) (speed * calibration),
|
---|
| 321 | audioFormat.isBigEndian());
|
---|
[1865] | 322 | DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
|
---|
| 323 | audioOutputLine = (SourceDataLine) AudioSystem.getLine(info);
|
---|
| 324 | audioOutputLine.open(audioFormat);
|
---|
| 325 | audioOutputLine.start();
|
---|
| 326 | }
|
---|
| 327 | stateChange = State.PLAYING;
|
---|
| 328 | break;
|
---|
| 329 | case PAUSE:
|
---|
| 330 | stateChange = State.PAUSED;
|
---|
| 331 | break;
|
---|
[1169] | 332 | }
|
---|
| 333 | command.ok(stateChange);
|
---|
| 334 | } catch (Exception startPlayingException) {
|
---|
| 335 | command.failed(startPlayingException); // sets state
|
---|
| 336 | }
|
---|
| 337 | } catch (Exception e) {
|
---|
| 338 | state = State.NOTPLAYING;
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
| 342 |
|
---|
[5871] | 343 | /**
|
---|
[6070] | 344 | * Shows a popup audio error message for the given exception.
|
---|
[5871] | 345 | * @param ex The exception used as error reason. Cannot be {@code null}.
|
---|
| 346 | */
|
---|
[1169] | 347 | public static void audioMalfunction(Exception ex) {
|
---|
[3264] | 348 | String msg = ex.getMessage();
|
---|
| 349 | if(msg == null)
|
---|
| 350 | msg = tr("unspecified reason");
|
---|
| 351 | else
|
---|
| 352 | msg = tr(msg);
|
---|
[2017] | 353 | JOptionPane.showMessageDialog(Main.parent,
|
---|
[3264] | 354 | "<html><p>" + msg + "</p></html>",
|
---|
[1169] | 355 | tr("Error playing sound"), JOptionPane.ERROR_MESSAGE);
|
---|
| 356 | }
|
---|
[547] | 357 | }
|
---|