Oscilloscope.java
package client.teavm.common.oscilloscope;
import builder.resid.SIDMixer;
import client.teavm.common.IImportedApi;
import libsidplay.C64;
import libsidplay.common.SIDEmu;
import libsidutils.oscilloscope.OplOscilloscope;
import libsidutils.oscilloscope.OscilloscopeService;
import libsidutils.oscilloscope.OscilloscopeUtil;
import libsidutils.oscilloscope.SnapshotQueue;
import java.nio.ByteBuffer;
import java.util.Arrays;
/**
* Publishes playback-aligned numeric SID/OPL snapshots to the browser. Sampling belongs exclusively to C64's shared
* service.
*/
public final class Oscilloscope {
private final IImportedApi importedApi;
private final OscilloscopeUtil subscription;
private final SIDMixer softwareMixer;
private final SnapshotQueue snapshots;
private final ByteBuffer[] batchBuffers = new ByteBuffer[3];
private final int width;
private int detailMask = 1, generation;
private boolean active;
public Oscilloscope(IImportedApi importedApi, C64 c64, SIDMixer mixer, int width) {
this.importedApi = importedApi;
this.softwareMixer = mixer;
snapshots = new SnapshotQueue(mixer.getScopeCapacity());
this.width = Math.clamp(width, 1, 300);
subscription = new OscilloscopeUtil(c64) {
@Override
protected int getFastForwardBitMask() {
return mixer.getFastForwardBitMask();
}
@Override
protected void updateGauges(long time, Integer chip, SIDEmu sid) {
}
@Override
protected void updateSources(int type, int generation) {
Oscilloscope.this.generation = generation;
snapshots.clear();
Arrays.fill(batchBuffers, null);
importedApi.processOscSources(type, generation);
}
@Override
protected void updateFrame(long time, OscilloscopeService source, int generation, int sequence) {
if (!softwareMixer.isWarp()) {
snapshots.capture(source, time, generation, sequence, Oscilloscope.this.width, detailMask);
}
}
};
}
public void setDetailMask(int mask) {
detailMask = mask & OplOscilloscope.ALL_CHANNELS;
}
public void start() {
active = true;
subscription.start();
}
public void stop() {
active = false;
subscription.stop();
snapshots.clear();
Arrays.fill(batchBuffers, null);
}
/**
* Publishes snapshots only through submitted audio. A busy three-slot transport leaves them in the bounded producer
* queue; receipt/acknowledgement retries publication without making drawing block the emulator.
*/
public void flush() {
if (!active) {
return;
}
long through = softwareMixer.getSubmittedCycle();
int bytes = snapshots.batchBytes(through);
if (bytes == 16) {
return;
}
int slot = importedApi.prepareOscBatch();
if (slot < 0) {
return;
}
if (batchBuffers[slot] == null || batchBuffers[slot].capacity() < bytes) {
batchBuffers[slot] = importedApi.initOscBatchBuffer(bytes, slot);
}
snapshots.writeBatch(batchBuffers[slot], through, generation);
importedApi.processOscBatch(batchBuffers[slot], slot);
}
}