1 /* 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package com.sun.media.sound; 26 27 import java.io.IOException; 28 import java.io.InputStream; 29 import java.util.ArrayList; 30 import java.util.List; 31 32 import javax.sound.sampled.AudioInputStream; 33 import javax.sound.sampled.AudioSystem; 34 35 /** 36 * Main mixer for SoftMixingMixer. 37 * 38 * @author Karl Helgason 39 */ 40 public final class SoftMixingMainMixer { 41 42 public static final int CHANNEL_LEFT = 0; 43 44 public static final int CHANNEL_RIGHT = 1; 45 46 public static final int CHANNEL_EFFECT1 = 2; 47 48 public static final int CHANNEL_EFFECT2 = 3; 49 50 public static final int CHANNEL_EFFECT3 = 4; 51 52 public static final int CHANNEL_EFFECT4 = 5; 53 54 public static final int CHANNEL_LEFT_DRY = 10; 55 56 public static final int CHANNEL_RIGHT_DRY = 11; 57 58 public static final int CHANNEL_SCRATCH1 = 12; 59 60 public static final int CHANNEL_SCRATCH2 = 13; 61 62 public static final int CHANNEL_CHANNELMIXER_LEFT = 14; 63 64 public static final int CHANNEL_CHANNELMIXER_RIGHT = 15; 65 66 private final SoftMixingMixer mixer; 67 68 private final AudioInputStream ais; 69 70 private final SoftAudioBuffer[] buffers; 71 72 private final SoftAudioProcessor reverb; 73 74 private final SoftAudioProcessor chorus; 75 76 private final SoftAudioProcessor agc; 77 78 private final int nrofchannels; 79 80 private final Object control_mutex; 81 82 private final List<SoftMixingDataLine> openLinesList = new ArrayList<SoftMixingDataLine>(); 83 84 private SoftMixingDataLine[] openLines = new SoftMixingDataLine[0]; 85 86 public AudioInputStream getInputStream() { 87 return ais; 88 } 89 90 void processAudioBuffers() { 91 for (int i = 0; i < buffers.length; i++) { 92 buffers[i].clear(); 93 } 94 95 SoftMixingDataLine[] openLines; 96 synchronized (control_mutex) { 97 openLines = this.openLines; 98 for (int i = 0; i < openLines.length; i++) { 99 openLines[i].processControlLogic(); 100 } 101 chorus.processControlLogic(); 102 reverb.processControlLogic(); 103 agc.processControlLogic(); 104 } 105 for (int i = 0; i < openLines.length; i++) { 106 openLines[i].processAudioLogic(buffers); 107 } 108 109 chorus.processAudio(); 110 reverb.processAudio(); 111 112 agc.processAudio(); 113 114 } 115 116 public SoftMixingMainMixer(SoftMixingMixer mixer) { 117 this.mixer = mixer; 118 119 nrofchannels = mixer.getFormat().getChannels(); 120 121 int buffersize = (int) (mixer.getFormat().getSampleRate() / mixer 122 .getControlRate()); 123 124 control_mutex = mixer.control_mutex; 125 buffers = new SoftAudioBuffer[16]; 126 for (int i = 0; i < buffers.length; i++) { 127 buffers[i] = new SoftAudioBuffer(buffersize, mixer.getFormat()); 128 129 } 130 131 reverb = new SoftReverb(); 132 chorus = new SoftChorus(); 133 agc = new SoftLimiter(); 134 135 float samplerate = mixer.getFormat().getSampleRate(); 136 float controlrate = mixer.getControlRate(); 137 reverb.init(samplerate, controlrate); 138 chorus.init(samplerate, controlrate); 139 agc.init(samplerate, controlrate); 140 141 reverb.setMixMode(true); 142 chorus.setMixMode(true); 143 agc.setMixMode(false); 144 145 chorus.setInput(0, buffers[CHANNEL_EFFECT2]); 146 chorus.setOutput(0, buffers[CHANNEL_LEFT]); 147 if (nrofchannels != 1) 148 chorus.setOutput(1, buffers[CHANNEL_RIGHT]); 149 chorus.setOutput(2, buffers[CHANNEL_EFFECT1]); 150 151 reverb.setInput(0, buffers[CHANNEL_EFFECT1]); 152 reverb.setOutput(0, buffers[CHANNEL_LEFT]); 153 if (nrofchannels != 1) 154 reverb.setOutput(1, buffers[CHANNEL_RIGHT]); 155 156 agc.setInput(0, buffers[CHANNEL_LEFT]); 157 if (nrofchannels != 1) 158 agc.setInput(1, buffers[CHANNEL_RIGHT]); 159 agc.setOutput(0, buffers[CHANNEL_LEFT]); 160 if (nrofchannels != 1) 161 agc.setOutput(1, buffers[CHANNEL_RIGHT]); 162 163 InputStream in = new InputStream() { 164 165 private final SoftAudioBuffer[] buffers = SoftMixingMainMixer.this.buffers; 166 167 private final int nrofchannels = SoftMixingMainMixer.this.mixer 168 .getFormat().getChannels(); 169 170 private final int buffersize = buffers[0].getSize(); 171 172 private final byte[] bbuffer = new byte[buffersize 173 * (SoftMixingMainMixer.this.mixer.getFormat() 174 .getSampleSizeInBits() / 8) * nrofchannels]; 175 176 private int bbuffer_pos = 0; 177 178 private final byte[] single = new byte[1]; 179 180 public void fillBuffer() { 181 processAudioBuffers(); 182 for (int i = 0; i < nrofchannels; i++) 183 buffers[i].get(bbuffer, i); 184 bbuffer_pos = 0; 185 } 186 187 public int read(byte[] b, int off, int len) { 188 int bbuffer_len = bbuffer.length; 189 int offlen = off + len; 190 byte[] bbuffer = this.bbuffer; 191 while (off < offlen) 192 if (available() == 0) 193 fillBuffer(); 194 else { 195 int bbuffer_pos = this.bbuffer_pos; 196 while (off < offlen && bbuffer_pos < bbuffer_len) 197 b[off++] = bbuffer[bbuffer_pos++]; 198 this.bbuffer_pos = bbuffer_pos; 199 } 200 return len; 201 } 202 203 public int read() throws IOException { 204 int ret = read(single); 205 if (ret == -1) 206 return -1; 207 return single[0] & 0xFF; 208 } 209 210 public int available() { 211 return bbuffer.length - bbuffer_pos; 212 } 213 214 public void close() { 215 SoftMixingMainMixer.this.mixer.close(); 216 } 217 218 }; 219 220 ais = new AudioInputStream(in, mixer.getFormat(), 221 AudioSystem.NOT_SPECIFIED); 222 223 } 224 225 public void openLine(SoftMixingDataLine line) { 226 synchronized (control_mutex) { 227 openLinesList.add(line); 228 openLines = openLinesList 229 .toArray(new SoftMixingDataLine[openLinesList.size()]); 230 } 231 } 232 233 public void closeLine(SoftMixingDataLine line) { 234 synchronized (control_mutex) { 235 openLinesList.remove(line); 236 openLines = openLinesList 237 .toArray(new SoftMixingDataLine[openLinesList.size()]); 238 if (openLines.length == 0) 239 if (mixer.implicitOpen) 240 mixer.close(); 241 } 242 243 } 244 245 public SoftMixingDataLine[] getOpenLines() { 246 synchronized (control_mutex) { 247 return openLines; 248 } 249 250 } 251 252 public void close() { 253 SoftMixingDataLine[] openLines = this.openLines; 254 for (int i = 0; i < openLines.length; i++) { 255 openLines[i].close(); 256 } 257 } 258 259 }