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