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