diff options
Diffstat (limited to 'songdbj/javazoom/jl/player/AudioDeviceBase.java')
-rw-r--r-- | songdbj/javazoom/jl/player/AudioDeviceBase.java | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/songdbj/javazoom/jl/player/AudioDeviceBase.java b/songdbj/javazoom/jl/player/AudioDeviceBase.java new file mode 100644 index 0000000000..d9c84f08e7 --- /dev/null +++ b/songdbj/javazoom/jl/player/AudioDeviceBase.java | |||
@@ -0,0 +1,177 @@ | |||
1 | /* | ||
2 | * 11/19/04 1.0 moved to LGPL. | ||
3 | * 29/01/00 Initial version. mdm@techie.com | ||
4 | *----------------------------------------------------------------------- | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU Library General Public License as published | ||
7 | * by the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Library General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Library General Public | ||
16 | * License along with this program; if not, write to the Free Software | ||
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
18 | *---------------------------------------------------------------------- | ||
19 | */ | ||
20 | |||
21 | package javazoom.jl.player; | ||
22 | |||
23 | import javazoom.jl.decoder.Decoder; | ||
24 | import javazoom.jl.decoder.JavaLayerException; | ||
25 | |||
26 | /** | ||
27 | * The <code>AudioDeviceBase</code> class provides a simple thread-safe | ||
28 | * implementation of the <code>AudioDevice</code> interface. | ||
29 | * Template methods are provided for subclasses to override and | ||
30 | * in doing so provide the implementation for the main operations | ||
31 | * of the <code>AudioDevice</code> interface. | ||
32 | * | ||
33 | * @since 0.0.8 | ||
34 | * @author Mat McGowan | ||
35 | */ | ||
36 | /* | ||
37 | * REVIEW: It is desirable to be able to use the decoder whe | ||
38 | * in the implementation of open(), but the decoder | ||
39 | * has not yet read a frame, and so much of the | ||
40 | * desired information (sample rate, channels etc.) | ||
41 | * are not available. | ||
42 | */ | ||
43 | public abstract class AudioDeviceBase implements AudioDevice | ||
44 | { | ||
45 | private boolean open = false; | ||
46 | |||
47 | private Decoder decoder = null; | ||
48 | |||
49 | /** | ||
50 | * Opens this audio device. | ||
51 | * | ||
52 | * @param decoder The decoder that will provide audio data | ||
53 | * to this audio device. | ||
54 | */ | ||
55 | public synchronized void open(Decoder decoder) throws JavaLayerException | ||
56 | { | ||
57 | if (!isOpen()) | ||
58 | { | ||
59 | this.decoder = decoder; | ||
60 | openImpl(); | ||
61 | setOpen(true); | ||
62 | } | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Template method to provide the | ||
67 | * implementation for the opening of the audio device. | ||
68 | */ | ||
69 | protected void openImpl() throws JavaLayerException | ||
70 | { | ||
71 | } | ||
72 | |||
73 | /** | ||
74 | * Sets the open state for this audio device. | ||
75 | */ | ||
76 | protected void setOpen(boolean open) | ||
77 | { | ||
78 | this.open = open; | ||
79 | } | ||
80 | |||
81 | /** | ||
82 | * Determines if this audio device is open or not. | ||
83 | * | ||
84 | * @return <code>true</code> if the audio device is open, | ||
85 | * <code>false</code> if it is not. | ||
86 | */ | ||
87 | public synchronized boolean isOpen() | ||
88 | { | ||
89 | return open; | ||
90 | } | ||
91 | |||
92 | /** | ||
93 | * Closes this audio device. If the device is currently playing | ||
94 | * audio, playback is stopped immediately without flushing | ||
95 | * any buffered audio data. | ||
96 | */ | ||
97 | public synchronized void close() | ||
98 | { | ||
99 | if (isOpen()) | ||
100 | { | ||
101 | closeImpl(); | ||
102 | setOpen(false); | ||
103 | decoder = null; | ||
104 | } | ||
105 | } | ||
106 | |||
107 | /** | ||
108 | * Template method to provide the implementation for | ||
109 | * closing the audio device. | ||
110 | */ | ||
111 | protected void closeImpl() | ||
112 | { | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Writes audio data to this audio device. Audio data is | ||
117 | * assumed to be in the output format of the decoder. This | ||
118 | * method may return before the data has actually been sounded | ||
119 | * by the device if the device buffers audio samples. | ||
120 | * | ||
121 | * @param samples The samples to write to the audio device. | ||
122 | * @param offs The offset into the array of the first sample to write. | ||
123 | * @param len The number of samples from the array to write. | ||
124 | * @throws JavaLayerException if the audio data could not be | ||
125 | * written to the audio device. | ||
126 | * If the audio device is not open, this method does nthing. | ||
127 | */ | ||
128 | public void write(short[] samples, int offs, int len) | ||
129 | throws JavaLayerException | ||
130 | { | ||
131 | if (isOpen()) | ||
132 | { | ||
133 | writeImpl(samples, offs, len); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | /** | ||
138 | * Template method to provide the implementation for | ||
139 | * writing audio samples to the audio device. | ||
140 | */ | ||
141 | protected void writeImpl(short[] samples, int offs, int len) | ||
142 | throws JavaLayerException | ||
143 | { | ||
144 | } | ||
145 | |||
146 | /** | ||
147 | * Waits for any buffered audio samples to be played by the | ||
148 | * audio device. This method should only be called prior | ||
149 | * to closing the device. | ||
150 | */ | ||
151 | public void flush() | ||
152 | { | ||
153 | if (isOpen()) | ||
154 | { | ||
155 | flushImpl(); | ||
156 | } | ||
157 | } | ||
158 | |||
159 | /** | ||
160 | * Template method to provide the implementation for | ||
161 | * flushing any buffered audio data. | ||
162 | */ | ||
163 | protected void flushImpl() | ||
164 | { | ||
165 | } | ||
166 | |||
167 | /** | ||
168 | * Retrieves the decoder that provides audio data to this | ||
169 | * audio device. | ||
170 | * | ||
171 | * @return The associated decoder. | ||
172 | */ | ||
173 | protected Decoder getDecoder() | ||
174 | { | ||
175 | return decoder; | ||
176 | } | ||
177 | } | ||