1 import java.awt.image.SampleModel;
\r
3 import FileOutputStream;
\r
6 * 11/19/04 1.0 moved to LGPL.
\r
7 * 29/01/00 Initial version. mdm@techie.com
\r
8 *-----------------------------------------------------------------------
\r
9 * This program is free software; you can redistribute it and/or modify
\r
10 * it under the terms of the GNU Library General Public License as published
\r
11 * by the Free Software Foundation; either version 2 of the License, or
\r
12 * (at your option) any later version.
\r
14 * This program is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
17 * GNU Library General Public License for more details.
\r
19 * You should have received a copy of the GNU Library General Public
\r
20 * License along with this program; if not, write to the Free Software
\r
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
\r
22 *----------------------------------------------------------------------
\r
26 * The <code>Player</code> class implements a simple player for playback of an
\r
27 * MPEG audio stream.
\r
29 * @author Mat McGowan
\r
33 // REVIEW: the audio device should not be opened until the
\r
34 // first MPEG audio frame has been decoded.
\r
35 @LATTICE("B<DE,DE<ST,DE<HE,HE<ST,ST<FR")
\r
36 public class Player {
\r
38 * The current frame number.
\r
41 private int frame = 0;
\r
44 * The MPEG audio bitstream.
\r
46 // private Bitstream bitstream;
\r
49 * The MPEG audio decoder.
\r
51 /* final */@LOC("DE")
\r
52 private Decoder decoder;
\r
55 * The AudioDevice the audio samples are written to.
\r
57 // private AudioDevice audio;
\r
60 * Has the player been closed?
\r
63 private boolean closed = false;
\r
66 * Has the player played back all frames from the stream?
\r
69 private boolean complete = false;
\r
72 private int lastPosition = 0;
\r
75 private long sampleNumber;
\r
79 * Creates a new <code>Player</code> instance.
\r
81 public Player() throws JavaLayerException {
\r
85 public Player(AudioDevice device) throws JavaLayerException {
\r
86 // bitstream = new Bitstream(stream);
\r
87 decoder = new Decoder();
\r
89 // if (device!=null)
\r
95 // FactoryRegistry r = FactoryRegistry.systemRegistry();
\r
96 // audio = r.createAudioDevice();
\r
99 device.open(decoder);
\r
102 public void play() throws JavaLayerException {
\r
103 play(Integer.MAX_VALUE);
\r
107 * Plays a number of MPEG audio frames.
\r
110 * The number of frames to play.
\r
111 * @return true if the last frame was played, or false if there are more
\r
114 @LATTICE("OUT<THIS,THIS<IN,IN*,THISLOC=THIS")
\r
116 public boolean play(@LOC("IN") int frames) throws JavaLayerException {
\r
117 @LOC("OUT") boolean ret = true;
\r
119 // initialization before ssjava loop
\r
120 @LOC("THIS,Player.FR") boolean init = true;
\r
121 @LOC("THIS,Player.ST") Header h = BitstreamWrapper.readFrame();
\r
125 System.out.println( "Gobble sentinel: +++" );
\r
127 @LOC("IN") int count = 0;
\r
128 SSJAVA: while (count++ < 2147483646) {
\r
132 ret = decodeFrame(init, h);
\r
136 h = BitstreamWrapper.readFrame();
\r
140 * if (!ret) { // last frame, ensure all data flushed to the audio device.
\r
141 * AudioDevice out = audio; if (out!=null) { out.flush(); synchronized
\r
142 * (this) { complete = (!closed); close(); } } }
\r
148 * Cloases this player. Any audio currently playing is stopped immediately.
\r
151 public synchronized void close() {
\r
153 * AudioDevice out = audio; if (out!=null) { closed = true; audio = null; //
\r
154 * this may fail, so ensure object state is set up before // calling this
\r
155 * method. out.close(); lastPosition = out.getPosition(); try {
\r
156 * bitstream.close(); } catch (BitstreamException ex) { } }
\r
161 * Returns the completed status of this player.
\r
163 * @return true if all available MPEG audio frames have been decoded, or false
\r
166 public synchronized boolean isComplete() {
\r
171 * Retrieves the position in milliseconds of the current audio sample being
\r
172 * played. This method delegates to the <code>
\r
173 * AudioDevice</code> that is used by this player to sound the decoded audio
\r
176 public int getPosition() {
\r
177 // int position = lastPosition;
\r
179 // AudioDevice out = audio;
\r
182 // position = out.getPosition();
\r
184 // return position;
\r
190 * Decodes a single frame.
\r
192 * @return true if there are no more frames to decode, false otherwise.
\r
194 @LATTICE("C<THIS,O<THIS,THIS<IN,C*,THISLOC=THIS,RETURNLOC=O,GLOBALLOC=THIS")
\r
195 protected boolean decodeFrame(@LOC("THIS,Player.FR") boolean init, @LOC("THIS,Player.ST") Header h)
\r
196 throws JavaLayerException {
\r
198 // AudioDevice out = audio;
\r
202 // Header h = bitstream.readFrame();
\r
208 // @LOC("O") SampleBuffer output = (SampleBuffer) decoder.decodeFrame(h);
\r
209 decoder.decodeFrame(h);
\r
211 // it looks like there is left and right channel interleaved into the
\r
212 // output buffer, so only sample one channel (stride=2)
\r
213 short[] outbuf = SampleBufferWrapper.getBuffer();
\r
214 TERMINATE: for (@LOC("C") int i = 0; i < SampleBufferWrapper.getBufferLength(); i = i + 2) {
\r
215 System.out.println( sampleNumber+" "+outbuf[i] );
\r
221 // synchronized (this)
\r
226 // out.write(output.getBuffer(), 0, output.getBufferLength());
\r
230 // bitstream.closeFrame();
\r
231 } catch (RuntimeException ex) {
\r
232 throw new JavaLayerException("Exception decoding audio frame", ex);
\r
235 * catch (IOException ex) {
\r
236 * System.out.println("exception decoding audio frame: "+ex); return false;
\r
237 * } catch (BitstreamException bitex) {
\r
238 * System.out.println("exception decoding audio frame: "+bitex); return
\r
239 * false; } catch (DecoderException decex) {
\r
240 * System.out.println("exception decoding audio frame: "+decex); return
\r