Ignore:
Timestamp:
2008-02-25T14:59:31+01:00 (18 years ago)
Author:
david
Message:

implement fast/slow forward

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/AudioPlayer.java

    r559 r563  
    3838        private double bytesPerSecond;
    3939        private static long chunk = 8000; /* bytes */
     40        private double speed = 1.0;
    4041
    4142        /**
     
    4849                private URL url;
    4950                private double offset; // seconds
    50 
     51                private double speed; // ratio
     52               
    5153                /*
    5254                 * Called to execute the commands in the other thread
    5355                 */
    54                 protected void play(URL url, double offset) throws Exception {
     56                protected void play(URL url, double offset, double speed) throws Exception {
    5557                        this.url = url;
    5658                        this.offset = offset;
     59                        this.speed = speed;
    5760                        command = Command.PLAY;
    5861                        result = Result.WAITING;
     
    8588                        return offset;
    8689                }
     90                protected double speed() {
     91                        return speed;
     92                }
    8793                protected URL url() {
    8894                        return url;
     
    102108         */
    103109        public static void play(URL url) throws Exception {
    104                 AudioPlayer.play(url, 0.0);
     110                AudioPlayer.get().command.play(url, 0.0, 1.0);
    105111        }
    106112       
     
    112118         */
    113119        public static void play(URL url, double seconds) throws Exception {
    114                 AudioPlayer.get().command.play(url, seconds);
     120                AudioPlayer.get().command.play(url, seconds, 1.0);
     121        }
     122       
     123        /**
     124         * Plays a WAV audio file from a specified position at variable speed.
     125         * @param url The resource to play, which must be a WAV file or stream
     126         * @param seconds The number of seconds into the audio to start playing
     127         * @param speed Rate at which audio playes (1.0 = real time, > 1 is faster)
     128         * @throws audio fault exception, e.g. can't open stream,  unhandleable audio format
     129         */
     130        public static void play(URL url, double seconds, double speed) throws Exception {
     131                AudioPlayer.get().command.play(url, seconds, speed);
    115132        }
    116133       
     
    155172        }
    156173       
     174        /**
     175         * Speed at which we will play.
     176         * @returns double, speed multiplier
     177         */
     178        public static double speed() {
     179                return AudioPlayer.get().speed;
     180        }
     181
    157182        /**
    158183         *  gets the singleton object, and if this is the first time, creates it along with
     
    241266                                        case PLAY:     
    242267                                                double offset = command.offset();
     268                                                speed = command.speed();
    243269                                                if (playingUrl != command.url() ||
    244270                                                        stateChange != State.PAUSED ||
     
    252278                                                        audioInputStream = AudioSystem.getAudioInputStream(playingUrl);
    253279                                                        audioFormat = audioInputStream.getFormat();
    254                                                         DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
    255280                                                        long nBytesRead = 0;
    256281                                                        position = 0.0;
     
    258283                                                        bytesPerSecond = audioFormat.getFrameRate() /* frames per second */
    259284                                                                * audioFormat.getFrameSize() /* bytes per frame */;
     285                                                        if (speed * bytesPerSecond > 256000.0)
     286                                                                speed = 256000 / bytesPerSecond;
    260287                                                        if (offset != 0.0 && adjustedOffset > 0.0) {
    261288                                                                long bytesToSkip = (long)(
     
    275302                                                                position = adjustedOffset;
    276303                                                        }
    277                                                         if (audioOutputLine == null) {
    278                                                                 audioOutputLine = (SourceDataLine) AudioSystem.getLine(info);
    279                                                                 audioOutputLine.open(audioFormat);
    280                                                                 audioOutputLine.start();
    281                                                         }
     304                                                        if (audioOutputLine != null)
     305                                                                audioOutputLine.close();
     306                                                        audioFormat = new AudioFormat(audioFormat.getEncoding(),
     307                                                                                audioFormat.getSampleRate() * (float) speed,
     308                                                                                audioFormat.getSampleSizeInBits(),
     309                                                                                audioFormat.getChannels(),
     310                                                                                audioFormat.getFrameSize(),
     311                                                                                audioFormat.getFrameRate() * (float) speed,
     312                                                                                audioFormat.isBigEndian());
     313                                                        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
     314                                                        audioOutputLine = (SourceDataLine) AudioSystem.getLine(info);
     315                                                        audioOutputLine.open(audioFormat);
     316                                                        audioOutputLine.start();
    282317                                                }
    283318                                                stateChange = State.PLAYING;
Note: See TracChangeset for help on using the changeset viewer.