summaryrefslogtreecommitdiff
path: root/songdbj/org/tritonus/share/sampled/convert/TFormatConversionProvider.java
diff options
context:
space:
mode:
Diffstat (limited to 'songdbj/org/tritonus/share/sampled/convert/TFormatConversionProvider.java')
-rw-r--r--songdbj/org/tritonus/share/sampled/convert/TFormatConversionProvider.java170
1 files changed, 170 insertions, 0 deletions
diff --git a/songdbj/org/tritonus/share/sampled/convert/TFormatConversionProvider.java b/songdbj/org/tritonus/share/sampled/convert/TFormatConversionProvider.java
new file mode 100644
index 0000000000..eaec65bb06
--- /dev/null
+++ b/songdbj/org/tritonus/share/sampled/convert/TFormatConversionProvider.java
@@ -0,0 +1,170 @@
1/*
2 * TFormatConversionProvider.java
3 *
4 * This file is part of Tritonus: http://www.tritonus.org/
5 */
6
7/*
8 * Copyright (c) 1999, 2000 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
31package org.tritonus.share.sampled.convert;
32
33import javax.sound.sampled.AudioSystem;
34import javax.sound.sampled.AudioFormat;
35import javax.sound.sampled.AudioInputStream;
36import javax.sound.sampled.spi.FormatConversionProvider;
37
38import org.tritonus.share.TDebug;
39import org.tritonus.share.sampled.AudioFormats;
40
41
42
43/** Base class for all conversion providers of Tritonus.
44 *
45 * @author Matthias Pfisterer
46 */
47public abstract class TFormatConversionProvider
48extends FormatConversionProvider
49{
50 protected static final AudioFormat.Encoding[] EMPTY_ENCODING_ARRAY = new AudioFormat.Encoding[0];
51 protected static final AudioFormat[] EMPTY_FORMAT_ARRAY = new AudioFormat[0];
52
53
54
55 // $$fb2000-10-04: use AudioSystem.NOT_SPECIFIED for all fields.
56 public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream audioInputStream)
57 {
58 AudioFormat sourceFormat = audioInputStream.getFormat();
59 AudioFormat targetFormat = new AudioFormat(
60 targetEncoding,
61 AudioSystem.NOT_SPECIFIED, // sample rate
62 AudioSystem.NOT_SPECIFIED, // sample size in bits
63 AudioSystem.NOT_SPECIFIED, // channels
64 AudioSystem.NOT_SPECIFIED, // frame size
65 AudioSystem.NOT_SPECIFIED, // frame rate
66 sourceFormat.isBigEndian()); // big endian
67 if (TDebug.TraceAudioConverter)
68 {
69 TDebug.out("TFormatConversionProvider.getAudioInputStream(AudioFormat.Encoding, AudioInputStream):");
70 TDebug.out("trying to convert to " + targetFormat);
71 }
72 return getAudioInputStream(targetFormat, audioInputStream);
73 }
74
75
76
77 /**
78 * WARNING: this method uses <code>getTargetFormats(AudioFormat.Encoding, AudioFormat)</code>
79 * which may create infinite loops if the latter is overwritten.
80 * <p>
81 * This method is overwritten here to make use of org.tritonus.share.sampled.AudioFormats.matches
82 * and is considered temporary until AudioFormat.matches is corrected in the JavaSound API.
83 */
84 /* $$mp: if we decide to use getMatchingFormat(), this method should be
85 implemented by simply calling getMatchingFormat() and comparing the
86 result against null.
87 */
88 public boolean isConversionSupported(
89 AudioFormat targetFormat,
90 AudioFormat sourceFormat)
91 {
92 if (TDebug.TraceAudioConverter)
93 {
94 TDebug.out(">TFormatConversionProvider.isConversionSupported(AudioFormat, AudioFormat):");
95 TDebug.out("class: "+getClass().getName());
96 TDebug.out("checking if conversion possible");
97 TDebug.out("from: " + sourceFormat);
98 TDebug.out("to: " + targetFormat);
99 }
100 AudioFormat[] aTargetFormats = getTargetFormats(targetFormat.getEncoding(), sourceFormat);
101 for (int i = 0; i < aTargetFormats.length; i++)
102 {
103 if (TDebug.TraceAudioConverter)
104 {
105 TDebug.out("checking against possible target format: " + aTargetFormats[i]);
106 }
107 if (aTargetFormats[i] != null
108 && AudioFormats.matches(aTargetFormats[i], targetFormat))
109 {
110 if (TDebug.TraceAudioConverter)
111 {
112 TDebug.out("<result=true");
113 }
114 return true;
115 }
116 }
117 if (TDebug.TraceAudioConverter) {
118 TDebug.out("<result=false");
119 }
120 return false;
121 }
122
123
124 /**
125 * WARNING: this method uses <code>getTargetFormats(AudioFormat.Encoding, AudioFormat)</code>
126 * which may create infinite loops if the latter is overwritten.
127 * <p>
128 * This method is overwritten here to make use of org.tritonus.share.sampled.AudioFormats.matches
129 * and is considered temporary until AudioFormat.matches is corrected in the JavaSound API.
130 */
131 public AudioFormat getMatchingFormat(
132 AudioFormat targetFormat,
133 AudioFormat sourceFormat)
134 {
135 if (TDebug.TraceAudioConverter)
136 {
137 TDebug.out(">TFormatConversionProvider.isConversionSupported(AudioFormat, AudioFormat):");
138 TDebug.out("class: "+getClass().getName());
139 TDebug.out("checking if conversion possible");
140 TDebug.out("from: " + sourceFormat);
141 TDebug.out("to: " + targetFormat);
142 }
143 AudioFormat[] aTargetFormats = getTargetFormats(targetFormat.getEncoding(), sourceFormat);
144 for (int i = 0; i < aTargetFormats.length; i++)
145 {
146 if (TDebug.TraceAudioConverter)
147 {
148 TDebug.out("checking against possible target format: " + aTargetFormats[i]);
149 }
150 if (aTargetFormats[i] != null
151 && AudioFormats.matches(aTargetFormats[i], targetFormat))
152 {
153 if (TDebug.TraceAudioConverter)
154 {
155 TDebug.out("<result=true");
156 }
157 return aTargetFormats[i];
158 }
159 }
160 if (TDebug.TraceAudioConverter) {
161 TDebug.out("<result=false");
162 }
163 return null;
164 }
165
166}
167
168
169
170/*** TFormatConversionProvider.java ***/