package com.jsyn.unitgen;

import c1.C0406c;
import java.io.PrintStream;
import java.util.Collection;
import java.util.LinkedHashMap;
import q0.InterfaceC0696b;
import t0.C0719b;
import x0.InterfaceC0737a;

/* JADX INFO: loaded from: classes.dex */
public abstract class u {
    public static final double FALSE = 0.0d;
    public static final String PORT_NAME_AMPLITUDE = "Amplitude";
    public static final String PORT_NAME_CUTOFF = "Cutoff";
    public static final String PORT_NAME_FREQUENCY = "Frequency";
    public static final String PORT_NAME_FREQUENCY_SCALER = "FreqScaler";
    public static final String PORT_NAME_INPUT = "Input";
    public static final String PORT_NAME_OUTPUT = "Output";
    public static final String PORT_NAME_PAN = "Pan";
    public static final String PORT_NAME_PHASE = "Phase";
    public static final String PORT_NAME_PRESSURE = "Pressure";
    public static final String PORT_NAME_TIMBRE = "Timbre";
    public static final String PORT_NAME_TIME = "Time";
    public static final double TRUE = 1.0d;
    protected static final double VERY_SMALL_FLOAT = 1.0E-26d;
    private static int nextId;
    private AbstractC0420b circuit;
    private final int id;
    private long lastFrameCount;
    protected C0719b synthesisEngine;
    private final LinkedHashMap<String, x0.m> ports = new LinkedHashMap<>();
    private boolean enabled = true;

    public u() {
        int i2 = nextId;
        nextId = i2 + 1;
        this.id = i2;
    }

    public void addPort(x0.m mVar) {
        mVar.h(this);
        this.ports.put(mVar.b().toLowerCase(), mVar);
    }

    protected void autoStop() {
        this.synthesisEngine.n(getTopUnit());
    }

    public double convertHalfLifeToMultiplier(double d2) {
        if (d2 < getFramePeriod() * 2.0d) {
            return 1.0d;
        }
        return 1.0d - Math.pow(0.5d, 1.0d / (d2 * ((double) getSynthesisEngine().v())));
    }

    protected double convertTimeToRate(double d2) {
        if (d2 < this.synthesisEngine.w()) {
            return 1.0d;
        }
        return getFramePeriod() / d2;
    }

    public void flattenOutputs() {
        for (x0.m mVar : this.ports.values()) {
            if (mVar instanceof x0.l) {
                ((x0.l) mVar).s();
            }
        }
    }

    public void generate() {
        generate(0, 8);
    }

    public abstract void generate(int i2, int i3);

    public AbstractC0420b getCircuit() {
        return this.circuit;
    }

    public double getFramePeriod() {
        return this.synthesisEngine.u();
    }

    public int getFrameRate() {
        return this.synthesisEngine.v();
    }

    public int getId() {
        return this.id;
    }

    public x0.m getPortByName(String str) {
        return this.ports.get(str.toLowerCase());
    }

    public Collection<x0.m> getPorts() {
        return this.ports.values();
    }

    public C0719b getSynthesisEngine() {
        return this.synthesisEngine;
    }

    public InterfaceC0696b getSynthesizer() {
        return this.synthesisEngine;
    }

    public u getTopUnit() {
        u uVar = this;
        while (true) {
            AbstractC0420b abstractC0420b = uVar.circuit;
            if (abstractC0420b == null) {
                return uVar;
            }
            uVar = abstractC0420b;
        }
    }

    public u getUnitGenerator() {
        return this;
    }

    protected double incrementWrapPhase(double d2, double d3) {
        double d4 = d2 + d3;
        return d4 >= 1.0d ? d4 - 2.0d : d4 < -1.0d ? d4 + 2.0d : d4;
    }

    public boolean isEnabled() {
        return this.enabled;
    }

    public boolean isStartRequired() {
        return false;
    }

    public void printConnections() {
        printConnections(System.out);
    }

    public void pullData(long j2, int i2, int i3) {
        if (!this.enabled || j2 <= this.lastFrameCount) {
            return;
        }
        this.lastFrameCount = j2;
        for (Object obj : this.ports.values()) {
            if (obj instanceof InterfaceC0737a) {
                ((InterfaceC0737a) obj).a(j2, i2, i3);
            }
        }
        generate(i2, i3);
    }

    public void setCircuit(AbstractC0420b abstractC0420b) {
        if (this.circuit != null && abstractC0420b != null) {
            throw new RuntimeException("Unit is already in a circuit.");
        }
        this.circuit = abstractC0420b;
    }

    public void setEnabled(boolean z2) {
        this.enabled = z2;
        if (z2) {
            return;
        }
        flattenOutputs();
    }

    public void setFrameRate(int i2) {
    }

    public void setPort(String str, double d2, C0406c c0406c) {
        x0.k kVar = (x0.k) getPortByName(str);
        if (kVar != null) {
            kVar.w(d2, c0406c);
        }
    }

    public void setSynthesisEngine(C0719b c0719b) {
        C0719b c0719b2 = this.synthesisEngine;
        if (c0719b2 != null && c0719b2 != c0719b) {
            throw new RuntimeException("Unit synthesisEngine already set.");
        }
        this.synthesisEngine = c0719b;
    }

    public void start() {
        if (getSynthesisEngine() != null) {
            getSynthesisEngine().E(this);
            return;
        }
        throw new RuntimeException("This " + getClass().getName() + " was not add()ed to a Synthesizer.");
    }

    public void stop(double d2) {
        stop(new C0406c(d2));
    }

    public void printConnections(PrintStream printStream) {
        printConnections(printStream, 0);
    }

    public void stop() {
        getSynthesisEngine().G(this);
    }

    public void addPort(x0.m mVar, String str) {
        mVar.g(str);
        addPort(mVar);
    }

    public void printConnections(PrintStream printStream, int i2) {
        for (x0.m mVar : getPorts()) {
            if (mVar instanceof x0.k) {
                ((x0.k) mVar).u(printStream, i2);
            }
        }
    }

    public void stop(C0406c c0406c) {
        getSynthesisEngine().H(this, c0406c);
    }

    public void start(double d2) {
        start(new C0406c(d2));
    }

    public void start(C0406c c0406c) {
        if (getSynthesisEngine() != null) {
            getSynthesisEngine().F(this, c0406c);
            return;
        }
        throw new RuntimeException("This " + getClass().getName() + " was not add()ed to a Synthesizer.");
    }
}
