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