jdk/src/java.desktop/share/classes/com/sun/media/sound/JavaSoundAudioClip.java
author prr
Fri, 04 Nov 2016 15:59:59 -0700
changeset 42198 6ff366cc096b
parent 40444 afabcfc2f3ef
permissions -rw-r--r--
8168316: Suppress deprecation warnings for Applet classes in java.desktop Reviewed-by: serb, psadhukhan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
37557
00d5cb18c86b 7076354: JavaSoundAudioClip stop() Method sequencer.addMetaEventListener(this); wrong?
serb
parents: 32865
diff changeset
     2
 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    28
import java.applet.AudioClip;
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    29
import java.io.BufferedInputStream;
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    30
import java.io.ByteArrayOutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    34
import javax.sound.midi.InvalidMidiDataException;
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    35
import javax.sound.midi.MetaEventListener;
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    36
import javax.sound.midi.MetaMessage;
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    37
import javax.sound.midi.MidiFileFormat;
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    38
import javax.sound.midi.MidiSystem;
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    39
import javax.sound.midi.MidiUnavailableException;
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    40
import javax.sound.midi.Sequence;
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    41
import javax.sound.midi.Sequencer;
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    42
import javax.sound.sampled.AudioFormat;
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    43
import javax.sound.sampled.AudioInputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.sound.sampled.AudioSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.sound.sampled.Clip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.sound.sampled.DataLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.sound.sampled.LineEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.sound.sampled.LineListener;
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
    49
import javax.sound.sampled.SourceDataLine;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.sound.sampled.UnsupportedAudioFileException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Java Sound audio clip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author Arthur van Hoff, Kara Kytle, Jan Borgersen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
42198
6ff366cc096b 8168316: Suppress deprecation warnings for Applet classes in java.desktop
prr
parents: 40444
diff changeset
    58
@SuppressWarnings("deprecation")
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    59
public final class JavaSoundAudioClip implements AudioClip, MetaEventListener, LineListener {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final boolean DEBUG = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final int BUFFER_SIZE = 16384; // number of bytes written each time to the source data line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private long lastPlayCall = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final int MINIMUM_PLAY_DELAY = 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private byte loadedAudio[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private int loadedAudioByteLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private AudioFormat loadedAudioFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private AutoClosingClip clip = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private boolean clipLooping = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private DataPusher datapusher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private Sequencer sequencer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private Sequence sequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private boolean sequencerloop = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * used for determining how many samples is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * threshhold between playing as a Clip and streaming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * from the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * $$jb: 11.07.99: the engine has a limit of 1M
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * samples to play as a Clip, so compare this number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * with the number of samples in the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    90
    private static final long CLIP_THRESHOLD = 1048576;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    //private final static long CLIP_THRESHOLD = 1;
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    92
    private static final int STREAM_BUFFER_SIZE = 1024;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public JavaSoundAudioClip(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.<init>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        BufferedInputStream bis = new BufferedInputStream(in, STREAM_BUFFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        bis.mark(STREAM_BUFFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        boolean success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            AudioInputStream as = AudioSystem.getAudioInputStream(bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            // load the stream data into memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            success = loadAudioData(as);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                if (loadedAudioByteLength < CLIP_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    success = createClip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                if (!success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    success = createSourceDataLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } catch (UnsupportedAudioFileException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            // not an audio file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                MidiFileFormat mff = MidiSystem.getMidiFileFormat(bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                success = createSequencer(bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            } catch (InvalidMidiDataException e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (!success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw new IOException("Unable to create AudioClip from input stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
   128
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public synchronized void play() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        startImpl(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
   133
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public synchronized void loop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        startImpl(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private synchronized void startImpl(boolean loop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // hack for some applets that call the start method very rapidly...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        long currentTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        long diff = currentTime - lastPlayCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (diff < MINIMUM_PLAY_DELAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (DEBUG || Printer.debug) Printer.debug("JavaSoundAudioClip.startImpl(loop="+loop+"): abort - too rapdly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        lastPlayCall = currentTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (DEBUG || Printer.debug) Printer.debug("JavaSoundAudioClip.startImpl(loop="+loop+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (clip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (!clip.isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.open()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    clip.open(loadedAudioFormat, loadedAudio, 0, loadedAudioByteLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.flush()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    clip.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    if (loop != clipLooping) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        // need to stop in case the looped status changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.stop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        clip.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                clip.setFramePosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                if (loop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.loop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    clip.loop(Clip.LOOP_CONTINUOUSLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.start()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    clip.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                clipLooping = loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                if (DEBUG || Printer.debug)Printer.debug("Clip should be playing/looping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } else if (datapusher != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                datapusher.start(loop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                if (DEBUG || Printer.debug)Printer.debug("Stream should be playing/looping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } else if (sequencer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                sequencerloop = loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                if (sequencer.isRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    sequencer.setMicrosecondPosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if (!sequencer.isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        sequencer.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        sequencer.setSequence(sequence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    } catch (InvalidMidiDataException e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        if (DEBUG || Printer.err)e1.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    } catch (MidiUnavailableException e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        if (DEBUG || Printer.err)e2.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                sequencer.addMetaEventListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    sequencer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    if (DEBUG || Printer.err) e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                if (DEBUG || Printer.debug)Printer.debug("Sequencer should be playing/looping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (DEBUG || Printer.err)e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
   207
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public synchronized void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip->stop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        lastPlayCall = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (clip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.flush()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                clip.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            } catch (Exception e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (Printer.err) e1.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.stop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                clip.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            } catch (Exception e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                if (Printer.err) e2.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (DEBUG || Printer.debug)Printer.debug("Clip should be stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } else if (datapusher != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            datapusher.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (DEBUG || Printer.debug)Printer.debug("Stream should be stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        } else if (sequencer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                sequencerloop = false;
37557
00d5cb18c86b 7076354: JavaSoundAudioClip stop() Method sequencer.addMetaEventListener(this); wrong?
serb
parents: 32865
diff changeset
   235
                sequencer.removeMetaEventListener(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                sequencer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            } catch (Exception e3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                if (Printer.err) e3.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                sequencer.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            } catch (Exception e4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                if (Printer.err) e4.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (DEBUG || Printer.debug)Printer.debug("Sequencer should be stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    // Event handlers (for debugging)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
   251
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public synchronized void update(LineEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (DEBUG || Printer.debug) Printer.debug("line event received: "+event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    // handle MIDI track end meta events for looping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
   258
    @Override
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
   259
    public synchronized void meta(MetaMessage message) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if( message.getType() == 47 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (sequencerloop){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                //notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                sequencer.setMicrosecondPosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                loop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
   274
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return getClass().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 37557
diff changeset
   279
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (clip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip.finalize: clip.close()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            clip.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        //$$fb 2001-09-26: may improve situation related to bug #4302884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (datapusher != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            datapusher.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (sequencer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            sequencer.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    // FILE LOADING METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private boolean loadAudioData(AudioInputStream as)  throws IOException, UnsupportedAudioFileException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip->openAsClip()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // first possibly convert this stream to PCM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        as = Toolkit.getPCMConvertedAudioInputStream(as);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (as == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        loadedAudioFormat = as.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        long frameLen = as.getFrameLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        int frameSize = loadedAudioFormat.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        long byteLen = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (frameLen != AudioSystem.NOT_SPECIFIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            && frameLen > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            && frameSize != AudioSystem.NOT_SPECIFIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            && frameSize > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            byteLen = frameLen * frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (byteLen != AudioSystem.NOT_SPECIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            // if the stream length is known, it can be efficiently loaded into memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            readStream(as, byteLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // otherwise we use a ByteArrayOutputStream to load it into memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            readStream(as);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // if everything went fine, we have now the audio data in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // loadedAudio, and the byte length in loadedAudioByteLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private void readStream(AudioInputStream as, long byteLen) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // arrays "only" max. 2GB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        int intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (byteLen > 2147483647) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            intLen = 2147483647;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            intLen = (int) byteLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        loadedAudio = new byte[intLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        loadedAudioByteLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // this loop may throw an IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            int bytesRead = as.read(loadedAudio, loadedAudioByteLength, intLen - loadedAudioByteLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (bytesRead <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                as.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            loadedAudioByteLength += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private void readStream(AudioInputStream as) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        DirectBAOS baos = new DirectBAOS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        byte buffer[] = new byte[16384];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        int totalBytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        // this loop may throw an IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        while( true ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            bytesRead = as.read(buffer, 0, buffer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if (bytesRead <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                as.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            totalBytesRead += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            baos.write(buffer, 0, bytesRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        loadedAudio = baos.getInternalBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        loadedAudioByteLength = totalBytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    // METHODS FOR CREATING THE DEVICE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    private boolean createClip() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createClip()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            DataLine.Info info = new DataLine.Info(Clip.class, loadedAudioFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            if (!(AudioSystem.isLineSupported(info)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                if (DEBUG || Printer.err)Printer.err("Clip not supported: "+loadedAudioFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                // fail silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            Object line = AudioSystem.getLine(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (!(line instanceof AutoClosingClip)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                if (DEBUG || Printer.err)Printer.err("Clip is not auto closing!"+clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                // fail -> will try with SourceDataLine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            clip = (AutoClosingClip) line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            clip.setAutoClosing(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if (DEBUG || Printer.debug) clip.addLineListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            if (DEBUG || Printer.err)e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            // fail silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (clip==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            // fail silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (DEBUG || Printer.debug)Printer.debug("Loaded clip.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private boolean createSourceDataLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (!(AudioSystem.isLineSupported(info)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                // fail silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if (DEBUG || Printer.err)e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            // fail silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (datapusher==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            // fail silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    private boolean createSequencer(BufferedInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSequencer()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        // get the sequencer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            sequencer = MidiSystem.getSequencer( );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        } catch(MidiUnavailableException me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (DEBUG || Printer.err)me.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (sequencer==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            sequence = MidiSystem.getSequence(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            if (sequence == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if (DEBUG || Printer.err)e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (DEBUG || Printer.debug)Printer.debug("Created Sequencer.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * private inner class representing a ByteArrayOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * which allows retrieval of the internal array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    private static class DirectBAOS extends ByteArrayOutputStream {
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   471
        DirectBAOS() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        public byte[] getInternalBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    } // class DirectBAOS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
}