+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
*/\r
\r
-package javazoom.jl.player;\r
-\r
-import java.io.InputStream;\r
-\r
-import javazoom.jl.decoder.Bitstream;\r
-import javazoom.jl.decoder.BitstreamException;\r
-import javazoom.jl.decoder.Decoder;\r
-import javazoom.jl.decoder.Header;\r
-import javazoom.jl.decoder.JavaLayerException;\r
-import javazoom.jl.decoder.SampleBuffer;\r
- \r
/**\r
- * The <code>Player</code> class implements a simple player for playback\r
- * of an MPEG audio stream. \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
+ * @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
-public class Player\r
-{ \r
- /**\r
- * The current frame number. \r
- */\r
- private int frame = 0;\r
- \r
- /**\r
- * The MPEG audio bitstream. \r
- */\r
- // javac blank final bug. \r
- /*final*/ private Bitstream bitstream;\r
- \r
- /**\r
- * The MPEG audio decoder. \r
- */\r
- /*final*/ 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
- private boolean closed = false;\r
- \r
- /**\r
- * Has the player played back all frames from the stream?\r
- */\r
- private boolean complete = false;\r
-\r
- private int lastPosition = 0;\r
- \r
- /**\r
- * Creates a new <code>Player</code> instance. \r
- */\r
- public Player(InputStream stream) throws JavaLayerException\r
- {\r
- this(stream, null); \r
- }\r
- \r
- public Player(InputStream stream, AudioDevice device) throws JavaLayerException\r
- {\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
- audio.open(decoder);\r
- }\r
- \r
- public void play() throws JavaLayerException\r
- {\r
- play(Integer.MAX_VALUE);\r
- }\r
- \r
- /**\r
- * Plays a number of MPEG audio frames. \r
- * \r
- * @param frames The number of frames to play. \r
- * @return true if the last frame was played, or false if there are\r
- * more frames. \r
- */\r
- public boolean play(int frames) throws JavaLayerException\r
- {\r
- boolean ret = true;\r
- \r
- while (frames-- > 0 && ret)\r
- {\r
- ret = decodeFrame(); \r
- }\r
- \r
- if (!ret)\r
- {\r
- // last frame, ensure all data flushed to the audio device. \r
- AudioDevice out = audio;\r
- if (out!=null)\r
- { \r
- out.flush();\r
- synchronized (this)\r
- {\r
- complete = (!closed);\r
- close();\r
- } \r
- }\r
- }\r
- return ret;\r
- }\r
- \r
- /**\r
- * Cloases this player. Any audio currently playing is stopped\r
- * immediately. \r
- */\r
- public synchronized void close()\r
- { \r
- AudioDevice out = audio;\r
- if (out!=null)\r
- { \r
- closed = true;\r
- audio = null; \r
- // this may fail, so ensure object state is set up before\r
- // calling this method. \r
- out.close();\r
- lastPosition = out.getPosition();\r
- try\r
- {\r
- bitstream.close();\r
- }\r
- catch (BitstreamException ex)\r
- {\r
- }\r
- }\r
- }\r
- \r
- /**\r
- * Returns the completed status of this player.\r
- * \r
- * @return true if all available MPEG audio frames have been\r
- * decoded, or false otherwise. \r
- */\r
- public synchronized boolean isComplete()\r
- {\r
- return complete; \r
- }\r
- \r
- /**\r
- * Retrieves the position in milliseconds of the current audio\r
- * sample being played. This method delegates to the <code>\r
- * AudioDevice</code> that is used by this player to sound\r
- * the decoded audio samples. \r
- */\r
- public int getPosition()\r
- {\r
- int position = lastPosition;\r
- \r
- AudioDevice out = audio; \r
- if (out!=null)\r
- {\r
- position = out.getPosition(); \r
- }\r
- return position;\r
- } \r
- \r
- /**\r
- * Decodes a single frame.\r
- * \r
- * @return true if there are no more frames to decode, false otherwise.\r
- */\r
- protected boolean decodeFrame() throws JavaLayerException\r
- { \r
- try\r
- {\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
- // sample buffer set when decoder constructed\r
- SampleBuffer output = (SampleBuffer)decoder.decodeFrame(h, bitstream);\r
- \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
- } \r
- catch (RuntimeException ex)\r
- {\r
- throw new JavaLayerException("Exception decoding audio frame", ex);\r
- }\r
-/*\r
- catch (IOException ex)\r
- {\r
- System.out.println("exception decoding audio frame: "+ex);\r
- return false; \r
- }\r
- catch (BitstreamException bitex)\r
- {\r
- System.out.println("exception decoding audio frame: "+bitex);\r
- return false; \r
- }\r
- catch (DecoderException decex)\r
- {\r
- System.out.println("exception decoding audio frame: "+decex);\r
- return false; \r
- }\r
-*/ \r
- return true;\r
- }\r
-\r
- \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
+ /**\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("T<IN,T*,IN*,THISLOC=T")\r
+ @RETURNLOC("T")\r
+ public boolean play(@LOC("IN") int frames) throws JavaLayerException {\r
+ @LOC("T") boolean ret = true;\r
+ \r
+ @LOC("T") int count=0;\r
+ SSJAVA: while (count++ < 2147483646) {\r
+ ret = decodeFrame();\r
+ if(!ret){\r
+ break;\r
+ }\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() throws JavaLayerException {\r
+ try {\r
+ // AudioDevice out = audio;\r
+ // if (out==null)\r
+ // return false;\r
+\r
+ // Header h = bitstream.readFrame();\r
+ @LOC("THIS,Player.ST") Header h = BitstreamWrapper.readFrame();\r
+\r
+ if (h == null)\r
+ return false;\r
+\r
+ // @LOC("O") SampleBuffer output = (SampleBuffer) decoder.decodeFrame(h);\r
+ decoder.decodeFrame(h);\r
+\r
+ // eom debug\r
+ @LOC("C") int sum = 0;\r
+ @LOC("C") short[] outbuf = SampleBufferWrapper.getBuffer();\r
+ // short[] outbuf = output.getBuffer();\r
+ TERMINATE:\r
+ 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
+ // 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
}\r