SoundSetup.java

package libsidutils.sound;

import libsidplay.common.*;
import libsidplay.config.IConfig;
import libsidplay.config.IEmulationSection;
import libsidplay.config.IFilterSection;
import libsidplay.sidtune.SidTune;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Applied software-SID settings, captured only when a chip is configured. No audio, CPU or register-write hooks.
 * Curves are calculated lazily by an open view and use the same estimates as the existing filter editors.
 */
public final class SoundSetup {
    public final int chip, address;
    public final ChipModel model;
    public final Emulation emulation;
    public final CPUClock clock;
    public final String declaredModel, modelSource, declaredClock, clockSource;
    public final long appliedAt;
    private final FilterSnapshot filter;
    private double[] curve;

    private SoundSetup(IConfig config, SidTune tune, int chip, CPUClock clock, long appliedAt) {
        IEmulationSection settings = config.getEmulationSection();
        this.chip = chip;
        this.clock = clock;
        this.appliedAt = appliedAt;
        address = SidTune.getSIDAddress(settings, tune, chip);
        model = ChipModel.getChipModel(settings, tune, chip);
        emulation = Emulation.getEmulation(settings, chip);
        declaredModel = tune == SidTune.RESET ? "UNKNOWN" : tune.getInfo().getSIDModel(chip).name();
        modelSource = modelSource(settings, tune, chip);
        declaredClock = tune == SidTune.RESET ? "UNKNOWN" : tune.getInfo().getClockSpeed().name();
        clockSource = clockSource(settings, tune, clock);
        String name = settings.getFilterName(chip, Engine.EMULATION, emulation, model);
        IFilterSection selected = config.getFilterSection().stream()
                .filter(f -> f.getName().equals(name) && f.matches(emulation, model)).findFirst().orElse(null);
        filter = selected == null ? null : new FilterSnapshot(selected);
    }

    public static SoundSetup capture(IConfig config, SidTune tune, int chip, CPUClock clock, long appliedAt) {
        return new SoundSetup(config, tune, chip, clock, appliedAt);
    }

    private static String modelSource(IEmulationSection settings, SidTune tune, int chip) {
        if (settings.getForcedSidModel(chip) != ChipModel.AUTO) return "MANUAL";
        if (settings.getOverrideSection().getSidModel()[chip] != null) return "DETECTED";
        String declared = tune == SidTune.RESET ? "UNKNOWN" : tune.getInfo().getSIDModel(chip).name();
        if (declared.equals("MOS6581") || declared.equals("MOS8580")) return "FILE";
        return chip == 0 ? "DEFAULT" : "FIRST_SID";
    }

    private static String clockSource(IEmulationSection settings, SidTune tune, CPUClock active) {
        // A running desktop machine can retain its old clock until restart.
        if (CPUClock.getCPUClock(settings, tune) != active) return "RUNNING";
        if (settings.getVicModel() != VICModel.DEFAULT) return "VIC";
        if (settings.getUserClockSpeed() != CPUClock.AUTO) return "MANUAL";
        if (tune != SidTune.RESET && (tune.getInfo().getClockSpeed() == SidTune.Clock.PAL
                || tune.getInfo().getClockSpeed() == SidTune.Clock.NTSC)) return "FILE";
        return "DEFAULT";
    }

    public String filterName() { return filter == null ? "" : filter.getName(); }

    /** Taken only for requested display data, never from the CPU or sample path. */
    public static int playbackFlags(builder.resid.ReSIDBase sid) {
        if (sid instanceof builder.resid.resid.ReSID resid) {
            return SignalState.flags(resid.isFilterEnabled(), resid.getMuteMask(), resid.isDigiBoostEnabled());
        }
        if (sid instanceof builder.resid.residfp.ReSIDfp residfp) {
            return SignalState.flags(residfp.isFilterEnabled(), residfp.getMuteMask(), residfp.isDigiBoostEnabled());
        }
        return 0;
    }

    /** Inspect existing backend state, without extending the public SIDEmu/ReSIDBase contracts. */
    public static boolean filterEnabled(builder.resid.ReSIDBase sid) {
        if (sid instanceof builder.resid.resid.ReSID resid) return resid.isFilterEnabled();
        if (sid instanceof builder.resid.residfp.ReSIDfp residfp) return residfp.isFilterEnabled();
        throw new IllegalArgumentException("Unsupported software SID implementation");
    }

    /** Cached view data. Callers must not modify the returned curve. */
    public synchronized double[] curve() {
        if (filter == null) return null;
        if (curve == null) {
            curve = new double[2048];
            for (int fc = 0; fc < curve.length; fc++) {
                curve[fc] = switch (emulation) {
                    case RESID -> builder.resid.resid.FilterModelConfig.estimateFrequency(filter, fc);
                    case RESIDFP -> builder.resid.residfp.FilterModelConfig.estimateFrequency(filter, fc);
                    case RESIDFP_CIRCUIT -> builder.resid.residfp.circuit.FilterModelConfig.estimateFrequency(filter, fc);
                    default -> Double.NaN;
                };
            }
        }
        return curve;
    }

    public Map<String, Object> toMap(boolean filterEnabled) {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("chip", chip);
        result.put("address", address);
        result.put("model", model.name());
        result.put("declaredModel", declaredModel);
        result.put("modelSource", modelSource);
        result.put("clock", clock.name());
        result.put("declaredClock", declaredClock);
        result.put("clockSource", clockSource);
        result.put("emulation", emulation.name());
        result.put("filterName", filterName());
        result.put("filterEnabled", filterEnabled);
        result.put("filter", filter == null ? null : filter.toMap());
        result.put("appliedAt", appliedAt);
        return result;
    }

    public Map<String, Object> toMap(int playbackFlags) {
        Map<String, Object> result = toMap((playbackFlags & SignalState.FILTER_ENABLED) != 0);
        result.put("playbackFlags", playbackFlags);
        return result;
    }
}