diff options
Diffstat (limited to 'songdbj/org/tritonus/share/sampled/convert/TEncodingFormatConversionProvider.java')
-rw-r--r-- | songdbj/org/tritonus/share/sampled/convert/TEncodingFormatConversionProvider.java | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/songdbj/org/tritonus/share/sampled/convert/TEncodingFormatConversionProvider.java b/songdbj/org/tritonus/share/sampled/convert/TEncodingFormatConversionProvider.java new file mode 100644 index 0000000000..6b83403c43 --- /dev/null +++ b/songdbj/org/tritonus/share/sampled/convert/TEncodingFormatConversionProvider.java | |||
@@ -0,0 +1,129 @@ | |||
1 | /* | ||
2 | * TEncodingFormatConversionProvider.java | ||
3 | * | ||
4 | * This file is part of Tritonus: http://www.tritonus.org/ | ||
5 | */ | ||
6 | |||
7 | /* | ||
8 | * Copyright (c) 2000 by Florian Bomers <http://www.bomers.de> | ||
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.convert; | ||
32 | |||
33 | import java.util.Collection; | ||
34 | import java.util.Iterator; | ||
35 | |||
36 | import javax.sound.sampled.AudioFormat; | ||
37 | import javax.sound.sampled.AudioSystem; | ||
38 | |||
39 | import org.tritonus.share.TDebug; | ||
40 | import org.tritonus.share.ArraySet; | ||
41 | |||
42 | |||
43 | // this class depends on handling of AudioSystem.NOT_SPECIFIED in AudioFormat.matches() | ||
44 | |||
45 | /** | ||
46 | * This is a base class for FormatConversionProviders that only | ||
47 | * change the encoding, i.e. they never | ||
48 | * <ul> | ||
49 | * <li> change the sample size in bits without changing the encoding | ||
50 | * <li> change the sample rate | ||
51 | * <li> change the number of channels | ||
52 | * </ul> | ||
53 | * <p>It is assumed that each source format can be encoded to all | ||
54 | * target formats. | ||
55 | * <p>In the sourceFormats and targetFormats collections that are passed to | ||
56 | * the constructor of this class, fields may be set to AudioSystem.NOT_SPECIFIED. | ||
57 | * This means that it handles all values of that field, but cannot change it. | ||
58 | * <p>This class prevents that a conversion is done (e.g. for sample rates), | ||
59 | * because the overriding class specified AudioSystem.NOT_SPECIFIED as sample rate, | ||
60 | * meaning it handles all sample rates. | ||
61 | * <p>Overriding classes must implement at least | ||
62 | * <code>AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream)</code> | ||
63 | * and provide a constructor that calls the protected constructor of this class. | ||
64 | * | ||
65 | * @author Florian Bomers | ||
66 | */ | ||
67 | public abstract class TEncodingFormatConversionProvider | ||
68 | extends TSimpleFormatConversionProvider | ||
69 | { | ||
70 | protected TEncodingFormatConversionProvider( | ||
71 | Collection<AudioFormat> sourceFormats, | ||
72 | Collection<AudioFormat> targetFormats) | ||
73 | { | ||
74 | super(sourceFormats, targetFormats); | ||
75 | } | ||
76 | |||
77 | |||
78 | |||
79 | /** | ||
80 | * This implementation assumes that the converter can convert | ||
81 | * from each of its source formats to each of its target | ||
82 | * formats. If this is not the case, the converter has to | ||
83 | * override this method. | ||
84 | * <p>When conversion is supported, for every target encoding, | ||
85 | * the fields sample size in bits, channels and sample rate are checked: | ||
86 | * <ul> | ||
87 | * <li>When a field in both the source and target format is AudioSystem.NOT_SPECIFIED, | ||
88 | * one instance of that targetFormat is returned with this field set to AudioSystem.NOT_SPECIFIED. | ||
89 | * <li>When a field in sourceFormat is set and it is AudioSystem.NOT_SPECIFIED in the target format, | ||
90 | * the value of the field of source format is set in the returned format. | ||
91 | * <li>The same applies for the other way round. | ||
92 | * </ul> | ||
93 | * For this, <code>replaceNotSpecified(sourceFormat, targetFormat)</code> in the base | ||
94 | * class TSimpleFormatConversionProvider is used - and accordingly, the frameSize | ||
95 | * is recalculated with <code>getFrameSize(...)</code> if a field with AudioSystem.NOT_SPECIFIED | ||
96 | * is replaced. Inheriting classes may wish to override this method if the | ||
97 | * default mode of calculating the frame size is not appropriate. | ||
98 | */ | ||
99 | public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) { | ||
100 | if (TDebug.TraceAudioConverter) { | ||
101 | TDebug.out(">TEncodingFormatConversionProvider.getTargetFormats(AudioFormat.Encoding, AudioFormat):"); | ||
102 | TDebug.out("checking if conversion possible"); | ||
103 | TDebug.out("from: " + sourceFormat); | ||
104 | TDebug.out("to: " + targetEncoding); | ||
105 | } | ||
106 | if (isConversionSupported(targetEncoding, sourceFormat)) { | ||
107 | // TODO: check that no duplicates may occur... | ||
108 | ArraySet<AudioFormat> result=new ArraySet<AudioFormat>(); | ||
109 | Iterator<AudioFormat> iterator = getCollectionTargetFormats().iterator(); | ||
110 | while (iterator.hasNext()) { | ||
111 | AudioFormat targetFormat = iterator.next(); | ||
112 | targetFormat=replaceNotSpecified(sourceFormat, targetFormat); | ||
113 | result.add(targetFormat); | ||
114 | } | ||
115 | if (TDebug.TraceAudioConverter) { | ||
116 | TDebug.out("< returning "+result.size()+" elements."); | ||
117 | } | ||
118 | return result.toArray(EMPTY_FORMAT_ARRAY); | ||
119 | } else { | ||
120 | if (TDebug.TraceAudioConverter) { | ||
121 | TDebug.out("< returning empty array."); | ||
122 | } | ||
123 | return EMPTY_FORMAT_ARRAY; | ||
124 | } | ||
125 | } | ||
126 | |||
127 | } | ||
128 | |||
129 | /*** TEncodingFormatConversionProvider.java ***/ | ||