ExportedUtilitiesApi.java

package client.teavm.common;

import libsidplay.common.Emulation;
import libsidplay.config.IFilterSection;

import static java.lang.String.format;
import static libsidplay.common.SIDChip.FC_MAX;

/**
 * Provides a set of utility functions that can be accessed from JavaScript or
 * WebAssembly builds generated by TeaVM.
 * <p>
 * This class exposes reusable methods that are independent of the emulator
 * core, allowing web clients or external tools to perform SID-related
 * calculations without instantiating the full emulator.
 * </p>
 */
public class ExportedUtilitiesApi implements IExportedUtilities {

    private final ILogger LOG;

    public ExportedUtilitiesApi(String[] args) {
        LOG = new MainArgs(args);
    }

    /**
     * Generate a SID chip–dependent filter curve.
     * <p>
     * This function estimates the frequency response for each possible filter
     * cutoff (FC) value, depending on the selected emulation model. It calls into
     * the corresponding {@code FilterModelConfig} of either ReSID or ReSID-FP to
     * compute the analog cutoff frequency in Hz.
     * </p>
     * <p>
     * The result is typically used for visualization or DSP processing in
     * JavaScript/WebAssembly clients.
     * </p>
     *
     * @param emulation the SID emulation type (e.g. {@link Emulation#RESIDFP})
     * @param filter    the filter configuration section defining model parameters
     * @return an array of {@code FC_MAX} double values representing the estimated
     * cutoff frequency curve
     */
    @Override
    public double[] filterCurve(Emulation emulation, IFilterSection filter) {
        LOG.fine(format("filterCurve: %s, filter: %s", emulation, filter));

        double[] result = new double[FC_MAX];
        for (int fc = 0; fc < FC_MAX; fc++) {
            if (emulation == Emulation.RESIDFP_CIRCUIT) {
                result[fc] = builder.resid.residfp.circuit.FilterModelConfig.estimateFrequency(filter, fc);
            } else if (emulation == Emulation.RESIDFP) {
                result[fc] = builder.resid.residfp.FilterModelConfig.estimateFrequency(filter, fc);
            } else {
                result[fc] = builder.resid.resid.FilterModelConfig.estimateFrequency(filter, fc);
            }
        }
        return result;
    }

}