summaryrefslogtreecommitdiff
path: root/songdbj/org/tritonus/share/sampled/mixer/TClip.java
diff options
context:
space:
mode:
Diffstat (limited to 'songdbj/org/tritonus/share/sampled/mixer/TClip.java')
-rw-r--r--songdbj/org/tritonus/share/sampled/mixer/TClip.java340
1 files changed, 340 insertions, 0 deletions
diff --git a/songdbj/org/tritonus/share/sampled/mixer/TClip.java b/songdbj/org/tritonus/share/sampled/mixer/TClip.java
new file mode 100644
index 0000000000..e0a8140c37
--- /dev/null
+++ b/songdbj/org/tritonus/share/sampled/mixer/TClip.java
@@ -0,0 +1,340 @@
1/*
2 * TClip.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/*
27|<--- this code is formatted to fit into 80 columns --->|
28*/
29
30package org.tritonus.share.sampled.mixer;
31
32import java.io.IOException;
33import java.io.ByteArrayInputStream;
34import java.util.Collection;
35
36import javax.sound.sampled.AudioFormat;
37import javax.sound.sampled.AudioSystem;
38import javax.sound.sampled.Clip;
39import javax.sound.sampled.Control;
40import javax.sound.sampled.DataLine;
41import javax.sound.sampled.SourceDataLine;
42import javax.sound.sampled.AudioInputStream;
43import javax.sound.sampled.LineUnavailableException;
44import javax.sound.sampled.Mixer;
45
46import org.tritonus.share.TDebug;
47import org.tritonus.share.sampled.mixer.TDataLine;
48
49
50
51public class TClip
52extends TDataLine
53implements Clip
54{
55 private static final Class[] CONTROL_CLASSES = {/*GainControl.class*/};
56 private static final int BUFFER_FRAMES = 16384;
57
58
59 public TClip(DataLine.Info info)
60 {
61 super(null, // TMixer
62 info);
63 }
64
65
66
67 public TClip(DataLine.Info info,
68 Collection<Control> controls)
69 {
70 super(null, // TMixer
71 info,
72 controls);
73 }
74
75
76
77 public void open(AudioFormat audioFormat,
78 byte[] abData,
79 int nOffset,
80 int nLength)
81 throws LineUnavailableException
82 {
83 // int nBufferLength = nNumFrames * audioFormat.getFrameSize();
84 // TODO: check if nOffset + nBufferLength <= abData.length
85 // perhaps truncate automatically
86 ByteArrayInputStream bais = new ByteArrayInputStream(abData, nOffset, nLength);
87 AudioInputStream audioInputStream = new AudioInputStream(bais, audioFormat, AudioSystem.NOT_SPECIFIED);
88 try
89 {
90 open(audioInputStream);
91 }
92 catch (IOException e)
93 {
94 if (TDebug.TraceAllExceptions)
95 {
96 TDebug.out(e);
97 }
98 throw new LineUnavailableException("IOException occured");
99 }
100 }
101
102
103
104 public void open(AudioInputStream audioInputStream)
105 throws LineUnavailableException, IOException
106 {
107 AudioFormat audioFormat = audioInputStream.getFormat();
108 // TOOD:
109 DataLine.Info info = new DataLine.Info(Clip.class,
110 audioFormat, -1/*nBufferSize*/);
111/*
112 setLineInfo(info);
113 int nFrameSize = audioFormat.getFrameSize();
114 long lTotalLength = audioInputStream.getFrameLength() * nFrameSize;
115 int nFormat = Esd.ESD_STREAM | Esd.ESD_PLAY | EsdUtils.getEsdFormat(audioFormat);
116 if (TDebug.TraceClip)
117 {
118 TDebug.out("format: " + nFormat);
119 TDebug.out("sample rate: " + audioFormat.getSampleRate());
120 }
121 // m_esdSample.open(nFormat, (int) audioFormat.getSampleRate(), (int) lTotalLength);
122 if (TDebug.TraceClip)
123 {
124 TDebug.out("size in esd: " + audioInputStream.getFrameLength() * nFrameSize);
125 }
126 int nBufferLength = BUFFER_FRAMES * nFrameSize;
127 byte[] abData = new byte[nBufferLength];
128 int nBytesRead = 0;
129 int nTotalBytes = 0;
130 while (nBytesRead != -1)
131 {
132 try
133 {
134 nBytesRead = audioInputStream.read(abData, 0, abData.length);
135 }
136 catch (IOException e)
137 {
138 if (TDebug.TraceClip || TDebug.TraceAllExceptions)
139 {
140 TDebug.out(e);
141 }
142 }
143 if (nBytesRead >= 0)
144 {
145 nTotalBytes += nBytesRead;
146 if (TDebug.TraceClip)
147 {
148 TDebug.out("TClip.open(): total bytes: " + nTotalBytes);
149 TDebug.out("TClip.open(): Trying to write: " + nBytesRead);
150 }
151 int nBytesWritten = 0; //m_esdSample.write(abData, 0, nBytesRead);
152 if (TDebug.TraceClip)
153 {
154 TDebug.out("TClip.open(): Written: " + nBytesWritten);
155 }
156 }
157 }
158 // to trigger the events
159 // open();
160 */
161 }
162
163
164
165 public int getFrameLength()
166 {
167 // TODO:
168 return -1;
169 }
170
171
172
173 public long getMicrosecondLength()
174 {
175 // TODO:
176 return -1;
177 }
178
179
180
181 public void setFramePosition(int nPosition)
182 {
183 // TOOD:
184 }
185
186
187
188 public void setMicrosecondPosition(long lPosition)
189 {
190 // TOOD:
191 }
192
193
194
195 public int getFramePosition()
196 {
197 // TOOD:
198 return -1;
199 }
200
201
202
203 public long getMicrosecondPosition()
204 {
205 // TOOD:
206 return -1;
207 }
208
209
210
211 public void setLoopPoints(int nStart, int nEnd)
212 {
213 // TOOD:
214 }
215
216
217
218 public void loop(int nCount)
219 {
220 if (TDebug.TraceClip)
221 {
222 TDebug.out("TClip.loop(int): called; count = " + nCount);
223 }
224 if (false/*isStarted()*/)
225 {
226 /*
227 * only allow zero count to stop the looping
228 * at the end of an iteration.
229 */
230 if (nCount == 0)
231 {
232 if (TDebug.TraceClip)
233 {
234 TDebug.out("TClip.loop(int): stopping sample");
235 }
236 // m_esdSample.stop();
237 }
238 }
239 else
240 {
241 if (nCount == 0)
242 {
243 if (TDebug.TraceClip)
244 {
245 TDebug.out("TClip.loop(int): starting sample (once)");
246 }
247 // m_esdSample.play();
248 }
249 else
250 {
251 /*
252 * we're ignoring the count, because esd
253 * cannot loop for a fixed number of
254 * times.
255 */
256 // TDebug.out("hallo");
257 if (TDebug.TraceClip)
258 {
259 TDebug.out("TClip.loop(int): starting sample (forever)");
260 }
261 // m_esdSample.loop();
262 }
263 }
264 // TOOD:
265 }
266
267
268
269 public void flush()
270 {
271 // TOOD:
272 }
273
274
275
276 public void drain()
277 {
278 // TOOD:
279 }
280
281
282
283 public void close()
284 {
285 // m_esdSample.free();
286 // m_esdSample.close();
287 // TOOD:
288 }
289
290
291
292
293 public void open()
294 {
295 // TODO:
296 }
297
298
299
300 public void start()
301 {
302 if (TDebug.TraceClip)
303 {
304 TDebug.out("TClip.start(): called");
305 }
306 /*
307 * This is a hack. What start() really should do is
308 * start playing at the position playback was stopped.
309 */
310 if (TDebug.TraceClip)
311 {
312 TDebug.out("TClip.start(): calling 'loop(0)' [hack]");
313 }
314 loop(0);
315 }
316
317
318
319 public void stop()
320 {
321 // TODO:
322 // m_esdSample.kill();
323 }
324
325
326
327 /*
328 * This method is enforced by DataLine, but doesn't make any
329 * sense for Clips.
330 */
331 public int available()
332 {
333 return -1;
334 }
335}
336
337
338
339/*** TClip.java ***/
340