829N/A/*
6321N/A * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
829N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
829N/A *
829N/A * This code is free software; you can redistribute it and/or modify it
829N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
829N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
829N/A *
829N/A * This code is distributed in the hope that it will be useful, but WITHOUT
829N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
829N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
829N/A * version 2 for more details (a copy is included in the LICENSE file that
829N/A * accompanied this code).
829N/A *
829N/A * You should have received a copy of the GNU General Public License version
829N/A * 2 along with this work; if not, write to the Free Software Foundation,
829N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
829N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
829N/A */
829N/Apackage com.sun.media.sound;
829N/A
829N/Aimport java.io.IOException;
829N/Aimport java.io.InputStream;
829N/Aimport java.util.Arrays;
829N/A
829N/Aimport javax.sound.sampled.AudioFormat;
829N/Aimport javax.sound.sampled.AudioInputStream;
829N/Aimport javax.sound.sampled.AudioSystem;
829N/Aimport javax.sound.sampled.DataLine;
829N/Aimport javax.sound.sampled.LineEvent;
829N/Aimport javax.sound.sampled.LineUnavailableException;
829N/Aimport javax.sound.sampled.SourceDataLine;
829N/A
829N/A/**
829N/A * SourceDataLine implemention for the SoftMixingMixer.
829N/A *
829N/A * @author Karl Helgason
829N/A */
6321N/Apublic final class SoftMixingSourceDataLine extends SoftMixingDataLine
6321N/A implements SourceDataLine {
829N/A
829N/A private boolean open = false;
829N/A
829N/A private AudioFormat format = new AudioFormat(44100.0f, 16, 2, true, false);
829N/A
829N/A private int framesize;
829N/A
829N/A private int bufferSize = -1;
829N/A
829N/A private float[] readbuffer;
829N/A
829N/A private boolean active = false;
829N/A
829N/A private byte[] cycling_buffer;
829N/A
829N/A private int cycling_read_pos = 0;
829N/A
829N/A private int cycling_write_pos = 0;
829N/A
829N/A private int cycling_avail = 0;
829N/A
829N/A private long cycling_framepos = 0;
829N/A
829N/A private AudioFloatInputStream afis;
829N/A
829N/A private static class NonBlockingFloatInputStream extends
829N/A AudioFloatInputStream {
829N/A AudioFloatInputStream ais;
829N/A
6321N/A NonBlockingFloatInputStream(AudioFloatInputStream ais) {
829N/A this.ais = ais;
829N/A }
829N/A
829N/A public int available() throws IOException {
829N/A return ais.available();
829N/A }
829N/A
829N/A public void close() throws IOException {
829N/A ais.close();
829N/A }
829N/A
829N/A public AudioFormat getFormat() {
829N/A return ais.getFormat();
829N/A }
829N/A
829N/A public long getFrameLength() {
829N/A return ais.getFrameLength();
829N/A }
829N/A
829N/A public void mark(int readlimit) {
829N/A ais.mark(readlimit);
829N/A }
829N/A
829N/A public boolean markSupported() {
829N/A return ais.markSupported();
829N/A }
829N/A
829N/A public int read(float[] b, int off, int len) throws IOException {
829N/A int avail = available();
829N/A if (len > avail) {
829N/A int ret = ais.read(b, off, avail);
829N/A Arrays.fill(b, off + ret, off + len, 0);
829N/A return len;
829N/A }
829N/A return ais.read(b, off, len);
829N/A }
829N/A
829N/A public void reset() throws IOException {
829N/A ais.reset();
829N/A }
829N/A
829N/A public long skip(long len) throws IOException {
829N/A return ais.skip(len);
829N/A }
829N/A
829N/A }
829N/A
6321N/A SoftMixingSourceDataLine(SoftMixingMixer mixer, DataLine.Info info) {
829N/A super(mixer, info);
829N/A }
829N/A
829N/A public int write(byte[] b, int off, int len) {
829N/A if (!isOpen())
829N/A return 0;
829N/A if (len % framesize != 0)
829N/A throw new IllegalArgumentException(
829N/A "Number of bytes does not represent an integral number of sample frames.");
4742N/A if (off < 0) {
4742N/A throw new ArrayIndexOutOfBoundsException(off);
4742N/A }
4742N/A if ((long)off + (long)len > (long)b.length) {
4742N/A throw new ArrayIndexOutOfBoundsException(b.length);
4742N/A }
829N/A
829N/A byte[] buff = cycling_buffer;
829N/A int buff_len = cycling_buffer.length;
829N/A
829N/A int l = 0;
829N/A while (l != len) {
829N/A int avail;
829N/A synchronized (cycling_buffer) {
829N/A int pos = cycling_write_pos;
829N/A avail = cycling_avail;
829N/A while (l != len) {
829N/A if (avail == buff_len)
829N/A break;
829N/A buff[pos++] = b[off++];
829N/A l++;
829N/A avail++;
829N/A if (pos == buff_len)
829N/A pos = 0;
829N/A }
829N/A cycling_avail = avail;
829N/A cycling_write_pos = pos;
829N/A if (l == len)
829N/A return l;
829N/A }
829N/A if (avail == buff_len) {
829N/A try {
829N/A Thread.sleep(1);
829N/A } catch (InterruptedException e) {
829N/A return l;
829N/A }
829N/A if (!isRunning())
829N/A return l;
829N/A }
829N/A }
829N/A
829N/A return l;
829N/A }
829N/A
829N/A //
829N/A // BooleanControl.Type.APPLY_REVERB
829N/A // BooleanControl.Type.MUTE
829N/A // EnumControl.Type.REVERB
829N/A //
829N/A // FloatControl.Type.SAMPLE_RATE
829N/A // FloatControl.Type.REVERB_SEND
829N/A // FloatControl.Type.VOLUME
829N/A // FloatControl.Type.PAN
829N/A // FloatControl.Type.MASTER_GAIN
829N/A // FloatControl.Type.BALANCE
829N/A
829N/A private boolean _active = false;
829N/A
829N/A private AudioFormat outputformat;
829N/A
829N/A private int out_nrofchannels;
829N/A
829N/A private int in_nrofchannels;
829N/A
829N/A private float _rightgain;
829N/A
829N/A private float _leftgain;
829N/A
829N/A private float _eff1gain;
829N/A
829N/A private float _eff2gain;
829N/A
829N/A protected void processControlLogic() {
829N/A _active = active;
829N/A _rightgain = rightgain;
829N/A _leftgain = leftgain;
829N/A _eff1gain = eff1gain;
829N/A _eff2gain = eff2gain;
829N/A }
829N/A
829N/A protected void processAudioLogic(SoftAudioBuffer[] buffers) {
829N/A if (_active) {
829N/A float[] left = buffers[SoftMixingMainMixer.CHANNEL_LEFT].array();
829N/A float[] right = buffers[SoftMixingMainMixer.CHANNEL_RIGHT].array();
829N/A int bufferlen = buffers[SoftMixingMainMixer.CHANNEL_LEFT].getSize();
829N/A
829N/A int readlen = bufferlen * in_nrofchannels;
829N/A if (readbuffer == null || readbuffer.length < readlen) {
829N/A readbuffer = new float[readlen];
829N/A }
829N/A int ret = 0;
829N/A try {
829N/A ret = afis.read(readbuffer);
829N/A if (ret != in_nrofchannels)
829N/A Arrays.fill(readbuffer, ret, readlen, 0);
829N/A } catch (IOException e) {
829N/A }
829N/A
829N/A int in_c = in_nrofchannels;
829N/A for (int i = 0, ix = 0; i < bufferlen; i++, ix += in_c) {
829N/A left[i] += readbuffer[ix] * _leftgain;
829N/A }
829N/A if (out_nrofchannels != 1) {
829N/A if (in_nrofchannels == 1) {
829N/A for (int i = 0, ix = 0; i < bufferlen; i++, ix += in_c) {
829N/A right[i] += readbuffer[ix] * _rightgain;
829N/A }
829N/A } else {
829N/A for (int i = 0, ix = 1; i < bufferlen; i++, ix += in_c) {
829N/A right[i] += readbuffer[ix] * _rightgain;
829N/A }
829N/A }
829N/A
829N/A }
829N/A
829N/A if (_eff1gain > 0.0001) {
829N/A float[] eff1 = buffers[SoftMixingMainMixer.CHANNEL_EFFECT1]
829N/A .array();
829N/A for (int i = 0, ix = 0; i < bufferlen; i++, ix += in_c) {
829N/A eff1[i] += readbuffer[ix] * _eff1gain;
829N/A }
829N/A if (in_nrofchannels == 2) {
829N/A for (int i = 0, ix = 1; i < bufferlen; i++, ix += in_c) {
829N/A eff1[i] += readbuffer[ix] * _eff1gain;
829N/A }
829N/A }
829N/A }
829N/A
829N/A if (_eff2gain > 0.0001) {
829N/A float[] eff2 = buffers[SoftMixingMainMixer.CHANNEL_EFFECT2]
829N/A .array();
829N/A for (int i = 0, ix = 0; i < bufferlen; i++, ix += in_c) {
829N/A eff2[i] += readbuffer[ix] * _eff2gain;
829N/A }
829N/A if (in_nrofchannels == 2) {
829N/A for (int i = 0, ix = 1; i < bufferlen; i++, ix += in_c) {
829N/A eff2[i] += readbuffer[ix] * _eff2gain;
829N/A }
829N/A }
829N/A }
829N/A
829N/A }
829N/A }
829N/A
829N/A public void open() throws LineUnavailableException {
829N/A open(format);
829N/A }
829N/A
829N/A public void open(AudioFormat format) throws LineUnavailableException {
829N/A if (bufferSize == -1)
829N/A bufferSize = ((int) (format.getFrameRate() / 2))
829N/A * format.getFrameSize();
829N/A open(format, bufferSize);
829N/A }
829N/A
829N/A public void open(AudioFormat format, int bufferSize)
829N/A throws LineUnavailableException {
829N/A
829N/A LineEvent event = null;
829N/A
829N/A if (bufferSize < format.getFrameSize() * 32)
829N/A bufferSize = format.getFrameSize() * 32;
829N/A
829N/A synchronized (control_mutex) {
829N/A
829N/A if (!isOpen()) {
829N/A if (!mixer.isOpen()) {
829N/A mixer.open();
829N/A mixer.implicitOpen = true;
829N/A }
829N/A
829N/A event = new LineEvent(this, LineEvent.Type.OPEN, 0);
829N/A
829N/A this.bufferSize = bufferSize - bufferSize
829N/A % format.getFrameSize();
829N/A this.format = format;
829N/A this.framesize = format.getFrameSize();
829N/A this.outputformat = mixer.getFormat();
829N/A out_nrofchannels = outputformat.getChannels();
829N/A in_nrofchannels = format.getChannels();
829N/A
829N/A open = true;
829N/A
829N/A mixer.getMainMixer().openLine(this);
829N/A
829N/A cycling_buffer = new byte[framesize * bufferSize];
829N/A cycling_read_pos = 0;
829N/A cycling_write_pos = 0;
829N/A cycling_avail = 0;
829N/A cycling_framepos = 0;
829N/A
829N/A InputStream cycling_inputstream = new InputStream() {
829N/A
829N/A public int read() throws IOException {
829N/A byte[] b = new byte[1];
829N/A int ret = read(b);
829N/A if (ret < 0)
829N/A return ret;
829N/A return b[0] & 0xFF;
829N/A }
829N/A
829N/A public int available() throws IOException {
829N/A synchronized (cycling_buffer) {
829N/A return cycling_avail;
829N/A }
829N/A }
829N/A
829N/A public int read(byte[] b, int off, int len)
829N/A throws IOException {
829N/A
829N/A synchronized (cycling_buffer) {
829N/A if (len > cycling_avail)
829N/A len = cycling_avail;
829N/A int pos = cycling_read_pos;
829N/A byte[] buff = cycling_buffer;
829N/A int buff_len = buff.length;
829N/A for (int i = 0; i < len; i++) {
829N/A b[off++] = buff[pos];
829N/A pos++;
829N/A if (pos == buff_len)
829N/A pos = 0;
829N/A }
829N/A cycling_read_pos = pos;
829N/A cycling_avail -= len;
829N/A cycling_framepos += len / framesize;
829N/A }
829N/A return len;
829N/A }
829N/A
829N/A };
829N/A
829N/A afis = AudioFloatInputStream
829N/A .getInputStream(new AudioInputStream(
829N/A cycling_inputstream, format,
829N/A AudioSystem.NOT_SPECIFIED));
829N/A afis = new NonBlockingFloatInputStream(afis);
829N/A
829N/A if (Math.abs(format.getSampleRate()
829N/A - outputformat.getSampleRate()) > 0.000001)
829N/A afis = new AudioFloatInputStreamResampler(afis,
829N/A outputformat);
829N/A
829N/A } else {
829N/A if (!format.matches(getFormat())) {
829N/A throw new IllegalStateException(
829N/A "Line is already open with format " + getFormat()
829N/A + " and bufferSize " + getBufferSize());
829N/A }
829N/A }
829N/A
829N/A }
829N/A
829N/A if (event != null)
829N/A sendEvent(event);
829N/A
829N/A }
829N/A
829N/A public int available() {
829N/A synchronized (cycling_buffer) {
829N/A return cycling_buffer.length - cycling_avail;
829N/A }
829N/A }
829N/A
829N/A public void drain() {
829N/A while (true) {
829N/A int avail;
829N/A synchronized (cycling_buffer) {
829N/A avail = cycling_avail;
829N/A }
829N/A if (avail != 0)
829N/A return;
829N/A try {
829N/A Thread.sleep(1);
829N/A } catch (InterruptedException e) {
829N/A return;
829N/A }
829N/A }
829N/A }
829N/A
829N/A public void flush() {
829N/A synchronized (cycling_buffer) {
829N/A cycling_read_pos = 0;
829N/A cycling_write_pos = 0;
829N/A cycling_avail = 0;
829N/A }
829N/A }
829N/A
829N/A public int getBufferSize() {
829N/A synchronized (control_mutex) {
829N/A return bufferSize;
829N/A }
829N/A }
829N/A
829N/A public AudioFormat getFormat() {
829N/A synchronized (control_mutex) {
829N/A return format;
829N/A }
829N/A }
829N/A
829N/A public int getFramePosition() {
829N/A return (int) getLongFramePosition();
829N/A }
829N/A
829N/A public float getLevel() {
829N/A return AudioSystem.NOT_SPECIFIED;
829N/A }
829N/A
829N/A public long getLongFramePosition() {
829N/A synchronized (cycling_buffer) {
829N/A return cycling_framepos;
829N/A }
829N/A }
829N/A
829N/A public long getMicrosecondPosition() {
829N/A return (long) (getLongFramePosition() * (1000000.0 / (double) getFormat()
829N/A .getSampleRate()));
829N/A }
829N/A
829N/A public boolean isActive() {
829N/A synchronized (control_mutex) {
829N/A return active;
829N/A }
829N/A }
829N/A
829N/A public boolean isRunning() {
829N/A synchronized (control_mutex) {
829N/A return active;
829N/A }
829N/A }
829N/A
829N/A public void start() {
829N/A
829N/A LineEvent event = null;
829N/A
829N/A synchronized (control_mutex) {
829N/A if (isOpen()) {
829N/A if (active)
829N/A return;
829N/A active = true;
829N/A event = new LineEvent(this, LineEvent.Type.START,
829N/A getLongFramePosition());
829N/A }
829N/A }
829N/A
829N/A if (event != null)
829N/A sendEvent(event);
829N/A }
829N/A
829N/A public void stop() {
829N/A LineEvent event = null;
829N/A
829N/A synchronized (control_mutex) {
829N/A if (isOpen()) {
829N/A if (!active)
829N/A return;
829N/A active = false;
829N/A event = new LineEvent(this, LineEvent.Type.STOP,
829N/A getLongFramePosition());
829N/A }
829N/A }
829N/A
829N/A if (event != null)
829N/A sendEvent(event);
829N/A }
829N/A
829N/A public void close() {
829N/A
829N/A LineEvent event = null;
829N/A
829N/A synchronized (control_mutex) {
829N/A if (!isOpen())
829N/A return;
829N/A stop();
829N/A
829N/A event = new LineEvent(this, LineEvent.Type.CLOSE,
829N/A getLongFramePosition());
829N/A
829N/A open = false;
829N/A mixer.getMainMixer().closeLine(this);
829N/A }
829N/A
829N/A if (event != null)
829N/A sendEvent(event);
829N/A
829N/A }
829N/A
829N/A public boolean isOpen() {
829N/A synchronized (control_mutex) {
829N/A return open;
829N/A }
829N/A }
829N/A
829N/A}