ProxyDriver.java
package sidplay.audio;
import libsidplay.common.CPUClock;
import libsidplay.common.EventScheduler;
import libsidplay.common.SIDListener;
import libsidplay.components.mos6510.IMOS6510Extension;
import libsidplay.components.mos656x.VIC;
import libsidplay.config.IAudioSection;
import javax.sound.sampled.LineUnavailableException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Optional;
/**
* Proxy driver to use two different sound or video drivers at the same time.
* <p>
* <B>Note:</B> Both drivers sample buffer must be equal in size.
*
* @author Ken Händel
*
*/
public class ProxyDriver implements AudioDriver, VideoDriver, SIDListener, IMOS6510Extension {
private final AudioDriver driverOne;
private final AudioDriver driverTwo;
/**
* Create a proxy driver
*
* @param driver1 sound driver, that buffer gets filled
* @param driver2 sound driver that gets the copied sample buffer
*/
public ProxyDriver(final AudioDriver driver1, final AudioDriver driver2) {
driverOne = driver1;
driverTwo = driver2;
}
@Override
public void setVideoTiming(double screenRefresh, int borderHeight) {
driverOne.setVideoTiming(screenRefresh, borderHeight);
driverTwo.setVideoTiming(screenRefresh, borderHeight);
}
@Override
public void open(IAudioSection audioSection, String recordingFilename, CPUClock cpuClock, EventScheduler context)
throws IOException, LineUnavailableException, InterruptedException {
driverOne.open(audioSection, recordingFilename, cpuClock, context);
driverTwo.open(audioSection, recordingFilename, cpuClock, context);
}
@Override
public void pause() {
driverOne.pause();
driverTwo.pause();
}
@Override
public void write() throws InterruptedException {
driverOne.write();
copySecond();
}
@Override
public void write(long first, long last, long offset, int frames) throws InterruptedException {
driverOne.write(first, last, offset, frames);
ByteBuffer target = driverTwo.buffer();
if (target != null) {
target.clear().put(driverOne.buffer().asReadOnlyBuffer().flip());
driverTwo.write(first, last, offset, frames);
}
}
@Override
public long getPlayedFrames() {
long first = driverOne.getPlayedFrames();
return first >= 0 ? first : driverTwo.getPlayedFrames();
}
@Override
public int getPlaybackBufferFrames() {
return Math.max(driverOne.getPlaybackBufferFrames(), driverTwo.getPlaybackBufferFrames());
}
private void copySecond() throws InterruptedException {
ByteBuffer driverTwoBuffer = driverTwo.buffer();
if (driverTwoBuffer != null) {
// Driver two's buffer gets the contents of driver one's buffer
ByteBuffer readOnlyCopy = driverOne.buffer().asReadOnlyBuffer();
readOnlyCopy.flip();
driverTwoBuffer.clear();
driverTwoBuffer.put(readOnlyCopy);
driverTwo.write();
}
}
@Override
public void accept(VIC vic) {
if (driverOne instanceof VideoDriver) {
((VideoDriver) driverOne).accept(vic);
}
if (driverTwo instanceof VideoDriver) {
((VideoDriver) driverTwo).accept(vic);
}
}
@Override
public void write(int sidNum, int addr, byte data) {
if (driverOne instanceof SIDListener) {
((SIDListener) driverOne).write(sidNum, addr, data);
}
if (driverTwo instanceof SIDListener) {
((SIDListener) driverTwo).write(sidNum, addr, data);
}
}
@Override
public void jmpJsr() {
if (driverOne instanceof IMOS6510Extension) {
((IMOS6510Extension) driverOne).jmpJsr();
}
if (driverTwo instanceof IMOS6510Extension) {
((IMOS6510Extension) driverTwo).jmpJsr();
}
}
@Override
public void close() {
driverOne.close();
driverTwo.close();
}
/**
* Driver one's buffer gets filled, while driver two gets a copy in method write()
*/
@Override
public ByteBuffer buffer() {
return driverOne.buffer();
}
@Override
public boolean isRecording() {
return driverOne.isRecording() || driverTwo.isRecording();
}
@Override
public boolean requiresPlayAddress() {
return driverOne.requiresPlayAddress() || driverTwo.requiresPlayAddress();
}
@Override
public String getExtension() {
return driverOne.getExtension() != null ? driverOne.getExtension() : driverTwo.getExtension();
}
@Override
public <T extends AudioDriver> Optional<T> lookup(Class<T> clz) {
if (clz.isInstance(driverOne)) {
return Optional.of(clz.cast(driverOne));
} else if (clz.isInstance(driverTwo)) {
return Optional.of(clz.cast(driverTwo));
}
return Optional.empty();
}
}