965c70362a71059f6e2943beb2348585937e6f2b
[IRC.git] / Robust / src / Benchmarks / SSJava / MP3Decoder / Player.java
1 import java.awt.image.SampleModel;\r
2 \r
3 import FileOutputStream;\r
4 \r
5 /*\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
13  *\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
18  *\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
23  */\r
24 \r
25 /**\r
26  * The <code>Player</code> class implements a simple player for playback of an\r
27  * MPEG audio stream.\r
28  * \r
29  * @author Mat McGowan\r
30  * @since 0.0.8\r
31  */\r
32 \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
37   /**\r
38    * The current frame number.\r
39    */\r
40   @LOC("FR")\r
41   private int frame = 0;\r
42 \r
43   /**\r
44    * The MPEG audio bitstream.\r
45    */\r
46   // private Bitstream bitstream;\r
47 \r
48   /**\r
49    * The MPEG audio decoder.\r
50    */\r
51   /* final */@LOC("DE")\r
52   private Decoder decoder;\r
53 \r
54   /**\r
55    * The AudioDevice the audio samples are written to.\r
56    */\r
57   // private AudioDevice audio;\r
58 \r
59   /**\r
60    * Has the player been closed?\r
61    */\r
62   @LOC("B")\r
63   private boolean closed = false;\r
64 \r
65   /**\r
66    * Has the player played back all frames from the stream?\r
67    */\r
68   @LOC("B")\r
69   private boolean complete = false;\r
70 \r
71   @LOC("B")\r
72   private int lastPosition = 0;\r
73 \r
74   @LOC("B")\r
75   private long sampleNumber;\r
76 \r
77   /**\r
78    * Creates a new <code>Player</code> instance.\r
79    */\r
80   public Player() throws JavaLayerException {\r
81     this(null);\r
82   }\r
83 \r
84   public Player(AudioDevice device) throws JavaLayerException {\r
85     // bitstream = new Bitstream(stream);\r
86     decoder = new Decoder();\r
87 \r
88     // if (device!=null)\r
89     // {\r
90     // audio = device;\r
91     // }\r
92     // else\r
93     // {\r
94     // FactoryRegistry r = FactoryRegistry.systemRegistry();\r
95     // audio = r.createAudioDevice();\r
96     // }\r
97 \r
98     device.open(decoder);\r
99   }\r
100 \r
101   public void play() throws JavaLayerException {\r
102     play(Integer.MAX_VALUE);\r
103   }\r
104 \r
105   /**\r
106    * Plays a number of MPEG audio frames.\r
107    * \r
108    * @param frames\r
109    *          The number of frames to play.\r
110    * @return true if the last frame was played, or false if there are more\r
111    *         frames.\r
112    */\r
113   @LATTICE("OUT<THIS,THIS<IN,IN*,THISLOC=THIS")\r
114   @RETURNLOC("OUT")\r
115   public boolean play(@LOC("IN") int frames) throws JavaLayerException {\r
116     @LOC("OUT") boolean ret = true;\r
117 \r
118     // initialization before ssjava loop\r
119     @LOC("THIS,Player.FR") boolean init = true;\r
120     @LOC("THIS,Player.ST") Header h = BitstreamWrapper.readFrame();\r
121     decoder.init(h);\r
122 \r
123     sampleNumber = 1;\r
124     System.out.println("Gobble sentinel: +++");\r
125 \r
126     // @LOC("IN") int count = 0;\r
127     SSJAVA: while (true) {\r
128       if (h == null) {\r
129         break;\r
130       }\r
131       ret = decodeFrame(init, h);\r
132       if (!ret) {\r
133         break;\r
134       }\r
135       h = BitstreamWrapper.readFrame();\r
136     }\r
137 \r
138     /*\r
139      * if (!ret) { // last frame, ensure all data flushed to the audio device.\r
140      * AudioDevice out = audio; if (out!=null) { out.flush(); synchronized\r
141      * (this) { complete = (!closed); close(); } } }\r
142      */\r
143     return ret;\r
144   }\r
145 \r
146   /**\r
147    * Cloases this player. Any audio currently playing is stopped immediately.\r
148    */\r
149 \r
150   public synchronized void close() {\r
151     /*\r
152      * AudioDevice out = audio; if (out!=null) { closed = true; audio = null; //\r
153      * this may fail, so ensure object state is set up before // calling this\r
154      * method. out.close(); lastPosition = out.getPosition(); try {\r
155      * bitstream.close(); } catch (BitstreamException ex) { } }\r
156      */\r
157   }\r
158 \r
159   /**\r
160    * Returns the completed status of this player.\r
161    * \r
162    * @return true if all available MPEG audio frames have been decoded, or false\r
163    *         otherwise.\r
164    */\r
165   public synchronized boolean isComplete() {\r
166     return complete;\r
167   }\r
168 \r
169   /**\r
170    * Retrieves the position in milliseconds of the current audio sample being\r
171    * played. This method delegates to the <code>\r
172    * AudioDevice</code> that is used by this player to sound the decoded audio\r
173    * samples.\r
174    */\r
175   public int getPosition() {\r
176     // int position = lastPosition;\r
177 \r
178     // AudioDevice out = audio;\r
179     // if (out!=null)\r
180     // {\r
181     // position = out.getPosition();\r
182     // }\r
183     // return position;\r
184     return 0;\r
185   }\r
186 \r
187   /**\r
188    * Decodes a single frame.\r
189    * \r
190    * @return true if there are no more frames to decode, false otherwise.\r
191    */\r
192   @LATTICE("C<THIS,O<THIS,THIS<IN,C*,THISLOC=THIS,RETURNLOC=O,GLOBALLOC=THIS")\r
193   protected boolean decodeFrame(@LOC("THIS,Player.FR") boolean init, @LOC("THIS,Player.ST") Header h)\r
194       throws JavaLayerException {\r
195     try {\r
196       // AudioDevice out = audio;\r
197       // if (out==null)\r
198       // return false;\r
199 \r
200       // Header h = bitstream.readFrame();\r
201 \r
202       // if (h == null){\r
203       // return false;\r
204       // }\r
205 \r
206       // @LOC("O") SampleBuffer output = (SampleBuffer) decoder.decodeFrame(h);\r
207       decoder.decodeFrame(h);\r
208 \r
209       DEBUG_OUTPUT_CHECKSUM();\r
210       // DEBUG_OUTPUT();\r
211       // synchronized (this)\r
212       // {\r
213       // out = audio;\r
214       // if (out!=null)\r
215       // {\r
216       // out.write(output.getBuffer(), 0, output.getBufferLength());\r
217       // }\r
218       // }\r
219 \r
220       // bitstream.closeFrame();\r
221     } catch (RuntimeException ex) {\r
222       throw new JavaLayerException("Exception decoding audio frame", ex);\r
223     }\r
224     /*\r
225      * catch (IOException ex) {\r
226      * System.out.println("exception decoding audio frame: "+ex); return false;\r
227      * } catch (BitstreamException bitex) {\r
228      * System.out.println("exception decoding audio frame: "+bitex); return\r
229      * false; } catch (DecoderException decex) {\r
230      * System.out.println("exception decoding audio frame: "+decex); return\r
231      * false; }\r
232      */\r
233     return true;\r
234   }\r
235 \r
236   @TRUST\r
237   public void DEBUG_OUTPUT() {\r
238     // it looks like there is left and right channel interleaved into the\r
239     // output buffer, so only sample one channel (stride=2)\r
240     short[] outbuf = SampleBufferWrapper.getBuffer();\r
241     for (int i = 0; i < SampleBufferWrapper.getBufferLength(); i = i + 2) {\r
242       System.out.println(sampleNumber + " " + outbuf[i]);\r
243       sampleNumber++;\r
244     }\r
245   }\r
246 \r
247   @TRUST\r
248   public void DEBUG_OUTPUT_CHECKSUM() {\r
249     // eom debug\r
250     @LOC("C") int sum = 0;\r
251     @LOC("C") short[] outbuf = SampleBufferWrapper.getBuffer();\r
252     // short[] outbuf = output.getBuffer();\r
253     TERMINATE: for (@LOC("C") int i = 0; i < SampleBufferWrapper.getBufferLength(); i++) {\r
254       // System.out.println(outbuf[i]);\r
255       sum += outbuf[i];\r
256     }\r
257     System.out.println(sum);\r
258     //\r
259   }\r
260 \r
261 }\r