-import java.awt.image.SampleModel;\r
-\r
-import FileOutputStream;\r
-\r
-/*\r
- * 11/19/04 1.0 moved to LGPL.\r
- * 29/01/00 Initial version. mdm@techie.com\r
- *-----------------------------------------------------------------------\r
- * This program is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU Library General Public License as published\r
- * by the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU Library General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU Library General Public\r
- * License along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
- *----------------------------------------------------------------------\r
- */\r
-\r
-/**\r
- * The <code>Player</code> class implements a simple player for playback of an\r
- * MPEG audio stream.\r
- * \r
- * @author Mat McGowan\r
- * @since 0.0.8\r
- */\r
-\r
-// REVIEW: the audio device should not be opened until the\r
-// first MPEG audio frame has been decoded.\r
-@LATTICE("B<DE,DE<ST,DE<HE,HE<ST,ST<FR")\r
-public class Player {\r
- /**\r
- * The current frame number.\r
- */\r
- @LOC("FR")\r
- private int frame = 0;\r
-\r
- /**\r
- * The MPEG audio bitstream.\r
- */\r
- // private Bitstream bitstream;\r
-\r
- /**\r
- * The MPEG audio decoder.\r
- */\r
- /* final */@LOC("DE")\r
- private Decoder decoder;\r
-\r
- /**\r
- * The AudioDevice the audio samples are written to.\r
- */\r
- // private AudioDevice audio;\r
-\r
- /**\r
- * Has the player been closed?\r
- */\r
- @LOC("B")\r
- private boolean closed = false;\r
-\r
- /**\r
- * Has the player played back all frames from the stream?\r
- */\r
- @LOC("B")\r
- private boolean complete = false;\r
-\r
- @LOC("B")\r
- private int lastPosition = 0;\r
-\r
- @LOC("B")\r
- private long sampleNumber;\r
-\r
- /**\r
- * Creates a new <code>Player</code> instance.\r
- */\r
- public Player() throws JavaLayerException {\r
- this(null);\r
- }\r
-\r
- public Player(AudioDevice device) throws JavaLayerException {\r
- // bitstream = new Bitstream(stream);\r
- decoder = new Decoder();\r
-\r
- // if (device!=null)\r
- // {\r
- // audio = device;\r
- // }\r
- // else\r
- // {\r
- // FactoryRegistry r = FactoryRegistry.systemRegistry();\r
- // audio = r.createAudioDevice();\r
- // }\r
-\r
- device.open(decoder);\r
- }\r
-\r
- public void play() throws JavaLayerException {\r
- play(Integer.MAX_VALUE);\r
- }\r
-\r
- /**\r
- * Plays a number of MPEG audio frames.\r
- * \r
- * @param frames\r
- * The number of frames to play.\r
- * @return true if the last frame was played, or false if there are more\r
- * frames.\r
- */\r
- @LATTICE("OUT<THIS,THIS<IN,IN*,THISLOC=THIS")\r
- @RETURNLOC("OUT")\r
- public boolean play(@LOC("IN") int frames) throws JavaLayerException {\r
- @LOC("OUT") boolean ret = true;\r
-\r
- // initialization before ssjava loop\r
- @LOC("THIS,Player.FR") boolean init = true;\r
- @LOC("THIS,Player.ST") Header h = BitstreamWrapper.readFrame();\r
- decoder.init(h);\r
-\r
- sampleNumber = 1;\r
- System.out.println("Gobble sentinel: +++");\r
-\r
- // @LOC("IN") int count = 0;\r
- SSJAVA: while (true) {\r
- if (h == null) {\r
- break;\r
- }\r
- ret = decodeFrame(init, h);\r
- if (!ret) {\r
- break;\r
- }\r
- h = BitstreamWrapper.readFrame();\r
- }\r
-\r
- /*\r
- * if (!ret) { // last frame, ensure all data flushed to the audio device.\r
- * AudioDevice out = audio; if (out!=null) { out.flush(); synchronized\r
- * (this) { complete = (!closed); close(); } } }\r
- */\r
- return ret;\r
- }\r
-\r
- /**\r
- * Cloases this player. Any audio currently playing is stopped immediately.\r
- */\r
-\r
- public synchronized void close() {\r
- /*\r
- * AudioDevice out = audio; if (out!=null) { closed = true; audio = null; //\r
- * this may fail, so ensure object state is set up before // calling this\r
- * method. out.close(); lastPosition = out.getPosition(); try {\r
- * bitstream.close(); } catch (BitstreamException ex) { } }\r
- */\r
- }\r
-\r
- /**\r
- * Returns the completed status of this player.\r
- * \r
- * @return true if all available MPEG audio frames have been decoded, or false\r
- * otherwise.\r
- */\r
- public synchronized boolean isComplete() {\r
- return complete;\r
- }\r
-\r
- /**\r
- * Retrieves the position in milliseconds of the current audio sample being\r
- * played. This method delegates to the <code>\r
- * AudioDevice</code> that is used by this player to sound the decoded audio\r
- * samples.\r
- */\r
- public int getPosition() {\r
- // int position = lastPosition;\r
-\r
- // AudioDevice out = audio;\r
- // if (out!=null)\r
- // {\r
- // position = out.getPosition();\r
- // }\r
- // return position;\r
- return 0;\r
- }\r
-\r
- /**\r
- * Decodes a single frame.\r
- * \r
- * @return true if there are no more frames to decode, false otherwise.\r
- */\r
- @LATTICE("C<THIS,O<THIS,THIS<IN,C*,THISLOC=THIS,RETURNLOC=O,GLOBALLOC=THIS")\r
- protected boolean decodeFrame(@LOC("THIS,Player.FR") boolean init, @LOC("THIS,Player.ST") Header h)\r
- throws JavaLayerException {\r
- try {\r
- // AudioDevice out = audio;\r
- // if (out==null)\r
- // return false;\r
-\r
- // Header h = bitstream.readFrame();\r
-\r
- // if (h == null){\r
- // return false;\r
- // }\r
- \r
- // @LOC("O") SampleBuffer output = (SampleBuffer) decoder.decodeFrame(h);\r
- decoder.decodeFrame(h);\r
-\r
-// DEBUG_OUTPUT_CHECKSUM();\r
-// DEBUG_OUTPUT();\r
- // synchronized (this)\r
- // {\r
- // out = audio;\r
- // if (out!=null)\r
- // {\r
- // out.write(output.getBuffer(), 0, output.getBufferLength());\r
- // }\r
- // }\r
-\r
- // bitstream.closeFrame();\r
- } catch (RuntimeException ex) {\r
- throw new JavaLayerException("Exception decoding audio frame", ex);\r
- }\r
- /*\r
- * catch (IOException ex) {\r
- * System.out.println("exception decoding audio frame: "+ex); return false;\r
- * } catch (BitstreamException bitex) {\r
- * System.out.println("exception decoding audio frame: "+bitex); return\r
- * false; } catch (DecoderException decex) {\r
- * System.out.println("exception decoding audio frame: "+decex); return\r
- * false; }\r
- */\r
- return true;\r
- }\r
-\r
- @TRUST\r
- public void DEBUG_OUTPUT() {\r
- // it looks like there is left and right channel interleaved into the\r
- // output buffer, so only sample one channel (stride=2)\r
- short[] outbuf = SampleBufferWrapper.getBuffer();\r
- for (int i = 0; i < SampleBufferWrapper.getBufferLength(); i = i + 2) {\r
- System.out.println(sampleNumber + " " + outbuf[i]);\r
- sampleNumber++;\r
- }\r
- }\r
-\r
- @TRUST\r
- public void DEBUG_OUTPUT_CHECKSUM() {\r
- // eom debug\r
- @LOC("C") int sum = 0;\r
- @LOC("C") short[] outbuf = SampleBufferWrapper.getBuffer();\r
- // short[] outbuf = output.getBuffer();\r
- TERMINATE: for (@LOC("C") int i = 0; i < SampleBufferWrapper.getBufferLength(); i++) {\r
- // System.out.println(outbuf[i]);\r
- sum += outbuf[i];\r
- }\r
- System.out.println(sum);\r
- //\r
- }\r
-\r
-}\r
+import java.awt.image.SampleModel;
+
+import FileOutputStream;
+
+/*
+ * 11/19/04 1.0 moved to LGPL.
+ * 29/01/00 Initial version. mdm@techie.com
+ *-----------------------------------------------------------------------
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *----------------------------------------------------------------------
+ */
+
+/**
+ * The <code>Player</code> class implements a simple player for playback of an
+ * MPEG audio stream.
+ *
+ * @author Mat McGowan
+ * @since 0.0.8
+ */
+
+// REVIEW: the audio device should not be opened until the
+// first MPEG audio frame has been decoded.
+@LATTICE("B<DE,DE<ST,DE<HE,HE<ST,ST<FR")
+public class Player {
+ /**
+ * The current frame number.
+ */
+ @LOC("FR")
+ private int frame = 0;
+
+ /**
+ * The MPEG audio bitstream.
+ */
+ // private Bitstream bitstream;
+
+ /**
+ * The MPEG audio decoder.
+ */
+ /* final */@LOC("DE")
+ private Decoder decoder;
+
+ /**
+ * The AudioDevice the audio samples are written to.
+ */
+ // private AudioDevice audio;
+
+ /**
+ * Has the player been closed?
+ */
+ @LOC("B")
+ private boolean closed = false;
+
+ /**
+ * Has the player played back all frames from the stream?
+ */
+ @LOC("B")
+ private boolean complete = false;
+
+ @LOC("B")
+ private int lastPosition = 0;
+
+ @LOC("B")
+ private long sampleNumber;
+
+ /**
+ * Creates a new <code>Player</code> instance.
+ */
+ public Player() throws JavaLayerException {
+ this(null);
+ }
+
+ public Player(AudioDevice device) throws JavaLayerException {
+ // bitstream = new Bitstream(stream);
+ decoder = new Decoder();
+
+ // if (device!=null)
+ // {
+ // audio = device;
+ // }
+ // else
+ // {
+ // FactoryRegistry r = FactoryRegistry.systemRegistry();
+ // audio = r.createAudioDevice();
+ // }
+
+ device.open(decoder);
+ }
+
+ public void play() throws JavaLayerException {
+ play(Integer.MAX_VALUE);
+ }
+
+ /**
+ * Plays a number of MPEG audio frames.
+ *
+ * @param frames
+ * The number of frames to play.
+ * @return true if the last frame was played, or false if there are more
+ * frames.
+ */
+ @LATTICE("OUT<THIS,THIS<IN,IN*,THISLOC=THIS")
+ @RETURNLOC("OUT")
+ public boolean play(@LOC("IN") int frames) throws JavaLayerException {
+ @LOC("OUT") boolean ret = true;
+
+ // initialization before ssjava loop
+ @LOC("THIS,Player.FR") boolean init = true;
+ @LOC("THIS,Player.ST") Header h = BitstreamWrapper.readFrame();
+ decoder.init(h);
+
+ sampleNumber = 1;
+ System.out.println("Gobble sentinel: +++");
+
+ // @LOC("IN") int count = 0;
+ SSJAVA: while (true) {
+ if (h == null) {
+ break;
+ }
+ ret = decodeFrame(init, h);
+ if (!ret) {
+ break;
+ }
+ h = BitstreamWrapper.readFrame();
+ }
+
+ /*
+ * if (!ret) { // last frame, ensure all data flushed to the audio device.
+ * AudioDevice out = audio; if (out!=null) { out.flush(); synchronized
+ * (this) { complete = (!closed); close(); } } }
+ */
+ return ret;
+ }
+
+ /**
+ * Cloases this player. Any audio currently playing is stopped immediately.
+ */
+
+ public synchronized void close() {
+ /*
+ * AudioDevice out = audio; if (out!=null) { closed = true; audio = null; //
+ * this may fail, so ensure object state is set up before // calling this
+ * method. out.close(); lastPosition = out.getPosition(); try {
+ * bitstream.close(); } catch (BitstreamException ex) { } }
+ */
+ }
+
+ /**
+ * Returns the completed status of this player.
+ *
+ * @return true if all available MPEG audio frames have been decoded, or false
+ * otherwise.
+ */
+ public synchronized boolean isComplete() {
+ return complete;
+ }
+
+ /**
+ * Retrieves the position in milliseconds of the current audio sample being
+ * played. This method delegates to the <code>
+ * AudioDevice</code> that is used by this player to sound the decoded audio
+ * samples.
+ */
+ public int getPosition() {
+ // int position = lastPosition;
+
+ // AudioDevice out = audio;
+ // if (out!=null)
+ // {
+ // position = out.getPosition();
+ // }
+ // return position;
+ return 0;
+ }
+
+ /**
+ * Decodes a single frame.
+ *
+ * @return true if there are no more frames to decode, false otherwise.
+ */
+ @LATTICE("C<THIS,O<THIS,THIS<IN,C*,THISLOC=THIS,RETURNLOC=O,GLOBALLOC=THIS")
+ protected boolean decodeFrame(@LOC("THIS,Player.FR") boolean init, @LOC("THIS,Player.ST") Header h)
+ throws JavaLayerException {
+ try {
+ // AudioDevice out = audio;
+ // if (out==null)
+ // return false;
+
+ // Header h = bitstream.readFrame();
+
+ // if (h == null){
+ // return false;
+ // }
+
+ // @LOC("O") SampleBuffer output = (SampleBuffer) decoder.decodeFrame(h);
+ decoder.decodeFrame(h);
+
+// DEBUG_OUTPUT_CHECKSUM();
+// DEBUG_OUTPUT();
+ // synchronized (this)
+ // {
+ // out = audio;
+ // if (out!=null)
+ // {
+ // out.write(output.getBuffer(), 0, output.getBufferLength());
+ // }
+ // }
+
+ // bitstream.closeFrame();
+ } catch (RuntimeException ex) {
+ throw new JavaLayerException("Exception decoding audio frame", ex);
+ }
+ /*
+ * catch (IOException ex) {
+ * System.out.println("exception decoding audio frame: "+ex); return false;
+ * } catch (BitstreamException bitex) {
+ * System.out.println("exception decoding audio frame: "+bitex); return
+ * false; } catch (DecoderException decex) {
+ * System.out.println("exception decoding audio frame: "+decex); return
+ * false; }
+ */
+ return true;
+ }
+
+ @TRUST
+ public void DEBUG_OUTPUT() {
+ // it looks like there is left and right channel interleaved into the
+ // output buffer, so only sample one channel (stride=2)
+ short[] outbuf = SampleBufferWrapper.getBuffer();
+ for (int i = 0; i < SampleBufferWrapper.getBufferLength(); i = i + 2) {
+ System.out.println(sampleNumber + " " + outbuf[i]);
+ sampleNumber++;
+ }
+ }
+
+ @TRUST
+ public void DEBUG_OUTPUT_CHECKSUM() {
+ // eom debug
+ @LOC("C") int sum = 0;
+ @LOC("C") short[] outbuf = SampleBufferWrapper.getBuffer();
+ // short[] outbuf = output.getBuffer();
+ TERMINATE: for (@LOC("C") int i = 0; i < SampleBufferWrapper.getBufferLength(); i++) {
+ // System.out.println(outbuf[i]);
+ sum += outbuf[i];
+ }
+ System.out.println(sum);
+ //
+ }
+
+}