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