Clip implemention for the SoftMixingMixer.
Method from com.sun.media.sound.SoftMixingClip Detail: |
public int available() {
return 0;
}
|
public void close() {
LineEvent event = null;
synchronized (control_mutex) {
if (!isOpen())
return;
stop();
event = new LineEvent(this, LineEvent.Type.CLOSE,
getLongFramePosition());
open = false;
mixer.getMainMixer().closeLine(this);
}
if (event != null)
sendEvent(event);
}
|
public void drain() {
}
|
public void flush() {
}
|
public int getBufferSize() {
return bufferSize;
}
|
public AudioFormat getFormat() {
return format;
}
|
public int getFrameLength() {
return bufferSize / format.getFrameSize();
}
|
public int getFramePosition() {
synchronized (control_mutex) {
return frameposition;
}
}
|
public float getLevel() {
return AudioSystem.NOT_SPECIFIED;
}
|
public long getLongFramePosition() {
return getFramePosition();
}
|
public long getMicrosecondLength() {
return (long) (getFrameLength() * (1000000.0 / (double) getFormat()
.getSampleRate()));
}
|
public long getMicrosecondPosition() {
return (long) (getFramePosition() * (1000000.0 / (double) getFormat()
.getSampleRate()));
}
|
public boolean isActive() {
synchronized (control_mutex) {
return active;
}
}
|
public boolean isOpen() {
return open;
}
|
public boolean isRunning() {
synchronized (control_mutex) {
return active;
}
}
|
public void loop(int count) {
LineEvent event = null;
synchronized (control_mutex) {
if (isOpen()) {
if (active)
return;
active = true;
active_sg = true;
loopcount = count;
event = new LineEvent(this, LineEvent.Type.START,
getLongFramePosition());
}
}
if (event != null)
sendEvent(event);
}
|
public void open() throws LineUnavailableException {
if (data == null) {
throw new IllegalArgumentException(
"Illegal call to open() in interface Clip");
}
open(format, data, offset, bufferSize);
}
|
public void open(AudioInputStream stream) throws LineUnavailableException, IOException {
if (isOpen()) {
throw new IllegalStateException("Clip is already open with format "
+ getFormat() + " and frame lengh of " + getFrameLength());
}
if (AudioFloatConverter.getConverter(stream.getFormat()) == null)
throw new IllegalArgumentException("Invalid format : "
+ stream.getFormat().toString());
if (stream.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
byte[] data = new byte[(int) stream.getFrameLength()
* stream.getFormat().getFrameSize()];
int readsize = 512 * stream.getFormat().getFrameSize();
int len = 0;
while (len != data.length) {
if (readsize > data.length - len)
readsize = data.length - len;
int ret = stream.read(data, len, readsize);
if (ret == -1)
break;
if (ret == 0)
Thread.yield();
len += ret;
}
open(stream.getFormat(), data, 0, len);
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[512 * stream.getFormat().getFrameSize()];
int r = 0;
while ((r = stream.read(b)) != -1) {
if (r == 0)
Thread.yield();
baos.write(b, 0, r);
}
open(stream.getFormat(), baos.toByteArray(), 0, baos.size());
}
}
|
public void open(AudioFormat format,
byte[] data,
int offset,
int bufferSize) throws LineUnavailableException {
synchronized (control_mutex) {
if (isOpen()) {
throw new IllegalStateException(
"Clip is already open with format " + getFormat()
+ " and frame lengh of " + getFrameLength());
}
if (AudioFloatConverter.getConverter(format) == null)
throw new IllegalArgumentException("Invalid format : "
+ format.toString());
if (bufferSize % format.getFrameSize() != 0)
throw new IllegalArgumentException(
"Buffer size does not represent an integral number of sample frames!");
this.data = data;
this.offset = offset;
this.bufferSize = bufferSize;
this.format = format;
this.framesize = format.getFrameSize();
loopstart = 0;
loopend = -1;
loop_sg = true;
if (!mixer.isOpen()) {
mixer.open();
mixer.implicitOpen = true;
}
outputformat = mixer.getFormat();
out_nrofchannels = outputformat.getChannels();
in_nrofchannels = format.getChannels();
open = true;
mixer.getMainMixer().openLine(this);
}
}
|
protected void processAudioLogic(SoftAudioBuffer[] buffers) {
if (_active) {
float[] left = buffers[SoftMixingMainMixer.CHANNEL_LEFT].array();
float[] right = buffers[SoftMixingMainMixer.CHANNEL_RIGHT].array();
int bufferlen = buffers[SoftMixingMainMixer.CHANNEL_LEFT].getSize();
int readlen = bufferlen * in_nrofchannels;
if (readbuffer == null || readbuffer.length < readlen) {
readbuffer = new float[readlen];
}
int ret = 0;
try {
ret = afis.read(readbuffer);
if (ret == -1) {
_active = false;
return;
}
if (ret != in_nrofchannels)
Arrays.fill(readbuffer, ret, readlen, 0);
} catch (IOException e) {
}
int in_c = in_nrofchannels;
for (int i = 0, ix = 0; i < bufferlen; i++, ix += in_c) {
left[i] += readbuffer[ix] * _leftgain;
}
if (out_nrofchannels != 1) {
if (in_nrofchannels == 1) {
for (int i = 0, ix = 0; i < bufferlen; i++, ix += in_c) {
right[i] += readbuffer[ix] * _rightgain;
}
} else {
for (int i = 0, ix = 1; i < bufferlen; i++, ix += in_c) {
right[i] += readbuffer[ix] * _rightgain;
}
}
}
if (_eff1gain > 0.0002) {
float[] eff1 = buffers[SoftMixingMainMixer.CHANNEL_EFFECT1]
.array();
for (int i = 0, ix = 0; i < bufferlen; i++, ix += in_c) {
eff1[i] += readbuffer[ix] * _eff1gain;
}
if (in_nrofchannels == 2) {
for (int i = 0, ix = 1; i < bufferlen; i++, ix += in_c) {
eff1[i] += readbuffer[ix] * _eff1gain;
}
}
}
if (_eff2gain > 0.0002) {
float[] eff2 = buffers[SoftMixingMainMixer.CHANNEL_EFFECT2]
.array();
for (int i = 0, ix = 0; i < bufferlen; i++, ix += in_c) {
eff2[i] += readbuffer[ix] * _eff2gain;
}
if (in_nrofchannels == 2) {
for (int i = 0, ix = 1; i < bufferlen; i++, ix += in_c) {
eff2[i] += readbuffer[ix] * _eff2gain;
}
}
}
}
}
|
protected void processControlLogic() {
_rightgain = rightgain;
_leftgain = leftgain;
_eff1gain = eff1gain;
_eff2gain = eff2gain;
if (active_sg) {
_active = active;
active_sg = false;
} else {
active = _active;
}
if (frameposition_sg) {
_frameposition = frameposition;
frameposition_sg = false;
afis = null;
} else {
frameposition = _frameposition;
}
if (loop_sg) {
_loopcount = loopcount;
_loopstart = loopstart;
_loopend = loopend;
}
if (afis == null) {
afis = AudioFloatInputStream.getInputStream(new AudioInputStream(
datastream, format, AudioSystem.NOT_SPECIFIED));
if (Math.abs(format.getSampleRate() - outputformat.getSampleRate()) > 0.000001)
afis = new AudioFloatInputStreamResampler(afis, outputformat);
}
}
|
public void setFramePosition(int frames) {
synchronized (control_mutex) {
frameposition_sg = true;
frameposition = frames;
}
}
|
public void setLoopPoints(int start,
int end) {
synchronized (control_mutex) {
if (end != -1) {
if (end < start)
throw new IllegalArgumentException("Invalid loop points : "
+ start + " - " + end);
if (end * framesize > bufferSize)
throw new IllegalArgumentException("Invalid loop points : "
+ start + " - " + end);
}
if (start * framesize > bufferSize)
throw new IllegalArgumentException("Invalid loop points : "
+ start + " - " + end);
if (0 < start)
throw new IllegalArgumentException("Invalid loop points : "
+ start + " - " + end);
loopstart = start;
loopend = end;
loop_sg = true;
}
}
|
public void setMicrosecondPosition(long microseconds) {
setFramePosition((int) (microseconds * (((double) getFormat()
.getSampleRate()) / 1000000.0)));
}
|
public void start() {
LineEvent event = null;
synchronized (control_mutex) {
if (isOpen()) {
if (active)
return;
active = true;
active_sg = true;
loopcount = 0;
event = new LineEvent(this, LineEvent.Type.START,
getLongFramePosition());
}
}
if (event != null)
sendEvent(event);
}
|
public void stop() {
LineEvent event = null;
synchronized (control_mutex) {
if (isOpen()) {
if (!active)
return;
active = false;
active_sg = true;
event = new LineEvent(this, LineEvent.Type.STOP,
getLongFramePosition());
}
}
if (event != null)
sendEvent(event);
}
|