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.ArrayList;
829N/Aimport java.util.List;
829N/A
829N/Aimport javax.sound.sampled.AudioInputStream;
829N/Aimport javax.sound.sampled.AudioSystem;
829N/A
829N/A/**
829N/A * Main mixer for SoftMixingMixer.
829N/A *
829N/A * @author Karl Helgason
829N/A */
6321N/Apublic final class SoftMixingMainMixer {
829N/A
829N/A public final static int CHANNEL_LEFT = 0;
829N/A
829N/A public final static int CHANNEL_RIGHT = 1;
829N/A
829N/A public final static int CHANNEL_EFFECT1 = 2;
829N/A
829N/A public final static int CHANNEL_EFFECT2 = 3;
829N/A
829N/A public final static int CHANNEL_EFFECT3 = 4;
829N/A
829N/A public final static int CHANNEL_EFFECT4 = 5;
829N/A
829N/A public final static int CHANNEL_LEFT_DRY = 10;
829N/A
829N/A public final static int CHANNEL_RIGHT_DRY = 11;
829N/A
829N/A public final static int CHANNEL_SCRATCH1 = 12;
829N/A
829N/A public final static int CHANNEL_SCRATCH2 = 13;
829N/A
829N/A public final static int CHANNEL_CHANNELMIXER_LEFT = 14;
829N/A
829N/A public final static int CHANNEL_CHANNELMIXER_RIGHT = 15;
829N/A
6321N/A private final SoftMixingMixer mixer;
829N/A
6321N/A private final AudioInputStream ais;
829N/A
6321N/A private final SoftAudioBuffer[] buffers;
829N/A
6321N/A private final SoftAudioProcessor reverb;
829N/A
6321N/A private final SoftAudioProcessor chorus;
829N/A
6321N/A private final SoftAudioProcessor agc;
829N/A
6321N/A private final int nrofchannels;
829N/A
6321N/A private final Object control_mutex;
829N/A
6321N/A private final List<SoftMixingDataLine> openLinesList = new ArrayList<SoftMixingDataLine>();
829N/A
829N/A private SoftMixingDataLine[] openLines = new SoftMixingDataLine[0];
829N/A
829N/A public AudioInputStream getInputStream() {
829N/A return ais;
829N/A }
829N/A
6321N/A void processAudioBuffers() {
829N/A for (int i = 0; i < buffers.length; i++) {
829N/A buffers[i].clear();
829N/A }
829N/A
829N/A SoftMixingDataLine[] openLines;
829N/A synchronized (control_mutex) {
829N/A openLines = this.openLines;
829N/A for (int i = 0; i < openLines.length; i++) {
829N/A openLines[i].processControlLogic();
829N/A }
829N/A chorus.processControlLogic();
829N/A reverb.processControlLogic();
829N/A agc.processControlLogic();
829N/A }
829N/A for (int i = 0; i < openLines.length; i++) {
829N/A openLines[i].processAudioLogic(buffers);
829N/A }
829N/A
829N/A chorus.processAudio();
829N/A reverb.processAudio();
829N/A
829N/A agc.processAudio();
829N/A
829N/A }
829N/A
829N/A public SoftMixingMainMixer(SoftMixingMixer mixer) {
829N/A this.mixer = mixer;
829N/A
829N/A nrofchannels = mixer.getFormat().getChannels();
829N/A
829N/A int buffersize = (int) (mixer.getFormat().getSampleRate() / mixer
829N/A .getControlRate());
829N/A
829N/A control_mutex = mixer.control_mutex;
829N/A buffers = new SoftAudioBuffer[16];
829N/A for (int i = 0; i < buffers.length; i++) {
829N/A buffers[i] = new SoftAudioBuffer(buffersize, mixer.getFormat());
829N/A
829N/A }
829N/A
829N/A reverb = new SoftReverb();
829N/A chorus = new SoftChorus();
829N/A agc = new SoftLimiter();
829N/A
829N/A float samplerate = mixer.getFormat().getSampleRate();
829N/A float controlrate = mixer.getControlRate();
829N/A reverb.init(samplerate, controlrate);
829N/A chorus.init(samplerate, controlrate);
829N/A agc.init(samplerate, controlrate);
829N/A
829N/A reverb.setMixMode(true);
829N/A chorus.setMixMode(true);
829N/A agc.setMixMode(false);
829N/A
829N/A chorus.setInput(0, buffers[CHANNEL_EFFECT2]);
829N/A chorus.setOutput(0, buffers[CHANNEL_LEFT]);
829N/A if (nrofchannels != 1)
829N/A chorus.setOutput(1, buffers[CHANNEL_RIGHT]);
829N/A chorus.setOutput(2, buffers[CHANNEL_EFFECT1]);
829N/A
829N/A reverb.setInput(0, buffers[CHANNEL_EFFECT1]);
829N/A reverb.setOutput(0, buffers[CHANNEL_LEFT]);
829N/A if (nrofchannels != 1)
829N/A reverb.setOutput(1, buffers[CHANNEL_RIGHT]);
829N/A
829N/A agc.setInput(0, buffers[CHANNEL_LEFT]);
829N/A if (nrofchannels != 1)
829N/A agc.setInput(1, buffers[CHANNEL_RIGHT]);
829N/A agc.setOutput(0, buffers[CHANNEL_LEFT]);
829N/A if (nrofchannels != 1)
829N/A agc.setOutput(1, buffers[CHANNEL_RIGHT]);
829N/A
829N/A InputStream in = new InputStream() {
829N/A
6321N/A private final SoftAudioBuffer[] buffers = SoftMixingMainMixer.this.buffers;
829N/A
6321N/A private final int nrofchannels = SoftMixingMainMixer.this.mixer
829N/A .getFormat().getChannels();
829N/A
6321N/A private final int buffersize = buffers[0].getSize();
829N/A
6321N/A private final byte[] bbuffer = new byte[buffersize
829N/A * (SoftMixingMainMixer.this.mixer.getFormat()
829N/A .getSampleSizeInBits() / 8) * nrofchannels];
829N/A
829N/A private int bbuffer_pos = 0;
829N/A
6321N/A private final byte[] single = new byte[1];
829N/A
829N/A public void fillBuffer() {
829N/A processAudioBuffers();
829N/A for (int i = 0; i < nrofchannels; i++)
829N/A buffers[i].get(bbuffer, i);
829N/A bbuffer_pos = 0;
829N/A }
829N/A
829N/A public int read(byte[] b, int off, int len) {
829N/A int bbuffer_len = bbuffer.length;
829N/A int offlen = off + len;
829N/A byte[] bbuffer = this.bbuffer;
829N/A while (off < offlen)
829N/A if (available() == 0)
829N/A fillBuffer();
829N/A else {
829N/A int bbuffer_pos = this.bbuffer_pos;
829N/A while (off < offlen && bbuffer_pos < bbuffer_len)
829N/A b[off++] = bbuffer[bbuffer_pos++];
829N/A this.bbuffer_pos = bbuffer_pos;
829N/A }
829N/A return len;
829N/A }
829N/A
829N/A public int read() throws IOException {
829N/A int ret = read(single);
829N/A if (ret == -1)
829N/A return -1;
829N/A return single[0] & 0xFF;
829N/A }
829N/A
829N/A public int available() {
829N/A return bbuffer.length - bbuffer_pos;
829N/A }
829N/A
829N/A public void close() {
829N/A SoftMixingMainMixer.this.mixer.close();
829N/A }
829N/A
829N/A };
829N/A
829N/A ais = new AudioInputStream(in, mixer.getFormat(),
829N/A AudioSystem.NOT_SPECIFIED);
829N/A
829N/A }
829N/A
829N/A public void openLine(SoftMixingDataLine line) {
829N/A synchronized (control_mutex) {
829N/A openLinesList.add(line);
829N/A openLines = openLinesList
829N/A .toArray(new SoftMixingDataLine[openLinesList.size()]);
829N/A }
829N/A }
829N/A
829N/A public void closeLine(SoftMixingDataLine line) {
829N/A synchronized (control_mutex) {
829N/A openLinesList.remove(line);
829N/A openLines = openLinesList
829N/A .toArray(new SoftMixingDataLine[openLinesList.size()]);
829N/A if (openLines.length == 0)
829N/A if (mixer.implicitOpen)
829N/A mixer.close();
829N/A }
829N/A
829N/A }
829N/A
829N/A public SoftMixingDataLine[] getOpenLines() {
829N/A synchronized (control_mutex) {
829N/A return openLines;
829N/A }
829N/A
829N/A }
829N/A
829N/A public void close() {
829N/A SoftMixingDataLine[] openLines = this.openLines;
829N/A for (int i = 0; i < openLines.length; i++) {
829N/A openLines[i].close();
829N/A }
829N/A }
829N/A
829N/A}