diff options
author | Michiel Van Der Kolk <not.valid@email.address> | 2005-07-11 15:42:37 +0000 |
---|---|---|
committer | Michiel Van Der Kolk <not.valid@email.address> | 2005-07-11 15:42:37 +0000 |
commit | 9fee0ec4ca0c5b7a334cc29dbb58e76c7a4c736e (patch) | |
tree | 4c304cd4151020bd5494d279ee68a105ae3a5a3a /songdbj/org/tritonus/share/sampled/mixer/TMixerProvider.java | |
parent | dfa8ecbe609ca8ea194d08560a44fb9a92e94b4b (diff) | |
download | rockbox-9fee0ec4ca0c5b7a334cc29dbb58e76c7a4c736e.tar.gz rockbox-9fee0ec4ca0c5b7a334cc29dbb58e76c7a4c736e.zip |
Songdb java version, source. only 1.5 compatible
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7101 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'songdbj/org/tritonus/share/sampled/mixer/TMixerProvider.java')
-rw-r--r-- | songdbj/org/tritonus/share/sampled/mixer/TMixerProvider.java | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/songdbj/org/tritonus/share/sampled/mixer/TMixerProvider.java b/songdbj/org/tritonus/share/sampled/mixer/TMixerProvider.java new file mode 100644 index 0000000000..3116d74dc3 --- /dev/null +++ b/songdbj/org/tritonus/share/sampled/mixer/TMixerProvider.java | |||
@@ -0,0 +1,240 @@ | |||
1 | /* | ||
2 | * TMixerProvider.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 | * This program is free software; you can redistribute it and/or modify | ||
11 | * it under the terms of the GNU Library General Public License as published | ||
12 | * by the Free Software Foundation; either version 2 of the License, or | ||
13 | * (at your option) any later version. | ||
14 | * | ||
15 | * This program is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
18 | * GNU Library General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU Library General Public | ||
21 | * License along with this program; if not, write to the Free Software | ||
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
23 | */ | ||
24 | |||
25 | /* | ||
26 | |<--- this code is formatted to fit into 80 columns --->| | ||
27 | */ | ||
28 | |||
29 | package org.tritonus.share.sampled.mixer; | ||
30 | |||
31 | import java.util.ArrayList; | ||
32 | import java.util.HashMap; | ||
33 | import java.util.HashSet; | ||
34 | import java.util.Iterator; | ||
35 | import java.util.List; | ||
36 | import java.util.Map; | ||
37 | import java.util.Set; | ||
38 | |||
39 | import javax.sound.sampled.Mixer; | ||
40 | import javax.sound.sampled.spi.MixerProvider; | ||
41 | |||
42 | import org.tritonus.share.TDebug; | ||
43 | |||
44 | |||
45 | |||
46 | public abstract class TMixerProvider | ||
47 | extends MixerProvider | ||
48 | { | ||
49 | private static final Mixer.Info[] EMPTY_MIXER_INFO_ARRAY = new Mixer.Info[0]; | ||
50 | |||
51 | private static Map<Class, MixerProviderStruct> sm_mixerProviderStructs = new HashMap<Class, MixerProviderStruct>(); | ||
52 | |||
53 | private boolean m_bDisabled = false; | ||
54 | |||
55 | |||
56 | |||
57 | |||
58 | public TMixerProvider() | ||
59 | { | ||
60 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.<init>(): begin"); } | ||
61 | // currently does nothing | ||
62 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.<init>(): end"); } | ||
63 | } | ||
64 | |||
65 | |||
66 | |||
67 | /* | ||
68 | Override this method if you want a thread-safe static initializaiton. | ||
69 | */ | ||
70 | protected void staticInit() | ||
71 | { | ||
72 | } | ||
73 | |||
74 | |||
75 | |||
76 | private MixerProviderStruct getMixerProviderStruct() | ||
77 | { | ||
78 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.getMixerProviderStruct(): begin"); } | ||
79 | Class cls = this.getClass(); | ||
80 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.getMixerProviderStruct(): called from " + cls); } | ||
81 | // Thread.dumpStack(); | ||
82 | synchronized (TMixerProvider.class) | ||
83 | { | ||
84 | MixerProviderStruct struct = sm_mixerProviderStructs.get(cls); | ||
85 | if (struct == null) | ||
86 | { | ||
87 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.getMixerProviderStruct(): creating new MixerProviderStruct for " + cls); } | ||
88 | struct = new MixerProviderStruct(); | ||
89 | sm_mixerProviderStructs.put(cls, struct); | ||
90 | } | ||
91 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.getMixerProviderStruct(): end"); } | ||
92 | return struct; | ||
93 | } | ||
94 | } | ||
95 | |||
96 | |||
97 | |||
98 | protected void disable() | ||
99 | { | ||
100 | if (TDebug.TraceMixerProvider) { TDebug.out("disabling " + getClass().getName()); } | ||
101 | m_bDisabled = true; | ||
102 | } | ||
103 | |||
104 | |||
105 | protected boolean isDisabled() | ||
106 | { | ||
107 | return m_bDisabled; | ||
108 | } | ||
109 | |||
110 | |||
111 | |||
112 | protected void addMixer(Mixer mixer) | ||
113 | { | ||
114 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.addMixer(): begin"); } | ||
115 | MixerProviderStruct struct = getMixerProviderStruct(); | ||
116 | synchronized (struct) | ||
117 | { | ||
118 | struct.m_mixers.add(mixer); | ||
119 | if (struct.m_defaultMixer == null) | ||
120 | { | ||
121 | struct.m_defaultMixer = mixer; | ||
122 | } | ||
123 | } | ||
124 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.addMixer(): end"); } | ||
125 | } | ||
126 | |||
127 | |||
128 | |||
129 | protected void removeMixer(Mixer mixer) | ||
130 | { | ||
131 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.removeMixer(): begin"); } | ||
132 | MixerProviderStruct struct = getMixerProviderStruct(); | ||
133 | synchronized (struct) | ||
134 | { | ||
135 | struct.m_mixers.remove(mixer); | ||
136 | // TODO: should search for another mixer | ||
137 | if (struct.m_defaultMixer == mixer) | ||
138 | { | ||
139 | struct.m_defaultMixer = null; | ||
140 | } | ||
141 | } | ||
142 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.removeMixer(): end"); } | ||
143 | } | ||
144 | |||
145 | |||
146 | // $$mp 2003/01/11: TODO: this implementation may become obsolete once the overridden method in spi.MixerProvider is implemented in a way documented officially. | ||
147 | public boolean isMixerSupported(Mixer.Info info) | ||
148 | { | ||
149 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.isMixerSupported(): begin"); } | ||
150 | boolean bIsSupported = false; | ||
151 | Mixer.Info[] infos = getMixerInfo(); | ||
152 | for (int i = 0; i < infos.length; i++) | ||
153 | { | ||
154 | if (infos[i].equals(info)) | ||
155 | { | ||
156 | bIsSupported = true; | ||
157 | break; | ||
158 | } | ||
159 | } | ||
160 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.isMixerSupported(): end"); } | ||
161 | return bIsSupported; | ||
162 | } | ||
163 | |||
164 | |||
165 | |||
166 | /** | ||
167 | */ | ||
168 | public Mixer getMixer(Mixer.Info info) | ||
169 | { | ||
170 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.getMixer(): begin"); } | ||
171 | MixerProviderStruct struct = getMixerProviderStruct(); | ||
172 | Mixer mixerResult = null; | ||
173 | synchronized (struct) | ||
174 | { | ||
175 | if (info == null) | ||
176 | { | ||
177 | mixerResult = struct.m_defaultMixer; | ||
178 | } | ||
179 | else | ||
180 | { | ||
181 | Iterator mixers = struct.m_mixers.iterator(); | ||
182 | while (mixers.hasNext()) | ||
183 | { | ||
184 | Mixer mixer = (Mixer) mixers.next(); | ||
185 | if (mixer.getMixerInfo().equals(info)) | ||
186 | { | ||
187 | mixerResult = mixer; | ||
188 | break; | ||
189 | } | ||
190 | } | ||
191 | } | ||
192 | } | ||
193 | if (mixerResult == null) | ||
194 | { | ||
195 | throw new IllegalArgumentException("no mixer available for " + info); | ||
196 | } | ||
197 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.getMixer(): end"); } | ||
198 | return mixerResult; | ||
199 | } | ||
200 | |||
201 | |||
202 | |||
203 | public Mixer.Info[] getMixerInfo() | ||
204 | { | ||
205 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.getMixerInfo(): begin"); } | ||
206 | Set<Mixer.Info> mixerInfos = new HashSet<Mixer.Info>(); | ||
207 | MixerProviderStruct struct = getMixerProviderStruct(); | ||
208 | synchronized (struct) | ||
209 | { | ||
210 | Iterator<Mixer> mixers = struct.m_mixers.iterator(); | ||
211 | while (mixers.hasNext()) | ||
212 | { | ||
213 | Mixer mixer = mixers.next(); | ||
214 | mixerInfos.add(mixer.getMixerInfo()); | ||
215 | } | ||
216 | } | ||
217 | if (TDebug.TraceMixerProvider) { TDebug.out("TMixerProvider.getMixerInfo(): end"); } | ||
218 | return mixerInfos.toArray(EMPTY_MIXER_INFO_ARRAY); | ||
219 | } | ||
220 | |||
221 | |||
222 | |||
223 | private class MixerProviderStruct | ||
224 | { | ||
225 | public List<Mixer> m_mixers; | ||
226 | public Mixer m_defaultMixer; | ||
227 | |||
228 | |||
229 | |||
230 | public MixerProviderStruct() | ||
231 | { | ||
232 | m_mixers = new ArrayList<Mixer>(); | ||
233 | m_defaultMixer = null; | ||
234 | } | ||
235 | } | ||
236 | } | ||
237 | |||
238 | |||
239 | |||
240 | /*** TMixerProvider.java ***/ | ||