1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*
* TSimpleFormatConversionProvider.java
*
* This file is part of Tritonus: http://www.tritonus.org/
*/
/*
* Copyright (c) 1999 - 2004 by Matthias Pfisterer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
|<--- this code is formatted to fit into 80 columns --->|
*/
package org.tritonus.share.sampled.convert;
import java.util.Collection;
import java.util.Iterator;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import org.tritonus.share.sampled.AudioFormats;
import org.tritonus.share.ArraySet;
import org.tritonus.share.TDebug;
/**
* This is a base class for FormatConversionProviders that can convert
* from each source encoding/format to each target encoding/format.
* If this is not the case, use TEncodingFormatConversionProvider.
*
* <p>Overriding classes must
* provide a constructor that calls the protected constructor of this class and override
* <code>AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream)</code>.
* The latter method should be able to handle the case that all fields are NOT_SPECIFIED
* and provide appropriate default values.
*
* @author Matthias Pfisterer
*/
// todo:
// - declare a constant ALL_BUT_SAME_VALUE (==-2) or so that can be used in format lists
// - consistent implementation of replacing NOT_SPECIFIED when not given in conversion
public abstract class TSimpleFormatConversionProvider
extends TFormatConversionProvider
{
private Collection<AudioFormat.Encoding> m_sourceEncodings;
private Collection<AudioFormat.Encoding> m_targetEncodings;
private Collection<AudioFormat> m_sourceFormats;
private Collection<AudioFormat> m_targetFormats;
protected TSimpleFormatConversionProvider(
Collection<AudioFormat> sourceFormats,
Collection<AudioFormat> targetFormats)
{
m_sourceEncodings = new ArraySet<AudioFormat.Encoding>();
m_targetEncodings = new ArraySet<AudioFormat.Encoding>();
m_sourceFormats = sourceFormats;
m_targetFormats = targetFormats;
collectEncodings(m_sourceFormats, m_sourceEncodings);
collectEncodings(m_targetFormats, m_targetEncodings);
}
/** Disables this FormatConversionProvider.
This may be useful when e.g. native libraries are not present.
TODO: enable method, better implementation
*/
protected void disable()
{
if (TDebug.TraceAudioConverter) { TDebug.out("TSimpleFormatConversionProvider.disable(): disabling " + getClass().getName()); }
m_sourceEncodings = new ArraySet<AudioFormat.Encoding>();
m_targetEncodings = new ArraySet<AudioFormat.Encoding>();
m_sourceFormats = new ArraySet<AudioFormat>();
m_targetFormats = new ArraySet<AudioFormat>();
}
private static void collectEncodings(Collection<AudioFormat> formats,
Collection<AudioFormat.Encoding> encodings)
{
Iterator<AudioFormat> iterator = formats.iterator();
while (iterator.hasNext())
{
AudioFormat format = iterator.next();
encodings.add(format.getEncoding());
}
}
public AudioFormat.Encoding[] getSourceEncodings()
{
return m_sourceEncodings.toArray(EMPTY_ENCODING_ARRAY);
}
public AudioFormat.Encoding[] getTargetEncodings()
{
return m_targetEncodings.toArray(EMPTY_ENCODING_ARRAY);
}
// overwritten of FormatConversionProvider
public boolean isSourceEncodingSupported(AudioFormat.Encoding sourceEncoding)
{
return m_sourceEncodings.contains(sourceEncoding);
}
// overwritten of FormatConversionProvider
public boolean isTargetEncodingSupported(AudioFormat.Encoding targetEncoding)
{
return m_targetEncodings.contains(targetEncoding);
}
/**
* This implementation assumes that the converter can convert
* from each of its source encodings to each of its target
* encodings. If this is not the case, the converter has to
* override this method.
*/
public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat)
{
if (isAllowedSourceFormat(sourceFormat))
{
return getTargetEncodings();
}
else
{
return EMPTY_ENCODING_ARRAY;
}
}
/**
* This implementation assumes that the converter can convert
* from each of its source formats to each of its target
* formats. If this is not the case, the converter has to
* override this method.
*/
public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat)
{
if (isConversionSupported(targetEncoding, sourceFormat))
{
return m_targetFormats.toArray(EMPTY_FORMAT_ARRAY);
}
else
{
return EMPTY_FORMAT_ARRAY;
}
}
// TODO: check if necessary
protected boolean isAllowedSourceEncoding(AudioFormat.Encoding sourceEncoding)
{
return m_sourceEncodings.contains(sourceEncoding);
}
protected boolean isAllowedTargetEncoding(AudioFormat.Encoding targetEncoding)
{
return m_targetEncodings.contains(targetEncoding);
}
protected boolean isAllowedSourceFormat(AudioFormat sourceFormat)
{
Iterator<AudioFormat> iterator = m_sourceFormats.iterator();
while (iterator.hasNext())
{
AudioFormat format = iterator.next();
if (AudioFormats.matches(format, sourceFormat))
{
return true;
}
}
return false;
}
protected boolean isAllowedTargetFormat(AudioFormat targetFormat)
{
Iterator<AudioFormat> iterator = m_targetFormats.iterator();
while (iterator.hasNext())
{
AudioFormat format = iterator.next();
if (AudioFormats.matches(format, targetFormat))
{
return true;
}
}
return false;
}
// $$fb 2000-04-02 added some convenience methods for overriding classes
protected Collection<AudioFormat.Encoding> getCollectionSourceEncodings()
{
return m_sourceEncodings;
}
protected Collection<AudioFormat.Encoding> getCollectionTargetEncodings()
{
return m_targetEncodings;
}
protected Collection<AudioFormat> getCollectionSourceFormats() {
return m_sourceFormats;
}
protected Collection<AudioFormat> getCollectionTargetFormats() {
return m_targetFormats;
}
/**
* Utility method to check whether these values match,
* taking into account AudioSystem.NOT_SPECIFIED.
* @return true if any of the values is AudioSystem.NOT_SPECIFIED
* or both values have the same value.
*/
//$$fb 2000-08-16: moved from TEncodingFormatConversionProvider
protected static boolean doMatch(int i1, int i2) {
return i1==AudioSystem.NOT_SPECIFIED
|| i2==AudioSystem.NOT_SPECIFIED
|| i1==i2;
}
/**
* @see #doMatch(int,int)
*/
//$$fb 2000-08-16: moved from TEncodingFormatConversionProvider
protected static boolean doMatch(float f1, float f2) {
return f1==AudioSystem.NOT_SPECIFIED
|| f2==AudioSystem.NOT_SPECIFIED
|| Math.abs(f1 - f2) < 1.0e-9;
}
/**
* Utility method, replaces all occurences of AudioSystem.NOT_SPECIFIED
* in <code>targetFormat</code> with the corresponding value in <code>sourceFormat</code>.
* If <code>targetFormat</code> does not contain any fields with AudioSystem.NOT_SPECIFIED,
* it is returned unmodified. The endian-ness and encoding remain the same in all cases.
* <p>
* If any of the fields is AudioSystem.NOT_SPECIFIED in both <code>sourceFormat</code> and
* <code>targetFormat</code>, it will remain not specified.
* <p>
* This method uses <code>getFrameSize(...)</code> (see below) to set the new frameSize,
* if a new AudioFormat instance is created.
* <p>
* This method isn't used in TSimpleFormatConversionProvider - it is solely there
* for inheriting classes.
*/
//$$fb 2000-08-16: moved from TEncodingFormatConversionProvider
protected AudioFormat replaceNotSpecified(AudioFormat sourceFormat, AudioFormat targetFormat) {
boolean bSetSampleSize=false;
boolean bSetChannels=false;
boolean bSetSampleRate=false;
boolean bSetFrameRate=false;
if (targetFormat.getSampleSizeInBits()==AudioSystem.NOT_SPECIFIED
&& sourceFormat.getSampleSizeInBits()!=AudioSystem.NOT_SPECIFIED) {
bSetSampleSize=true;
}
if (targetFormat.getChannels()==AudioSystem.NOT_SPECIFIED
&& sourceFormat.getChannels()!=AudioSystem.NOT_SPECIFIED) {
bSetChannels=true;
}
if (targetFormat.getSampleRate()==AudioSystem.NOT_SPECIFIED
&& sourceFormat.getSampleRate()!=AudioSystem.NOT_SPECIFIED) {
bSetSampleRate=true;
}
if (targetFormat.getFrameRate()==AudioSystem.NOT_SPECIFIED
&& sourceFormat.getFrameRate()!=AudioSystem.NOT_SPECIFIED) {
bSetFrameRate=true;
}
if (bSetSampleSize || bSetChannels || bSetSampleRate || bSetFrameRate
|| (targetFormat.getFrameSize()==AudioSystem.NOT_SPECIFIED
&& sourceFormat.getFrameSize()!=AudioSystem.NOT_SPECIFIED)) {
// create new format in place of the original target format
float sampleRate=bSetSampleRate?
sourceFormat.getSampleRate():targetFormat.getSampleRate();
float frameRate=bSetFrameRate?
sourceFormat.getFrameRate():targetFormat.getFrameRate();
int sampleSize=bSetSampleSize?
sourceFormat.getSampleSizeInBits():targetFormat.getSampleSizeInBits();
int channels=bSetChannels?
sourceFormat.getChannels():targetFormat.getChannels();
int frameSize=getFrameSize(
targetFormat.getEncoding(),
sampleRate,
sampleSize,
channels,
frameRate,
targetFormat.isBigEndian(),
targetFormat.getFrameSize());
targetFormat= new AudioFormat(
targetFormat.getEncoding(),
sampleRate,
sampleSize,
channels,
frameSize,
frameRate,
targetFormat.isBigEndian());
}
return targetFormat;
}
/**
* Calculates the frame size for the given format description.
* The default implementation returns AudioSystem.NOT_SPECIFIED
* if either <code>sampleSize</code> or <code>channels</code> is AudioSystem.NOT_SPECIFIED,
* otherwise <code>sampleSize*channels/8</code> is returned.
* <p>
* If this does not reflect the way to calculate the right frame size,
* inheriting classes should overwrite this method if they use
* replaceNotSpecified(...). It is not used elsewhere in this class.
*/
//$$fb 2000-08-16: added
protected int getFrameSize(
AudioFormat.Encoding encoding,
float sampleRate,
int sampleSize,
int channels,
float frameRate,
boolean bigEndian,
int oldFrameSize) {
if (sampleSize==AudioSystem.NOT_SPECIFIED || channels==AudioSystem.NOT_SPECIFIED) {
return AudioSystem.NOT_SPECIFIED;
}
return sampleSize*channels/8;
}
}
/*** TSimpleFormatConversionProvider.java ***/
|