AudioGeneratorThread.java
package server.netsiddev;
import builder.resid.resample.Resampler;
import builder.resid.resample.StereoResampler;
import libsidplay.common.CPUClock;
import libsidplay.common.SIDChip;
import libsidplay.common.SamplingMethod;
import libsidplay.components.cart.supported.ReSfxSoundExpander;
import sidplay.audio.AudioConfig;
import sidplay.audio.JavaSound;
import sidplay.fingerprinting.WhatsSidSupport;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.Mixer.Info;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.Arrays;
import java.util.Objects;
import java.util.Random;
import java.util.Vector;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import static server.netsiddev.NetworkSIDProtocol.*;
/**
* Audio generating thread which communicates with SIDWrite source over a BlockingQueue.
*
* @author Antti Lankila
*/
public class AudioGeneratorThread extends Thread {
/** Fractional bits in the integer mixer gain and panning coefficients. */
private static final int MIXER_FRACTIONAL_BITS = 10;
/** Mixer coefficient for unchanged amplitude. */
private static final int MIXER_UNITY_GAIN = 1 << MIXER_FRACTIONAL_BITS;
/** A tenfold amplitude increase corresponds to 20 dB. */
private static final double DB_PER_AMPLITUDE_DECADE = 20.0;
/**
* Random source for triangular dithering
*/
private static final Random RANDOM = new Random();
/**
* Current clock in the SID stream.
*/
private final AtomicLong playbackClock = new AtomicLong(0);
private final Object audioOutput = new Object();
/** FLUSH skips both queued writes and any batch the renderer already took from the queue. */
private volatile long flushGeneration;
/**
* Queue with SID writes from a client. We reserve a space assuming writes come at most one every 10 cpu clocks.
*/
private final BlockingQueue<SIDWrite> sidCommandQueue;
/**
* global setting for each 8580 if digiboost should be enabled
*/
private boolean digiBoostEnabled;
/**
* SIDs that generate output
*/
private SIDChip[] sids;
/**
* Keep clocking a SID after its first register write until it is reset.
*/
private boolean[] sidUsed;
private final ReSfxSoundExpander reSfxSoundExpander;
private volatile boolean reSfxSoundExpanderEnabled;
private volatile boolean reSfxSoundExpanderEnabledTmp;
/**
* SID resampler
*/
private StereoResampler resampler;
/**
* Currently active clocking value
*/
private CPUClock sidClocking;
/**
* Currently active sampling method
*/
private SamplingMethod sidSampling;
/**
* Current audio output frequency.
*/
private final AudioConfig audioConfig;
/**
* State of HP-TPDF.
*/
private int oldRandomValue;
private int[] sidLevel;
private final int deviceIndex;
private int[] sidPositionL;
private int[] sidPositionR;
private IntBuffer[] delayedSamples;
private final Object mixerSettings = new Object();
/** Each SID advances the envelope while it is producing samples. */
private long fadeDuration;
private long[] fadeRemaining;
private int fadeTarget;
private int[] fadeStart, fadeLevel;
private Mixer.Info mixerInfo;
private boolean deviceChanged = false;
/**
* Is an audio thread waiting?
*/
private final AtomicBoolean audioWait = new AtomicBoolean(true);
/**
* Is the audio thread requested to stop rapidly?
*/
private final AtomicBoolean quicklyDiscardAudio = new AtomicBoolean(false);
/**
* Audio output driver.
*/
private JavaSound driver = new JavaSound();
/**
* WhatsSID capture time in seconds
*/
private final int captureTime;
/**
* WhatsSID enabled?
*/
private final boolean whatsSidEnabled;
/**
* WhatsSID minimum confidence to match
*/
private final double minimumRelativeConfidence;
/**
* WhatsSID
*/
private WhatsSidSupport whatsSidSupport;
/**
* Triangularly shaped noise source for audio applications. Output of this PRNG is between ]-1, 1[.
*
* @return triangular noise sample
*/
protected int triangularDithering() {
int prevValue = oldRandomValue;
oldRandomValue = RANDOM.nextInt(2);
return oldRandomValue - prevValue;
}
public AudioGeneratorThread(AudioConfig config) {
setPriority(Thread.MAX_PRIORITY);
sidCommandQueue = new LinkedBlockingQueue<>();
audioConfig = config;
SIDDeviceSettings settings = SIDDeviceSettings.getInstance();
deviceIndex = settings.getDeviceIndex();
digiBoostEnabled = settings.getDigiBoostEnabled();
audioConfig.setAudioBufferSize(settings.getAudioBufferSize());
audioConfig.setBufferFrames(settings.getAudioBufferSize());
captureTime = settings.getWhatsSidCaptureTime();
whatsSidEnabled = settings.isWhatsSidEnable();
minimumRelativeConfidence = settings.getWhatsSidMinimumRelativeConfidence();
reSfxSoundExpander = new ReSfxSoundExpander(1, (int) CPUClock.PAL.getCpuFrequency(), () -> 0, () -> 0);
}
/** Maximum rendering participants, including the audio thread itself. */
protected int getSidRendererParallelism() {
return Runtime.getRuntime().availableProcessors();
}
void renderBatch(SIDRenderer renderer) throws InterruptedException {
renderer.renderAdaptive(sidClocking.getCpuFrequency());
}
@Override
public void run() {
try (SIDRenderer renderer = new SIDRenderer(getSidRendererParallelism())) {
synchronized (audioOutput) {
initDriver();
}
int[] outAudioBuffer = new int[SIDRenderer.BATCH_CYCLES * 2];
reSfxSoundExpander.prepare(audioConfig.getFrameRate(), SIDRenderer.BATCH_CYCLES * 2);
/* Wait for configuration/commands initially. */
synchronized (sidCommandQueue) {
while (sidCommandQueue.isEmpty() && !quicklyDiscardAudio.get()) {
sidCommandQueue.wait();
}
audioWait.set(false);
}
refreshParams();
SIDWrite pendingWrite = null;
int remainingCycles = 0;
long generation = 0;
while (!interrupted()) {
boolean discard;
SIDWrite write;
synchronized (sidCommandQueue) {
discard = generation != flushGeneration;
if (discard) {
generation = flushGeneration;
pendingWrite = null;
}
write = pendingWrite != null ? pendingWrite : sidCommandQueue.poll();
if (write != null) {
if (pendingWrite == null) {
remainingCycles = write.getCycles();
}
pendingWrite = null;
renderer.begin(sids, sidUsed);
while (true) {
int piece = Math.min(remainingCycles, SIDRenderer.BATCH_CYCLES - renderer.getCycles());
remainingCycles -= piece;
renderer.add(write, piece, remainingCycles == 0);
if (remainingCycles != 0) {
pendingWrite = write;
break;
}
if (write.isEnd() || renderer.isFull()) {
break;
}
SIDWrite next = sidCommandQueue.poll();
if (next == null) {
break;
}
write = next;
remainingCycles = next.getCycles();
}
}
}
if (discard) {
discardBufferedAudio();
}
if (write == null) {
awaitCommands(generation);
continue;
}
// Keep audioWait false while workers own their SID instances,
// even if collecting this batch emptied the network queue.
renderBatch(renderer);
if (generation != flushGeneration) {
continue;
}
Arrays.fill(outAudioBuffer, 0, renderer.getCycles() * 2, 0);
mixSidSamples(renderer, outAudioBuffer);
outputBatch(renderer, outAudioBuffer, generation);
synchronized (audioOutput) {
if (generation != flushGeneration) {
continue;
}
playbackClock.addAndGet(renderer.getCycles());
}
if (renderer.getWrite(renderer.size() - 1).isEnd()) {
/* 0-pad output, write one last time */
final ByteBuffer output = driver.buffer();
if (output.position() != 0) {
while (output.hasRemaining()) {
output.putShort((short) 0);
output.putShort((short) 0);
}
writeAudio(generation);
}
break;
}
if (deviceChanged) {
synchronized (audioOutput) {
initDriver();
deviceChanged = false;
}
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (LineUnavailableException e) {
e.printStackTrace();
} finally {
synchronized (audioOutput) {
driver.close();
}
}
}
private void awaitCommands(long generation) throws InterruptedException {
long exhaustionTime = 0;
if (!quicklyDiscardAudio.get()) {
// Submit partial PCM before waiting for the next network packet.
writeAudio(generation);
exhaustionTime = System.nanoTime()
+ TimeUnit.MILLISECONDS.toNanos(Math.max(1, driver.getRemainingPlayTime()));
}
synchronized (sidCommandQueue) {
while (generation == flushGeneration && sidCommandQueue.isEmpty()
&& !quicklyDiscardAudio.get() && System.nanoTime() < exhaustionTime) {
sidCommandQueue.wait(1);
}
if (generation != flushGeneration) {
return;
}
if (quicklyDiscardAudio.getAndSet(false)) {
driver.buffer().clear();
driver.flush();
}
if (sidCommandQueue.isEmpty()) {
driver.pause();
synchronized (audioWait) {
audioWait.set(true);
audioWait.notify();
}
sidCommandQueue.wait();
audioWait.set(false);
}
}
}
private void discardBufferedAudio() {
driver.buffer().clear();
resetResampler();
reSfxSoundExpander.start();
synchronized (delayedSamples) {
for (IntBuffer delay : delayedSamples) {
delay.clear();
while (delay.hasRemaining()) {
delay.put(0);
}
delay.flip();
}
}
}
private void mixSidSamples(SIDRenderer renderer, int[] output) {
// Only this thread touches mixer/fade/delay state. Stable chip order also
// makes serial and parallel synthesis produce identical integer mixing.
for (int sid = 0; sid < sids.length; sid++) {
int start = renderer.getActiveStart(sid);
if (start < 0) {
continue;
}
int[] samples = renderer.getSamples(sid);
synchronized (this.delayedSamples) {
synchronized (mixerSettings) {
IntBuffer delay = delayedSamples[sid];
long remaining = fadeRemaining[sid];
int envelope = fadeLevel[sid];
int gain = sidLevel[sid] * envelope >> MIXER_FRACTIONAL_BITS;
for (int i = start; i < renderer.getCycles(); i++) {
if (!delay.put(samples[i]).hasRemaining()) {
delay.flip();
}
int sample = delay.get(delay.position());
if (remaining > 0) {
// Interpolate by elapsed cycles so short fades can move by more
// than one gain step per cycle and always reach the endpoint.
envelope = fadeTarget + (int) ((fadeStart[sid] - fadeTarget) * --remaining / fadeDuration);
gain = sidLevel[sid] * envelope >> MIXER_FRACTIONAL_BITS;
}
sample = sample * gain >> MIXER_FRACTIONAL_BITS;
output[i << 1 | 0] += sample * sidPositionL[sid] >> MIXER_FRACTIONAL_BITS;
output[i << 1 | 1] += sample * sidPositionR[sid] >> MIXER_FRACTIONAL_BITS;
}
fadeRemaining[sid] = remaining;
fadeLevel[sid] = envelope;
}
}
}
}
private void outputBatch(SIDRenderer renderer, int[] outAudioBuffer, long generation) throws InterruptedException {
int offset = 0;
// Replay FM intervals/writes in their original order on the audio thread.
// Resampling and PCM submission share the exact same global timeline.
for (int event = 0; event < renderer.size() && generation == flushGeneration; event++) {
int piece = renderer.getCycles(event);
final ByteBuffer output = driver.buffer();
reSfxSoundExpanderEnabled = reSfxSoundExpanderEnabledTmp;
if (reSfxSoundExpanderEnabled && piece != 0) {
reSfxSoundExpander.clock(piece);
}
for (int i = offset; i < offset + piece; i++) {
int dithering = triangularDithering();
int valueL = outAudioBuffer[i << 1 | 0];
int valueR = outAudioBuffer[i << 1 | 1];
if (resampler.input(valueL, valueR)) {
valueL = Math.clamp(resampler.outputL() + dithering, Short.MIN_VALUE, Short.MAX_VALUE);
valueR = Math.clamp(resampler.outputR() + dithering, Short.MIN_VALUE, Short.MAX_VALUE);
output.putShort((short) valueL);
output.putShort((short) valueR);
}
if (whatsSidEnabled) {
whatsSidSupport.output(outAudioBuffer[i << 1 | 0], outAudioBuffer[i << 1 | 1]);
}
if (!output.hasRemaining()) {
writeAudio(generation);
}
}
offset += piece;
SIDWrite write = renderer.getWrite(event);
if (renderer.completesWrite(event) && write.isFmWrite()) {
reSfxSoundExpander.write(write.getRegister(), write.getValue());
}
}
}
private void writeAudio(long generation) throws InterruptedException {
synchronized (audioOutput) {
final ByteBuffer output = driver.buffer();
if (output.position() != 0) {
if (generation == flushGeneration) {
if (reSfxSoundExpanderEnabled) {
reSfxSoundExpander.process(output);
}
driver.write();
}
output.clear();
}
}
}
/**
* Reset the specified SID and sets the volume afterward.
*
* @param sidNumber The specified SID to reset.
* @param volume The volume of the specified SID after resetting it.
*/
public void reset(final int sidNumber, final byte volume) {
sidUsed[sidNumber] = false;
synchronized (mixerSettings) {
fadeRemaining[sidNumber] = 0;
fadeLevel[sidNumber] = MIXER_UNITY_GAIN;
}
if (sidNumber == 0) {
reSfxSoundExpander.reset();
}
if (sids[sidNumber] != null) {
sids[sidNumber].reset();
sids[sidNumber].write(0x18, volume);
}
}
public void reopen() {
// Fix for Linux ALSA audio systems, only
deviceChanged = true;
if (whatsSidEnabled) {
whatsSidSupport.reset();
}
}
private void initDriver() throws LineUnavailableException {
if (driver != null) {
driver.close();
}
driver = createDriver();
final Vector<AudioDevice> audioDevices = new Vector<>();
final AudioDeviceCompare cmp = new AudioDeviceCompare();
int theDeviceIndex = 0;
for (Info deviceInfo : JavaSound.getDeviceInfos()) {
final AudioDevice audioDeviceItem = new AudioDevice(theDeviceIndex, deviceInfo);
audioDevices.add(audioDeviceItem);
if (theDeviceIndex == 0) {
// the first device name is the primary device driver that can
// be translated on some systems
cmp.setPrimaryDeviceName(deviceInfo.getName());
}
if (mixerInfo == null) {
if (audioDeviceItem.getIndex() == deviceIndex) {
mixerInfo = deviceInfo;
}
} else if (Objects.equals(mixerInfo.getName(), deviceInfo.getName())) {
mixerInfo = deviceInfo;
}
theDeviceIndex++;
}
audioDevices.sort(cmp);
driver.open(audioConfig, mixerInfo);
}
/** Allow embedders to supply an audio sink without opening a sound device. */
protected JavaSound createDriver() {
return new JavaSound();
}
/**
* Mute a SID's voice.
*
* @param sidNumber The specified SID to mute the voice of.
* @param voiceNo The specific voice of the SID to mute.
* @param mute Mute/Unmute the SID voice.
*/
public void mute(final int sidNumber, final int voiceNo, final boolean mute) {
SIDChip chip = sids[sidNumber];
if (chip != null) {
synchronized (chip) {
chip.mute(voiceNo, mute);
}
}
}
/**
* Change the output device
*
* @param deviceInfo The device to use.
*/
public void changeDevice(final Mixer.Info deviceInfo) {
mixerInfo = deviceInfo;
deviceChanged = true;
}
public void setAudioBufferSize(Integer audioBufferSize) {
audioConfig.setAudioBufferSize(audioBufferSize);
audioConfig.setBufferFrames(audioBufferSize);
deviceChanged = true;
}
/**
* Set NTSC/PAL time source.
*
* @param clock The specified clock value to set.
*/
public void setClocking(CPUClock clock) {
sidClocking = clock;
reSfxSoundExpander.setCpuRate((int) clock.getCpuFrequency());
reSfxSoundExpander.reset();
refreshParams();
}
/**
* Set quality of audio output.
*
* @param samplingMethod The desired sampling method to use.
*/
public void setSampling(SamplingMethod samplingMethod) {
sidSampling = samplingMethod;
refreshParams();
}
public synchronized boolean isSfxEnabled() {
return reSfxSoundExpanderEnabledTmp;
}
public synchronized void setSfxEnable(boolean fxEnable) {
if (reSfxSoundExpanderEnabledTmp == fxEnable) {
return;
}
reSfxSoundExpander.reset();
this.reSfxSoundExpanderEnabledTmp = fxEnable;
refreshParams();
}
/**
* Update SID parameters to new settings based on a given clocking, sampling, and output frequency.
*/
private void refreshParams() {
for (SIDChip sid : sids) {
sid.setClockFrequency(sidClocking.getCpuFrequency());
}
resetResampler();
}
private void resetResampler() {
resampler = Resampler.createStereoResampler(sidClocking.getCpuFrequency(), sidSampling,
audioConfig.getFrameRate(), 20000);
}
public void setPosition(int sidNumber, int position) {
synchronized (mixerSettings) {
if (sids.length > 1) {
float leftFraction = position <= POSITION_CENTER ? 1
: (POSITION_RIGHT - position) / (float) POSITION_RIGHT;
float rightFraction = position >= POSITION_CENTER ? 1
: (position - POSITION_LEFT) / (float) -POSITION_LEFT;
sidPositionL[sidNumber] = (int) (MIXER_UNITY_GAIN * leftFraction);
sidPositionR[sidNumber] = (int) (MIXER_UNITY_GAIN * rightFraction);
} else {
sidPositionL[sidNumber] = MIXER_UNITY_GAIN;
sidPositionR[sidNumber] = MIXER_UNITY_GAIN;
}
}
}
public void setLevelAdjustment(int sid, int level) {
synchronized (mixerSettings) {
sidLevel[sid] = (int) (MIXER_UNITY_GAIN
* Math.pow(10.0, level / (LEVEL_STEPS_PER_DB * DB_PER_AMPLITUDE_DECADE)));
}
}
public void setDelay(int sid, int delay) {
synchronized (this.delayedSamples) {
int delayedSamples = (int) (sidClocking.getCpuFrequency() / MILLIS_PER_SECOND * delay);
this.delayedSamples[sid] = ByteBuffer.allocateDirect(Integer.BYTES * (delayedSamples + 1))
.order(ByteOrder.nativeOrder()).asIntBuffer().put(new int[delayedSamples + 1]);
this.delayedSamples[sid].flip();
}
}
public void setFadeIn(double fadeIn) {
startFade(fadeIn, MIXER_UNITY_GAIN);
}
public void setFadeOut(double fadeOut) {
startFade(fadeOut, 0);
}
private void startFade(double seconds, int target) {
synchronized (mixerSettings) {
fadeDuration = Math.max(0, (long) (seconds * sidClocking.getCpuFrequency()));
fadeTarget = target;
for (int sid = 0; sid < sids.length; sid++) {
if (fadeDuration == 0) {
fadeLevel[sid] = target;
} else if (target == MIXER_UNITY_GAIN) {
fadeLevel[sid] = 0;
}
fadeStart[sid] = fadeLevel[sid];
fadeRemaining[sid] = fadeDuration;
}
}
}
/**
* Acquire a command queue handle.
*
* @return command queue
*/
public BlockingQueue<SIDWrite> getSidCommandQueue() {
return sidCommandQueue;
}
public JavaSound getDriver() {
return driver;
}
/**
* Return the current clock in the SID stream.
*
* @return the clock
*/
public long getPlaybackClock() {
return playbackClock.get();
}
public long flush() {
// Serialize the acknowledgement and playback clock with PCM submission.
// Workers can finish their current render while the network discards its output.
synchronized (audioOutput) {
synchronized (sidCommandQueue) {
sidCommandQueue.clear();
flushGeneration++;
if (!audioWait.get()) {
sidCommandQueue.notify();
}
}
driver.flush();
return playbackClock.get();
}
}
/**
* Ensure that the event-handling thread is consuming events.
*/
public void ensureDraining() {
synchronized (sidCommandQueue) {
sidCommandQueue.notify();
}
}
public void ensureQuickDraining() {
synchronized (sidCommandQueue) {
quicklyDiscardAudio.getAndSet(true);
sidCommandQueue.notify();
}
}
public boolean isWaitingForCommands() {
return audioWait.get();
}
/** Wait for both queued events and in-flight audio generation before reconfiguration. */
public boolean waitUntilQueueReady(long timeout) {
boolean ready = isQueueReady();
if (!ready) {
// A short packet may be queued while the consumer still reports waiting.
// Wake it before checking quiescence, so queued old SID indices cannot
// survive into a smaller SID array or a replacement model.
ensureQuickDraining();
try {
synchronized (audioWait) {
if (!isQueueReady()) {
audioWait.wait(timeout);
}
}
} catch (InterruptedException ignored) {
}
ready = isQueueReady();
}
return ready;
}
private boolean isQueueReady() {
return audioWait.get() && sidCommandQueue.isEmpty();
}
public void setSidArray(SIDChip[] sid) {
this.sids = sid;
sidUsed = new boolean[sid.length];
sidClocking = CPUClock.PAL;
sidSampling = SamplingMethod.DECIMATE;
sidLevel = new int[sid.length];
sidPositionL = new int[sid.length];
sidPositionR = new int[sid.length];
delayedSamples = new IntBuffer[sid.length];
fadeRemaining = new long[sid.length];
fadeStart = new int[sid.length];
fadeLevel = new int[sid.length];
Arrays.fill(fadeLevel, MIXER_UNITY_GAIN);
for (int i = 0; i < sid.length; i++) {
setLevelAdjustment(i, 0);
if (sid.length > 1) {
setPosition(i, POSITION_LEFT + (POSITION_RIGHT - POSITION_LEFT) * i / (sid.length - 1));
} else {
setPosition(i, POSITION_CENTER);
}
setDelay(i, 0);
}
setDigiBoost(digiBoostEnabled);
setClocking(sidClocking);
whatsSidSupport = new WhatsSidSupport(sidClocking.getCpuFrequency(), captureTime, minimumRelativeConfidence);
}
public void setSID(int sidNumber, SIDChip sidConfig) {
sidConfig.setClockFrequency(sidClocking.getCpuFrequency());
sids[sidNumber] = sidConfig;
sidUsed[sidNumber] = false;
setDigiBoost(digiBoostEnabled);
}
/**
* Whether to enable Digiboost for all SID chips of model 8580.
*
* @param selected Whether to enable Digiboost.
*/
public void setDigiBoost(final boolean selected) {
digiBoostEnabled = selected;
for (SIDChip sidChip : sids) {
if (sidChip != null) {
synchronized (sidChip) {
sidChip.setDigiBoost(digiBoostEnabled);
}
}
}
}
public WhatsSidSupport getWhatsSidSupport() {
return whatsSidSupport;
}
}