diff options
Diffstat (limited to 'songdbj/org/tritonus/share/sampled/mixer/TSoftClip.java')
-rw-r--r-- | songdbj/org/tritonus/share/sampled/mixer/TSoftClip.java | 318 |
1 files changed, 318 insertions, 0 deletions
diff --git a/songdbj/org/tritonus/share/sampled/mixer/TSoftClip.java b/songdbj/org/tritonus/share/sampled/mixer/TSoftClip.java new file mode 100644 index 0000000000..b5a8aea2c1 --- /dev/null +++ b/songdbj/org/tritonus/share/sampled/mixer/TSoftClip.java | |||
@@ -0,0 +1,318 @@ | |||
1 | /* | ||
2 | * TSoftClip.java | ||
3 | * | ||
4 | * This file is part of Tritonus: http://www.tritonus.org/ | ||
5 | */ | ||
6 | |||
7 | /* | ||
8 | * Copyright (c) 1999 by Matthias Pfisterer | ||
9 | * | ||
10 | * | ||
11 | * This program is free software; you can redistribute it and/or modify | ||
12 | * it under the terms of the GNU Library General Public License as published | ||
13 | * by the Free Software Foundation; either version 2 of the License, or | ||
14 | * (at your option) any later version. | ||
15 | * | ||
16 | * This program is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU Library General Public License for more details. | ||
20 | * | ||
21 | * You should have received a copy of the GNU Library General Public | ||
22 | * License along with this program; if not, write to the Free Software | ||
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
24 | * | ||
25 | */ | ||
26 | |||
27 | /* | ||
28 | |<--- this code is formatted to fit into 80 columns --->| | ||
29 | */ | ||
30 | |||
31 | package org.tritonus.share.sampled.mixer; | ||
32 | |||
33 | import java.io.IOException; | ||
34 | import java.io.ByteArrayInputStream; | ||
35 | import java.io.ByteArrayOutputStream; | ||
36 | |||
37 | import javax.sound.sampled.AudioFormat; | ||
38 | import javax.sound.sampled.AudioSystem; | ||
39 | import javax.sound.sampled.Clip; | ||
40 | import javax.sound.sampled.DataLine; | ||
41 | import javax.sound.sampled.SourceDataLine; | ||
42 | import javax.sound.sampled.AudioInputStream; | ||
43 | import javax.sound.sampled.LineUnavailableException; | ||
44 | import javax.sound.sampled.Mixer; | ||
45 | |||
46 | import org.tritonus.share.TDebug; | ||
47 | import org.tritonus.share.sampled.mixer.TDataLine; | ||
48 | |||
49 | |||
50 | |||
51 | public class TSoftClip | ||
52 | extends TClip | ||
53 | implements Runnable | ||
54 | { | ||
55 | private static final Class[] CONTROL_CLASSES = {/*GainControl.class*/}; | ||
56 | private static final int BUFFER_SIZE = 16384; | ||
57 | |||
58 | |||
59 | private Mixer m_mixer; | ||
60 | private SourceDataLine m_line; | ||
61 | private byte[] m_abClip; | ||
62 | private int m_nRepeatCount; | ||
63 | private Thread m_thread; | ||
64 | |||
65 | public TSoftClip(Mixer mixer, AudioFormat format) | ||
66 | throws LineUnavailableException | ||
67 | { | ||
68 | // TODO: info object | ||
69 | /* | ||
70 | DataLine.Info info = new DataLine.Info(Clip.class, | ||
71 | audioFormat, -1); | ||
72 | */ | ||
73 | super(null); | ||
74 | m_mixer = mixer; | ||
75 | DataLine.Info info = new DataLine.Info( | ||
76 | SourceDataLine.class, | ||
77 | // TODO: should pass a real AudioFormat object that isn't too restrictive | ||
78 | format); | ||
79 | m_line = (SourceDataLine) AudioSystem.getLine(info); | ||
80 | } | ||
81 | |||
82 | |||
83 | |||
84 | public void open(AudioInputStream audioInputStream) | ||
85 | throws LineUnavailableException, IOException | ||
86 | { | ||
87 | AudioFormat audioFormat = audioInputStream.getFormat(); | ||
88 | setFormat(audioFormat); | ||
89 | int nFrameSize = audioFormat.getFrameSize(); | ||
90 | if (nFrameSize < 1) | ||
91 | { | ||
92 | throw new IllegalArgumentException("frame size must be positive"); | ||
93 | } | ||
94 | if (TDebug.TraceClip) | ||
95 | { | ||
96 | TDebug.out("TSoftClip.open(): format: " + audioFormat); | ||
97 | // TDebug.out("sample rate: " + audioFormat.getSampleRate()); | ||
98 | } | ||
99 | byte[] abData = new byte[BUFFER_SIZE]; | ||
100 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
101 | int nBytesRead = 0; | ||
102 | while (nBytesRead != -1) | ||
103 | { | ||
104 | try | ||
105 | { | ||
106 | nBytesRead = audioInputStream.read(abData, 0, abData.length); | ||
107 | } | ||
108 | catch (IOException e) | ||
109 | { | ||
110 | if (TDebug.TraceClip || TDebug.TraceAllExceptions) | ||
111 | { | ||
112 | TDebug.out(e); | ||
113 | } | ||
114 | } | ||
115 | if (nBytesRead >= 0) | ||
116 | { | ||
117 | if (TDebug.TraceClip) | ||
118 | { | ||
119 | TDebug.out("TSoftClip.open(): Trying to write: " + nBytesRead); | ||
120 | } | ||
121 | baos.write(abData, 0, nBytesRead); | ||
122 | if (TDebug.TraceClip) | ||
123 | { | ||
124 | TDebug.out("TSoftClip.open(): Written: " + nBytesRead); | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | m_abClip = baos.toByteArray(); | ||
129 | setBufferSize(m_abClip.length); | ||
130 | // open the line | ||
131 | m_line.open(getFormat()); | ||
132 | // to trigger the events | ||
133 | // open(); | ||
134 | } | ||
135 | |||
136 | |||
137 | |||
138 | public int getFrameLength() | ||
139 | { | ||
140 | if (isOpen()) | ||
141 | { | ||
142 | return getBufferSize() / getFormat().getFrameSize(); | ||
143 | } | ||
144 | else | ||
145 | { | ||
146 | return AudioSystem.NOT_SPECIFIED; | ||
147 | } | ||
148 | } | ||
149 | |||
150 | |||
151 | |||
152 | public long getMicrosecondLength() | ||
153 | { | ||
154 | if (isOpen()) | ||
155 | { | ||
156 | return (long) (getFrameLength() * getFormat().getFrameRate() * 1000000); | ||
157 | } | ||
158 | else | ||
159 | { | ||
160 | return AudioSystem.NOT_SPECIFIED; | ||
161 | } | ||
162 | } | ||
163 | |||
164 | |||
165 | |||
166 | public void setFramePosition(int nPosition) | ||
167 | { | ||
168 | // TOOD: | ||
169 | } | ||
170 | |||
171 | |||
172 | |||
173 | public void setMicrosecondPosition(long lPosition) | ||
174 | { | ||
175 | // TOOD: | ||
176 | } | ||
177 | |||
178 | |||
179 | |||
180 | public int getFramePosition() | ||
181 | { | ||
182 | // TOOD: | ||
183 | return -1; | ||
184 | } | ||
185 | |||
186 | |||
187 | |||
188 | public long getMicrosecondPosition() | ||
189 | { | ||
190 | // TOOD: | ||
191 | return -1; | ||
192 | } | ||
193 | |||
194 | |||
195 | |||
196 | public void setLoopPoints(int nStart, int nEnd) | ||
197 | { | ||
198 | // TOOD: | ||
199 | } | ||
200 | |||
201 | |||
202 | |||
203 | public void loop(int nCount) | ||
204 | { | ||
205 | if (TDebug.TraceClip) | ||
206 | { | ||
207 | TDebug.out("TSoftClip.loop(int): called; count = " + nCount); | ||
208 | } | ||
209 | if (false/*isStarted()*/) | ||
210 | { | ||
211 | /* | ||
212 | * only allow zero count to stop the looping | ||
213 | * at the end of an iteration. | ||
214 | */ | ||
215 | if (nCount == 0) | ||
216 | { | ||
217 | if (TDebug.TraceClip) | ||
218 | { | ||
219 | TDebug.out("TSoftClip.loop(int): stopping sample"); | ||
220 | } | ||
221 | // m_esdSample.stop(); | ||
222 | } | ||
223 | } | ||
224 | else | ||
225 | { | ||
226 | m_nRepeatCount = nCount; | ||
227 | m_thread = new Thread(this); | ||
228 | m_thread.start(); | ||
229 | } | ||
230 | // TOOD: | ||
231 | } | ||
232 | |||
233 | |||
234 | |||
235 | public void flush() | ||
236 | { | ||
237 | // TOOD: | ||
238 | } | ||
239 | |||
240 | |||
241 | |||
242 | public void drain() | ||
243 | { | ||
244 | // TOOD: | ||
245 | } | ||
246 | |||
247 | |||
248 | |||
249 | public void close() | ||
250 | { | ||
251 | // m_esdSample.free(); | ||
252 | // m_esdSample.close(); | ||
253 | // TOOD: | ||
254 | } | ||
255 | |||
256 | |||
257 | |||
258 | |||
259 | public void open() | ||
260 | { | ||
261 | // TODO: | ||
262 | } | ||
263 | |||
264 | |||
265 | |||
266 | public void start() | ||
267 | { | ||
268 | if (TDebug.TraceClip) | ||
269 | { | ||
270 | TDebug.out("TSoftClip.start(): called"); | ||
271 | } | ||
272 | /* | ||
273 | * This is a hack. What start() really should do is | ||
274 | * start playing at the position playback was stopped. | ||
275 | */ | ||
276 | if (TDebug.TraceClip) | ||
277 | { | ||
278 | TDebug.out("TSoftClip.start(): calling 'loop(0)' [hack]"); | ||
279 | } | ||
280 | loop(0); | ||
281 | } | ||
282 | |||
283 | |||
284 | |||
285 | public void stop() | ||
286 | { | ||
287 | // TODO: | ||
288 | // m_esdSample.kill(); | ||
289 | } | ||
290 | |||
291 | |||
292 | |||
293 | /* | ||
294 | * This method is enforced by DataLine, but doesn't make any | ||
295 | * sense for Clips. | ||
296 | */ | ||
297 | public int available() | ||
298 | { | ||
299 | return -1; | ||
300 | } | ||
301 | |||
302 | |||
303 | |||
304 | public void run() | ||
305 | { | ||
306 | while (m_nRepeatCount >= 0) | ||
307 | { | ||
308 | m_line.write(m_abClip, 0, m_abClip.length); | ||
309 | m_nRepeatCount--; | ||
310 | } | ||
311 | } | ||
312 | |||
313 | } | ||
314 | |||
315 | |||
316 | |||
317 | /*** TSoftClip.java ***/ | ||
318 | |||