summaryrefslogtreecommitdiff
path: root/songdbj/javazoom/jl/player/JavaSoundAudioDevice.java
diff options
context:
space:
mode:
Diffstat (limited to 'songdbj/javazoom/jl/player/JavaSoundAudioDevice.java')
-rw-r--r--songdbj/javazoom/jl/player/JavaSoundAudioDevice.java215
1 files changed, 0 insertions, 215 deletions
diff --git a/songdbj/javazoom/jl/player/JavaSoundAudioDevice.java b/songdbj/javazoom/jl/player/JavaSoundAudioDevice.java
deleted file mode 100644
index caa92cd6fc..0000000000
--- a/songdbj/javazoom/jl/player/JavaSoundAudioDevice.java
+++ /dev/null
@@ -1,215 +0,0 @@
1/*
2 * 11/26/04 Buffer size modified to support JRE 1.5 optimizations.
3 * (CPU usage < 1% under P4/2Ghz, RAM < 12MB).
4 * jlayer@javazoom.net
5 * 11/19/04 1.0 moved to LGPL.
6 * 06/04/01 Too fast playback fixed. mdm@techie.com
7 * 29/01/00 Initial version. mdm@techie.com
8 *-----------------------------------------------------------------------
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Library General Public License as published
11 * by the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *----------------------------------------------------------------------
23 */
24
25package javazoom.jl.player;
26
27import javax.sound.sampled.AudioFormat;
28import javax.sound.sampled.AudioSystem;
29import javax.sound.sampled.DataLine;
30import javax.sound.sampled.Line;
31import javax.sound.sampled.LineUnavailableException;
32import javax.sound.sampled.SourceDataLine;
33
34import javazoom.jl.decoder.Decoder;
35import javazoom.jl.decoder.JavaLayerException;
36
37/**
38 * The <code>JavaSoundAudioDevice</code> implements an audio
39 * device by using the JavaSound API.
40 *
41 * @since 0.0.8
42 * @author Mat McGowan
43 */
44public class JavaSoundAudioDevice extends AudioDeviceBase
45{
46 private SourceDataLine source = null;
47
48 private AudioFormat fmt = null;
49
50 private byte[] byteBuf = new byte[4096];
51
52 protected void setAudioFormat(AudioFormat fmt0)
53 {
54 fmt = fmt0;
55 }
56
57 protected AudioFormat getAudioFormat()
58 {
59 if (fmt==null)
60 {
61 Decoder decoder = getDecoder();
62 fmt = new AudioFormat(decoder.getOutputFrequency(),
63 16,
64 decoder.getOutputChannels(),
65 true,
66 false);
67 }
68 return fmt;
69 }
70
71 protected DataLine.Info getSourceLineInfo()
72 {
73 AudioFormat fmt = getAudioFormat();
74 //DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt, 4000);
75 DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt);
76 return info;
77 }
78
79 public void open(AudioFormat fmt) throws JavaLayerException
80 {
81 if (!isOpen())
82 {
83 setAudioFormat(fmt);
84 openImpl();
85 setOpen(true);
86 }
87 }
88
89 protected void openImpl()
90 throws JavaLayerException
91 {
92 }
93
94
95 // createSource fix.
96 protected void createSource() throws JavaLayerException
97 {
98 Throwable t = null;
99 try
100 {
101 Line line = AudioSystem.getLine(getSourceLineInfo());
102 if (line instanceof SourceDataLine)
103 {
104 source = (SourceDataLine)line;
105 //source.open(fmt, millisecondsToBytes(fmt, 2000));
106 source.open(fmt);
107 /*
108 if (source.isControlSupported(FloatControl.Type.MASTER_GAIN))
109 {
110 FloatControl c = (FloatControl)source.getControl(FloatControl.Type.MASTER_GAIN);
111 c.setValue(c.getMaximum());
112 }*/
113 source.start();
114
115 }
116 } catch (RuntimeException ex)
117 {
118 t = ex;
119 }
120 catch (LinkageError ex)
121 {
122 t = ex;
123 }
124 catch (LineUnavailableException ex)
125 {
126 t = ex;
127 }
128 if (source==null) throw new JavaLayerException("cannot obtain source audio line", t);
129 }
130
131 public int millisecondsToBytes(AudioFormat fmt, int time)
132 {
133 return (int)(time*(fmt.getSampleRate()*fmt.getChannels()*fmt.getSampleSizeInBits())/8000.0);
134 }
135
136 protected void closeImpl()
137 {
138 if (source!=null)
139 {
140 source.close();
141 }
142 }
143
144 protected void writeImpl(short[] samples, int offs, int len)
145 throws JavaLayerException
146 {
147 if (source==null)
148 createSource();
149
150 byte[] b = toByteArray(samples, offs, len);
151 source.write(b, 0, len*2);
152 }
153
154 protected byte[] getByteArray(int length)
155 {
156 if (byteBuf.length < length)
157 {
158 byteBuf = new byte[length+1024];
159 }
160 return byteBuf;
161 }
162
163 protected byte[] toByteArray(short[] samples, int offs, int len)
164 {
165 byte[] b = getByteArray(len*2);
166 int idx = 0;
167 short s;
168 while (len-- > 0)
169 {
170 s = samples[offs++];
171 b[idx++] = (byte)s;
172 b[idx++] = (byte)(s>>>8);
173 }
174 return b;
175 }
176
177 protected void flushImpl()
178 {
179 if (source!=null)
180 {
181 source.drain();
182 }
183 }
184
185 public int getPosition()
186 {
187 int pos = 0;
188 if (source!=null)
189 {
190 pos = (int)(source.getMicrosecondPosition()/1000);
191 }
192 return pos;
193 }
194
195 /**
196 * Runs a short test by playing a short silent sound.
197 */
198 public void test()
199 throws JavaLayerException
200 {
201 try
202 {
203 open(new AudioFormat(22050, 16, 1, true, false));
204 short[] data = new short[22050/10];
205 write(data, 0, data.length);
206 flush();
207 close();
208 }
209 catch (RuntimeException ex)
210 {
211 throw new JavaLayerException("Device test failed: "+ex);
212 }
213
214 }
215}