aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/jsyn/devices/javasound/JavaSoundAudioDevice.java
blob: 75c4a8a71ea32edb7b94417e5506e588414a3beb (plain)
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
 * Copyright 2009 Phil Burk, Mobileer Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jsyn.devices.javasound;

import java.util.ArrayList;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

import com.jsyn.devices.AudioDeviceInputStream;
import com.jsyn.devices.AudioDeviceManager;
import com.jsyn.devices.AudioDeviceOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Use JavaSound to access the audio hardware.
 *
 * @author Phil Burk (C) 2009 Mobileer Inc
 */
public class JavaSoundAudioDevice implements AudioDeviceManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(JavaSoundAudioDevice.class);

    private static final int BYTES_PER_SAMPLE = 2;
    private static final boolean USE_BIG_ENDIAN = false;

    ArrayList<DeviceInfo> deviceRecords;
    private double suggestedOutputLatency = 0.040;
    private double suggestedInputLatency = 0.100;
    private int defaultInputDeviceID = -1;
    private int defaultOutputDeviceID = -1;

    public JavaSoundAudioDevice() {
        String osName = System.getProperty("os.name");
        if (osName.contains("Windows")) {
            suggestedOutputLatency = 0.08;
            LOGGER.info("JSyn: default output latency set to "
                    + ((int) (suggestedOutputLatency * 1000)) + " msec for " + osName);
        }
        deviceRecords = new ArrayList<DeviceInfo>();
        sniffAvailableMixers();
        dumpAvailableMixers();
    }

    private void dumpAvailableMixers() {
        for (DeviceInfo deviceInfo : deviceRecords) {
            LOGGER.debug("" + deviceInfo);
        }
    }

    /**
     * Build device info and determine default devices.
     */
    private void sniffAvailableMixers() {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        for (int i = 0; i < mixers.length; i++) {
            DeviceInfo deviceInfo = new DeviceInfo();

            deviceInfo.name = mixers[i].getName();
            Mixer mixer = AudioSystem.getMixer(mixers[i]);

            Line.Info[] lines = mixer.getTargetLineInfo();
            deviceInfo.maxInputs = scanMaxChannels(lines);
            // Remember first device that supports input.
            if ((defaultInputDeviceID < 0) && (deviceInfo.maxInputs > 0)) {
                defaultInputDeviceID = i;
            }

            lines = mixer.getSourceLineInfo();
            deviceInfo.maxOutputs = scanMaxChannels(lines);
            // Remember first device that supports output.
            if ((defaultOutputDeviceID < 0) && (deviceInfo.maxOutputs > 0)) {
                defaultOutputDeviceID = i;
            }

            deviceRecords.add(deviceInfo);
        }
    }

    private int scanMaxChannels(Line.Info[] lines) {
        int maxChannels = 0;
        for (Line.Info line : lines) {
            if (line instanceof DataLine.Info) {
                int numChannels = scanMaxChannels(((DataLine.Info) line));
                if (numChannels > maxChannels) {
                    maxChannels = numChannels;
                }
            }
        }
        return maxChannels;
    }

    private int scanMaxChannels(DataLine.Info info) {
        int maxChannels = 0;
        for (AudioFormat format : info.getFormats()) {
            int numChannels = format.getChannels();
            if (numChannels > maxChannels) {
                maxChannels = numChannels;
            }
        }
        return maxChannels;
    }

    static class DeviceInfo {
        String name;
        int maxInputs;
        int maxOutputs;

        @Override
        public String toString() {
            return "AudioDevice: " + name + ", max in = " + maxInputs + ", max out = " + maxOutputs;
        }
    }

    private static class JavaSoundStream {
        AudioFormat format;
        byte[] bytes;
        int frameRate;
        int deviceID;
        int samplesPerFrame;

        public JavaSoundStream(int deviceID, int frameRate, int samplesPerFrame) {
            this.deviceID = deviceID;
            this.frameRate = frameRate;
            this.samplesPerFrame = samplesPerFrame;
            format = new AudioFormat(frameRate, 16, samplesPerFrame, true, USE_BIG_ENDIAN);
        }

        Line getDataLine(DataLine.Info info) throws LineUnavailableException {
            Line dataLine;
            if (deviceID >= 0) {
                Mixer.Info[] mixers = AudioSystem.getMixerInfo();
                Mixer mixer = AudioSystem.getMixer(mixers[deviceID]);
                dataLine = mixer.getLine(info);
            } else {
                dataLine = AudioSystem.getLine(info);
            }
            return dataLine;
        }

        int calculateBufferSize(double suggestedOutputLatency) {
            int numFrames = (int) (suggestedOutputLatency * frameRate);
            return numFrames * samplesPerFrame * BYTES_PER_SAMPLE;
        }

    }

    private class JavaSoundOutputStream extends JavaSoundStream implements AudioDeviceOutputStream {
        SourceDataLine line;

        public JavaSoundOutputStream(int deviceID, int frameRate, int samplesPerFrame) {
            super(deviceID, frameRate, samplesPerFrame);
        }

        @Override
        public void start() {
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
            if (!AudioSystem.isLineSupported(info)) {
                // Handle the error.
                LOGGER.error("JavaSoundOutputStream - not supported." + format);
            } else {
                try {
                    line = (SourceDataLine) getDataLine(info);
                    int bufferSize = calculateBufferSize(suggestedOutputLatency);
                    line.open(format, bufferSize);
                    LOGGER.debug("Output buffer size = " + bufferSize + " bytes.");
                    line.start();

                } catch (Exception e) {
                    e.printStackTrace();
                    line = null;
                }
            }
        }

        /** Grossly inefficient. Call the array version instead. */
        @Override
        public void write(double value) {
            double[] buffer = new double[1];
            buffer[0] = value;
            write(buffer, 0, 1);
        }

        @Override
        public void write(double[] buffer) {
            write(buffer, 0, buffer.length);
        }

        @Override
        public void write(double[] buffer, int start, int count) {
            // Allocate byte buffer if needed.
            if ((bytes == null) || ((bytes.length * 2) < count)) {
                bytes = new byte[count * 2];
            }

            // Convert float samples to LittleEndian bytes.
            int byteIndex = 0;
            for (int i = 0; i < count; i++) {
                // Offset before casting so that we can avoid using floor().
                // Also round by adding 0.5 so that very small signals go to zero.
                double temp = (32767.0 * buffer[i + start]) + 32768.5;
                int sample = ((int) temp) - 32768;
                if (sample > Short.MAX_VALUE) {
                    sample = Short.MAX_VALUE;
                } else if (sample < Short.MIN_VALUE) {
                    sample = Short.MIN_VALUE;
                }
                bytes[byteIndex++] = (byte) sample; // little end
                bytes[byteIndex++] = (byte) (sample >> 8); // big end
            }

            line.write(bytes, 0, byteIndex);
        }

        @Override
        public void stop() {
            if (line != null) {
                line.stop();
                line.flush();
                line.close();
                line = null;
            } else {
                new RuntimeException("AudioOutput stop attempted when no line created.")
                        .printStackTrace();
            }
        }

        @Override
        public double getLatency() {
            if (line == null) {
                return 0.0;
            }
            int numBytes = line.getBufferSize();
            int numFrames = numBytes / (BYTES_PER_SAMPLE * samplesPerFrame);
            return ((double) numFrames) / frameRate;
        }

        @Override
        public void close() {
        }

    }

    private class JavaSoundInputStream extends JavaSoundStream implements AudioDeviceInputStream {
        TargetDataLine line;

        public JavaSoundInputStream(int deviceID, int frameRate, int samplesPerFrame) {
            super(deviceID, frameRate, samplesPerFrame);
        }

        @Override
        public void start() {
            DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
            if (!AudioSystem.isLineSupported(info)) {
                // Handle the error.
                LOGGER.error("JavaSoundInputStream - not supported." + format);
            } else {
                try {
                    line = (TargetDataLine) getDataLine(info);
                    int bufferSize = calculateBufferSize(suggestedInputLatency);
                    line.open(format, bufferSize);
                    LOGGER.debug("Input buffer size = " + bufferSize + " bytes.");
                    line.start();
                } catch (Exception e) {
                    e.printStackTrace();
                    line = null;
                }
            }
        }

        @Override
        public double read() {
            double[] buffer = new double[1];
            read(buffer, 0, 1);
            return buffer[0];
        }

        @Override
        public int read(double[] buffer) {
            return read(buffer, 0, buffer.length);
        }

        @Override
        public int read(double[] buffer, int start, int count) {
            // Allocate byte buffer if needed.
            if ((bytes == null) || ((bytes.length * 2) < count)) {
                bytes = new byte[count * 2];
            }
            int bytesRead = line.read(bytes, 0, bytes.length);

            // Convert BigEndian bytes to float samples
            int bi = 0;
            for (int i = 0; i < count; i++) {
                int sample = bytes[bi++] & 0x00FF; // little end
                sample = sample + (bytes[bi++] << 8); // big end
                buffer[i + start] = sample * (1.0 / 32767.0);
            }
            return bytesRead / 4;
        }

        @Override
        public void stop() {
            if (line != null) {
                line.drain();
                line.close();
            } else {
                new RuntimeException("AudioInput stop attempted when no line created.")
                        .printStackTrace();
            }
        }

        @Override
        public double getLatency() {
            if (line == null) {
                return 0.0;
            }
            int numBytes = line.getBufferSize();
            int numFrames = numBytes / (BYTES_PER_SAMPLE * samplesPerFrame);
            return ((double) numFrames) / frameRate;
        }

        @Override
        public int available() {
            return line.available() / BYTES_PER_SAMPLE;
        }

        @Override
        public void close() {
        }

    }

    @Override
    public AudioDeviceOutputStream createOutputStream(int deviceID, int frameRate,
            int samplesPerFrame) {
        return new JavaSoundOutputStream(deviceID, frameRate, samplesPerFrame);
    }

    @Override
    public AudioDeviceInputStream createInputStream(int deviceID, int frameRate, int samplesPerFrame) {
        return new JavaSoundInputStream(deviceID, frameRate, samplesPerFrame);
    }

    @Override
    public double getDefaultHighInputLatency(int deviceID) {
        return 0.300;
    }

    @Override
    public double getDefaultHighOutputLatency(int deviceID) {
        return 0.300;
    }

    @Override
    public int getDefaultInputDeviceID() {
        return defaultInputDeviceID;
    }

    @Override
    public int getDefaultOutputDeviceID() {
        return defaultOutputDeviceID;
    }

    @Override
    public double getDefaultLowInputLatency(int deviceID) {
        return 0.100;
    }

    @Override
    public double getDefaultLowOutputLatency(int deviceID) {
        return 0.100;
    }

    @Override
    public int getDeviceCount() {
        return deviceRecords.size();
    }

    @Override
    public String getDeviceName(int deviceID) {
        return deviceRecords.get(deviceID).name;
    }

    @Override
    public int getMaxInputChannels(int deviceID) {
        return deviceRecords.get(deviceID).maxInputs;
    }

    @Override
    public int getMaxOutputChannels(int deviceID) {
        return deviceRecords.get(deviceID).maxOutputs;
    }

    @Override
    public int setSuggestedOutputLatency(double latency) {
        suggestedOutputLatency = latency;
        return 0;
    }

    @Override
    public int setSuggestedInputLatency(double latency) {
        suggestedInputLatency = latency;
        return 0;
    }

    @Override
    public String getName() {
        return "JavaSound";
    }

}