diff options
Diffstat (limited to 'songdbj/org/tritonus/share/sampled/mixer/TMixer.java')
-rw-r--r-- | songdbj/org/tritonus/share/sampled/mixer/TMixer.java | 506 |
1 files changed, 0 insertions, 506 deletions
diff --git a/songdbj/org/tritonus/share/sampled/mixer/TMixer.java b/songdbj/org/tritonus/share/sampled/mixer/TMixer.java deleted file mode 100644 index 6a5dc4db72..0000000000 --- a/songdbj/org/tritonus/share/sampled/mixer/TMixer.java +++ /dev/null | |||
@@ -1,506 +0,0 @@ | |||
1 | /* | ||
2 | * TMixer.java | ||
3 | * | ||
4 | * This file is part of Tritonus: http://www.tritonus.org/ | ||
5 | */ | ||
6 | |||
7 | /* | ||
8 | * Copyright (c) 1999 - 2004 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.util.ArrayList; | ||
34 | import java.util.Collection; | ||
35 | import java.util.Iterator; | ||
36 | import java.util.Set; | ||
37 | |||
38 | import javax.sound.sampled.AudioFormat; | ||
39 | import javax.sound.sampled.Clip; | ||
40 | import javax.sound.sampled.DataLine; | ||
41 | import javax.sound.sampled.Line; | ||
42 | import javax.sound.sampled.LineUnavailableException; | ||
43 | import javax.sound.sampled.Mixer; | ||
44 | import javax.sound.sampled.Port; | ||
45 | import javax.sound.sampled.SourceDataLine; | ||
46 | import javax.sound.sampled.TargetDataLine; | ||
47 | |||
48 | import org.tritonus.share.TDebug; | ||
49 | import org.tritonus.share.sampled.AudioFormats; | ||
50 | import org.tritonus.share.ArraySet; | ||
51 | |||
52 | |||
53 | |||
54 | // TODO: global controls (that use the system mixer) | ||
55 | public abstract class TMixer | ||
56 | extends TLine | ||
57 | implements Mixer | ||
58 | { | ||
59 | private static Line.Info[] EMPTY_LINE_INFO_ARRAY = new Line.Info[0]; | ||
60 | private static Line[] EMPTY_LINE_ARRAY = new Line[0]; | ||
61 | |||
62 | private Mixer.Info m_mixerInfo; | ||
63 | private Collection<AudioFormat> m_supportedSourceFormats; | ||
64 | private Collection<AudioFormat> m_supportedTargetFormats; | ||
65 | private Collection<Line.Info> m_supportedSourceLineInfos; | ||
66 | private Collection<Line.Info> m_supportedTargetLineInfos; | ||
67 | private Set<SourceDataLine> m_openSourceDataLines; | ||
68 | private Set<TargetDataLine> m_openTargetDataLines; | ||
69 | |||
70 | |||
71 | /** Constructor for mixers that use setSupportInformation(). | ||
72 | */ | ||
73 | protected TMixer(Mixer.Info mixerInfo, | ||
74 | Line.Info lineInfo) | ||
75 | { | ||
76 | this(mixerInfo, | ||
77 | lineInfo, | ||
78 | new ArrayList<AudioFormat>(), | ||
79 | new ArrayList<AudioFormat>(), | ||
80 | new ArrayList<Line.Info>(), | ||
81 | new ArrayList<Line.Info>()); | ||
82 | } | ||
83 | |||
84 | |||
85 | |||
86 | /** Constructor for mixers. | ||
87 | */ | ||
88 | protected TMixer(Mixer.Info mixerInfo, | ||
89 | Line.Info lineInfo, | ||
90 | Collection<AudioFormat> supportedSourceFormats, | ||
91 | Collection<AudioFormat> supportedTargetFormats, | ||
92 | Collection<Line.Info> supportedSourceLineInfos, | ||
93 | Collection<Line.Info> supportedTargetLineInfos) | ||
94 | { | ||
95 | super(null, // TMixer | ||
96 | lineInfo); | ||
97 | if (TDebug.TraceMixer) { TDebug.out("TMixer.<init>(): begin"); } | ||
98 | m_mixerInfo = mixerInfo; | ||
99 | setSupportInformation( | ||
100 | supportedSourceFormats, | ||
101 | supportedTargetFormats, | ||
102 | supportedSourceLineInfos, | ||
103 | supportedTargetLineInfos); | ||
104 | m_openSourceDataLines = new ArraySet<SourceDataLine>(); | ||
105 | m_openTargetDataLines = new ArraySet<TargetDataLine>(); | ||
106 | if (TDebug.TraceMixer) { TDebug.out("TMixer.<init>(): end"); } | ||
107 | } | ||
108 | |||
109 | |||
110 | |||
111 | protected void setSupportInformation( | ||
112 | Collection<AudioFormat> supportedSourceFormats, | ||
113 | Collection<AudioFormat> supportedTargetFormats, | ||
114 | Collection<Line.Info> supportedSourceLineInfos, | ||
115 | Collection<Line.Info> supportedTargetLineInfos) | ||
116 | { | ||
117 | if (TDebug.TraceMixer) { TDebug.out("TMixer.setSupportInformation(): begin"); } | ||
118 | m_supportedSourceFormats = supportedSourceFormats; | ||
119 | m_supportedTargetFormats = supportedTargetFormats; | ||
120 | m_supportedSourceLineInfos = supportedSourceLineInfos; | ||
121 | m_supportedTargetLineInfos = supportedTargetLineInfos; | ||
122 | if (TDebug.TraceMixer) { TDebug.out("TMixer.setSupportInformation(): end"); } | ||
123 | } | ||
124 | |||
125 | |||
126 | |||
127 | public Mixer.Info getMixerInfo() | ||
128 | { | ||
129 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getMixerInfo(): begin"); } | ||
130 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getMixerInfo(): end"); } | ||
131 | return m_mixerInfo; | ||
132 | } | ||
133 | |||
134 | |||
135 | |||
136 | public Line.Info[] getSourceLineInfo() | ||
137 | { | ||
138 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getSourceLineInfo(): begin"); } | ||
139 | Line.Info[] infos = (Line.Info[]) m_supportedSourceLineInfos.toArray(EMPTY_LINE_INFO_ARRAY); | ||
140 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getSourceLineInfo(): end"); } | ||
141 | return infos; | ||
142 | } | ||
143 | |||
144 | |||
145 | |||
146 | public Line.Info[] getTargetLineInfo() | ||
147 | { | ||
148 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getTargetLineInfo(): begin"); } | ||
149 | Line.Info[] infos = (Line.Info[]) m_supportedTargetLineInfos.toArray(EMPTY_LINE_INFO_ARRAY); | ||
150 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getTargetLineInfo(): end"); } | ||
151 | return infos; | ||
152 | } | ||
153 | |||
154 | |||
155 | |||
156 | public Line.Info[] getSourceLineInfo(Line.Info info) | ||
157 | { | ||
158 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getSourceLineInfo(Line.Info): info to test: " + info); } | ||
159 | // TODO: | ||
160 | return EMPTY_LINE_INFO_ARRAY; | ||
161 | } | ||
162 | |||
163 | |||
164 | |||
165 | public Line.Info[] getTargetLineInfo(Line.Info info) | ||
166 | { | ||
167 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getTargetLineInfo(Line.Info): info to test: " + info); } | ||
168 | // TODO: | ||
169 | return EMPTY_LINE_INFO_ARRAY; | ||
170 | } | ||
171 | |||
172 | |||
173 | |||
174 | public boolean isLineSupported(Line.Info info) | ||
175 | { | ||
176 | if (TDebug.TraceMixer) { TDebug.out("TMixer.isLineSupported(): info to test: " + info); } | ||
177 | Class lineClass = info.getLineClass(); | ||
178 | if (lineClass.equals(SourceDataLine.class)) | ||
179 | { | ||
180 | return isLineSupportedImpl(info, m_supportedSourceLineInfos); | ||
181 | } | ||
182 | else if (lineClass.equals(TargetDataLine.class)) | ||
183 | { | ||
184 | return isLineSupportedImpl(info, m_supportedTargetLineInfos); | ||
185 | } | ||
186 | else if (lineClass.equals(Port.class)) | ||
187 | { | ||
188 | return isLineSupportedImpl(info, m_supportedSourceLineInfos) || isLineSupportedImpl(info, m_supportedTargetLineInfos); | ||
189 | } | ||
190 | else | ||
191 | { | ||
192 | return false; | ||
193 | } | ||
194 | } | ||
195 | |||
196 | |||
197 | |||
198 | private static boolean isLineSupportedImpl(Line.Info info, Collection supportedLineInfos) | ||
199 | { | ||
200 | Iterator iterator = supportedLineInfos.iterator(); | ||
201 | while (iterator.hasNext()) | ||
202 | { | ||
203 | Line.Info info2 = (Line.Info) iterator.next(); | ||
204 | if (info2.matches(info)) | ||
205 | { | ||
206 | return true; | ||
207 | } | ||
208 | } | ||
209 | return false; | ||
210 | } | ||
211 | |||
212 | |||
213 | |||
214 | public Line getLine(Line.Info info) | ||
215 | throws LineUnavailableException | ||
216 | { | ||
217 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getLine(): begin"); } | ||
218 | Class lineClass = info.getLineClass(); | ||
219 | DataLine.Info dataLineInfo = null; | ||
220 | Port.Info portInfo = null; | ||
221 | AudioFormat[] aFormats = null; | ||
222 | if (info instanceof DataLine.Info) | ||
223 | { | ||
224 | dataLineInfo = (DataLine.Info) info; | ||
225 | aFormats = dataLineInfo.getFormats(); | ||
226 | } | ||
227 | else if (info instanceof Port.Info) | ||
228 | { | ||
229 | portInfo = (Port.Info) info; | ||
230 | } | ||
231 | AudioFormat format = null; | ||
232 | Line line = null; | ||
233 | if (lineClass == SourceDataLine.class) | ||
234 | { | ||
235 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getLine(): type: SourceDataLine"); } | ||
236 | if (dataLineInfo == null) | ||
237 | { | ||
238 | throw new IllegalArgumentException("need DataLine.Info for SourceDataLine"); | ||
239 | } | ||
240 | format = getSupportedSourceFormat(aFormats); | ||
241 | line = getSourceDataLine(format, dataLineInfo.getMaxBufferSize()); | ||
242 | } | ||
243 | else if (lineClass == Clip.class) | ||
244 | { | ||
245 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getLine(): type: Clip"); } | ||
246 | if (dataLineInfo == null) | ||
247 | { | ||
248 | throw new IllegalArgumentException("need DataLine.Info for Clip"); | ||
249 | } | ||
250 | format = getSupportedSourceFormat(aFormats); | ||
251 | line = getClip(format); | ||
252 | } | ||
253 | else if (lineClass == TargetDataLine.class) | ||
254 | { | ||
255 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getLine(): type: TargetDataLine"); } | ||
256 | if (dataLineInfo == null) | ||
257 | { | ||
258 | throw new IllegalArgumentException("need DataLine.Info for TargetDataLine"); | ||
259 | } | ||
260 | format = getSupportedTargetFormat(aFormats); | ||
261 | line = getTargetDataLine(format, dataLineInfo.getMaxBufferSize()); | ||
262 | } | ||
263 | else if (lineClass == Port.class) | ||
264 | { | ||
265 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getLine(): type: TargetDataLine"); } | ||
266 | if (portInfo == null) | ||
267 | { | ||
268 | throw new IllegalArgumentException("need Port.Info for Port"); | ||
269 | } | ||
270 | line = getPort(portInfo); | ||
271 | } | ||
272 | else | ||
273 | { | ||
274 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getLine(): unknown line type, will throw exception"); } | ||
275 | throw new LineUnavailableException("unknown line class: " + lineClass); | ||
276 | } | ||
277 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getLine(): end"); } | ||
278 | return line; | ||
279 | } | ||
280 | |||
281 | |||
282 | |||
283 | protected SourceDataLine getSourceDataLine(AudioFormat format, int nBufferSize) | ||
284 | throws LineUnavailableException | ||
285 | { | ||
286 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getSourceDataLine(): begin"); } | ||
287 | throw new IllegalArgumentException("this mixer does not support SourceDataLines"); | ||
288 | } | ||
289 | |||
290 | |||
291 | |||
292 | protected Clip getClip(AudioFormat format) | ||
293 | throws LineUnavailableException | ||
294 | { | ||
295 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getClip(): begin"); } | ||
296 | throw new IllegalArgumentException("this mixer does not support Clips"); | ||
297 | } | ||
298 | |||
299 | |||
300 | |||
301 | protected TargetDataLine getTargetDataLine(AudioFormat format, int nBufferSize) | ||
302 | throws LineUnavailableException | ||
303 | { | ||
304 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getTargetDataLine(): begin"); } | ||
305 | throw new IllegalArgumentException("this mixer does not support TargetDataLines"); | ||
306 | } | ||
307 | |||
308 | |||
309 | |||
310 | protected Port getPort(Port.Info info) | ||
311 | throws LineUnavailableException | ||
312 | { | ||
313 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getTargetDataLine(): begin"); } | ||
314 | throw new IllegalArgumentException("this mixer does not support Ports"); | ||
315 | } | ||
316 | |||
317 | |||
318 | |||
319 | private AudioFormat getSupportedSourceFormat(AudioFormat[] aFormats) | ||
320 | { | ||
321 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getSupportedSourceFormat(): begin"); } | ||
322 | AudioFormat format = null; | ||
323 | for (int i = 0; i < aFormats.length; i++) | ||
324 | { | ||
325 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getSupportedSourceFormat(): checking " + aFormats[i] + "..."); } | ||
326 | if (isSourceFormatSupported(aFormats[i])) | ||
327 | { | ||
328 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getSupportedSourceFormat(): ...supported"); } | ||
329 | format = aFormats[i]; | ||
330 | break; | ||
331 | } | ||
332 | else | ||
333 | { | ||
334 | if (TDebug.TraceMixer) | ||
335 | { | ||
336 | TDebug.out("TMixer.getSupportedSourceFormat(): ...no luck"); | ||
337 | } | ||
338 | } | ||
339 | } | ||
340 | if (format == null) | ||
341 | { | ||
342 | throw new IllegalArgumentException("no line matchine one of the passed formats"); | ||
343 | } | ||
344 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getSupportedSourceFormat(): end"); } | ||
345 | return format; | ||
346 | } | ||
347 | |||
348 | |||
349 | |||
350 | private AudioFormat getSupportedTargetFormat(AudioFormat[] aFormats) | ||
351 | { | ||
352 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getSupportedTargetFormat(): begin"); } | ||
353 | AudioFormat format = null; | ||
354 | for (int i = 0; i < aFormats.length; i++) | ||
355 | { | ||
356 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getSupportedTargetFormat(): checking " + aFormats[i] + " ..."); } | ||
357 | if (isTargetFormatSupported(aFormats[i])) | ||
358 | { | ||
359 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getSupportedTargetFormat(): ...supported"); } | ||
360 | format = aFormats[i]; | ||
361 | break; | ||
362 | } | ||
363 | else | ||
364 | { | ||
365 | if (TDebug.TraceMixer) | ||
366 | { | ||
367 | TDebug.out("TMixer.getSupportedTargetFormat(): ...no luck"); | ||
368 | } | ||
369 | } | ||
370 | } | ||
371 | if (format == null) | ||
372 | { | ||
373 | throw new IllegalArgumentException("no line matchine one of the passed formats"); | ||
374 | } | ||
375 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getSupportedTargetFormat(): end"); } | ||
376 | return format; | ||
377 | } | ||
378 | |||
379 | |||
380 | |||
381 | /* | ||
382 | not implemented here: | ||
383 | getMaxLines(Line.Info) | ||
384 | */ | ||
385 | |||
386 | |||
387 | |||
388 | public Line[] getSourceLines() | ||
389 | { | ||
390 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getSourceLines(): called"); } | ||
391 | return (Line[]) m_openSourceDataLines.toArray(EMPTY_LINE_ARRAY); | ||
392 | } | ||
393 | |||
394 | |||
395 | |||
396 | public Line[] getTargetLines() | ||
397 | { | ||
398 | if (TDebug.TraceMixer) { TDebug.out("TMixer.getTargetLines(): called"); } | ||
399 | return (Line[]) m_openTargetDataLines.toArray(EMPTY_LINE_ARRAY); | ||
400 | } | ||
401 | |||
402 | |||
403 | |||
404 | public void synchronize(Line[] aLines, | ||
405 | boolean bMaintainSync) | ||
406 | { | ||
407 | throw new IllegalArgumentException("synchronization not supported"); | ||
408 | } | ||
409 | |||
410 | |||
411 | |||
412 | public void unsynchronize(Line[] aLines) | ||
413 | { | ||
414 | throw new IllegalArgumentException("synchronization not supported"); | ||
415 | } | ||
416 | |||
417 | |||
418 | |||
419 | public boolean isSynchronizationSupported(Line[] aLines, | ||
420 | boolean bMaintainSync) | ||
421 | { | ||
422 | return false; | ||
423 | } | ||
424 | |||
425 | |||
426 | |||
427 | protected boolean isSourceFormatSupported(AudioFormat format) | ||
428 | { | ||
429 | if (TDebug.TraceMixer) { TDebug.out("TMixer.isSourceFormatSupported(): format to test: " + format); } | ||
430 | Iterator<AudioFormat> iterator = m_supportedSourceFormats.iterator(); | ||
431 | while (iterator.hasNext()) | ||
432 | { | ||
433 | AudioFormat supportedFormat = iterator.next(); | ||
434 | if (AudioFormats.matches(supportedFormat, format)) | ||
435 | { | ||
436 | return true; | ||
437 | } | ||
438 | } | ||
439 | return false; | ||
440 | } | ||
441 | |||
442 | |||
443 | |||
444 | protected boolean isTargetFormatSupported(AudioFormat format) | ||
445 | { | ||
446 | if (TDebug.TraceMixer) { TDebug.out("TMixer.isTargetFormatSupported(): format to test: " + format); } | ||
447 | Iterator<AudioFormat> iterator = m_supportedTargetFormats.iterator(); | ||
448 | while (iterator.hasNext()) | ||
449 | { | ||
450 | AudioFormat supportedFormat = iterator.next(); | ||
451 | if (AudioFormats.matches(supportedFormat, format)) | ||
452 | { | ||
453 | return true; | ||
454 | } | ||
455 | } | ||
456 | return false; | ||
457 | } | ||
458 | |||
459 | |||
460 | |||
461 | /*package*/ void registerOpenLine(Line line) | ||
462 | { | ||
463 | if (TDebug.TraceMixer) { TDebug.out("TMixer.registerOpenLine(): line to register: " + line); | ||
464 | } | ||
465 | if (line instanceof SourceDataLine) | ||
466 | { | ||
467 | synchronized (m_openSourceDataLines) | ||
468 | { | ||
469 | m_openSourceDataLines.add((SourceDataLine) line); | ||
470 | } | ||
471 | } | ||
472 | else if (line instanceof TargetDataLine) | ||
473 | { | ||
474 | synchronized (m_openSourceDataLines) | ||
475 | { | ||
476 | m_openTargetDataLines.add((TargetDataLine) line); | ||
477 | } | ||
478 | } | ||
479 | } | ||
480 | |||
481 | |||
482 | |||
483 | /*package*/ void unregisterOpenLine(Line line) | ||
484 | { | ||
485 | if (TDebug.TraceMixer) { TDebug.out("TMixer.unregisterOpenLine(): line to unregister: " + line); } | ||
486 | if (line instanceof SourceDataLine) | ||
487 | { | ||
488 | synchronized (m_openSourceDataLines) | ||
489 | { | ||
490 | m_openSourceDataLines.remove((SourceDataLine) line); | ||
491 | } | ||
492 | } | ||
493 | else if (line instanceof TargetDataLine) | ||
494 | { | ||
495 | synchronized (m_openTargetDataLines) | ||
496 | { | ||
497 | m_openTargetDataLines.remove((TargetDataLine) line); | ||
498 | } | ||
499 | } | ||
500 | } | ||
501 | } | ||
502 | |||
503 | |||
504 | |||
505 | /*** TMixer.java ***/ | ||
506 | |||