package androidx.media3.decoder.midi;

import androidx.annotation.Nullable;
import androidx.media3.common.util.Assertions;
import androidx.media3.decoder.midi.SonivoxWaveData;
import b1.AbstractC0401a;
import c1.C0406c;
import com.jsyn.unitgen.A;
import com.jsyn.unitgen.AbstractC0420b;
import com.jsyn.unitgen.B;
import com.jsyn.unitgen.C0419a;
import com.jsyn.unitgen.c;
import com.jsyn.unitgen.g;
import com.jsyn.unitgen.j;
import com.jsyn.unitgen.u;
import com.jsyn.unitgen.y;
import com.jsyn.unitgen.z;
import r0.C0705d;
import x0.k;
import x0.l;

/* JADX INFO: loaded from: classes.dex */
final class SonivoxSynthVoice extends AbstractC0420b implements y {
    private static final double AMPLITUDE_SCALER = 10.0d;
    private static final double DEFAULT_FILTER_CUTOFF = 4000.0d;
    private static final boolean FILTER_ENABLED = false;
    private final c ampEnv;
    private final k amplitude;
    private final j amplitudeMultiplier;
    private final k cutoff;
    private final g filter;
    private final c filterEnv;
    private final k filterEnvDepth;
    private final k frequency;
    private int presetIndex;

    @Nullable
    private SonivoxWaveData.WavetableRegion region;
    private final C0705d sonivoxSample;
    private int waveOffset;
    private final z waveOscillator;
    private int waveSize;
    private final B whiteNoise;

    public SonivoxSynthVoice(short[] sArr) {
        this.sonivoxSample = new C0705d(sArr);
        j jVar = new j();
        this.amplitudeMultiplier = jVar;
        A a2 = new A();
        this.waveOscillator = a2;
        B b2 = new B();
        this.whiteNoise = b2;
        c cVar = new c();
        this.ampEnv = cVar;
        c cVar2 = new c();
        this.filterEnv = cVar2;
        k kVar = cVar2.f3233i;
        this.filterEnvDepth = kVar;
        g gVar = new g();
        this.filter = gVar;
        k kVar2 = jVar.f3286b;
        this.amplitude = kVar2;
        j jVar2 = new j();
        k kVar3 = jVar2.f3285a;
        this.frequency = kVar3;
        C0419a c0419a = new C0419a();
        k kVar4 = c0419a.f3286b;
        this.cutoff = kVar4;
        k kVar5 = jVar2.f3286b;
        j jVar3 = new j();
        add(jVar2);
        add(jVar);
        add(jVar3);
        add(a2);
        add(b2);
        add(cVar);
        add(cVar2);
        add(gVar);
        add(c0419a);
        cVar2.f3291b.r(c0419a.f3285a);
        c0419a.f3287c.r(gVar.f3281c);
        jVar2.f3287c.r(a2.f3298d);
        jVar.f3287c.r(jVar3.f3285a);
        jVar3.f3287c.r(cVar.f3233i);
        addPort(kVar2, u.PORT_NAME_AMPLITUDE);
        addPort(kVar3, u.PORT_NAME_FREQUENCY);
        addPort(kVar4, u.PORT_NAME_CUTOFF);
        addPortAlias(kVar4, u.PORT_NAME_TIMBRE);
        addPort(kVar5, u.PORT_NAME_FREQUENCY_SCALER);
        addPort(kVar, "FilterEnvDepth");
        cVar2.b(this, "Filter");
        cVar.b(this, "Amp");
        kVar3.E(a2.f3298d);
        kVar5.D(0.2d, 1.0d, 4.0d);
        kVar4.E(gVar.f3281c);
        kVar.D(-4000.0d, 2000.0d, DEFAULT_FILTER_CUTOFF);
        a2.f3279b.v(0.5d);
        cVar.a(this);
        jVar3.f3286b.D(1.0d, 1.0d, 4.0d);
        addPortAlias(jVar3.f3286b, u.PORT_NAME_PRESSURE);
        usePreset(0);
    }

    private static void applyToEnvelope(SonivoxWaveData.Envelope envelope, c cVar, C0406c c0406c) {
        cVar.f3228d.w(envelope.getAttackTimeInSeconds(), c0406c);
        cVar.f3230f.w(envelope.getDecayTimeInSeconds(), c0406c);
        cVar.f3231g.w(envelope.getSustainLevel(), c0406c);
        cVar.f3232h.w(envelope.getReleaseTimeInSeconds(), c0406c);
    }

    private static double calculateRateScaler(SonivoxWaveData.WavetableRegion wavetableRegion, double d2) {
        return Math.pow(2.0d, (d2 + (((double) wavetableRegion.tuning) * 0.01d)) / 12.0d);
    }

    private void reset() {
        this.ampEnv.f3228d.v(0.1d);
        this.ampEnv.f3230f.v(0.9d);
        this.ampEnv.f3231g.v(0.1d);
        this.ampEnv.f3232h.v(0.1d);
        this.filterEnv.f3228d.v(0.01d);
        this.filterEnv.f3230f.v(0.6d);
        this.filterEnv.f3231g.v(0.4d);
        this.filterEnv.f3232h.v(1.0d);
        this.filter.f3260r.v(1.0d);
        this.cutoff.v(5000.0d);
        this.filterEnvDepth.v(500.0d);
        this.region = null;
    }

    @Nullable
    private SonivoxWaveData.WavetableRegion selectRegionByNoteNumber(@Nullable SonivoxWaveData.WavetableRegion wavetableRegion, int i2, C0406c c0406c) {
        if (wavetableRegion == null || !wavetableRegion.isNoteInRange(i2)) {
            int programRegion = SonivoxWaveData.getProgramRegion(this.presetIndex);
            int i3 = programRegion;
            wavetableRegion = SonivoxWaveData.extractRegion(programRegion);
            while (!wavetableRegion.isNoteInRange(i2) && !wavetableRegion.isLast()) {
                i3++;
                wavetableRegion = SonivoxWaveData.extractRegion(i3);
            }
        }
        int i4 = wavetableRegion.waveIndex;
        if (wavetableRegion.useNoise()) {
            this.waveOffset = -1;
            this.waveSize = 0;
        } else {
            if (i4 >= SonivoxWaveData.getWaveCount()) {
                return null;
            }
            this.waveOffset = SonivoxWaveData.getWaveOffset(i4);
            this.waveSize = SonivoxWaveData.getWaveSize(i4);
        }
        SonivoxWaveData.Articulation articulationExtractArticulation = SonivoxWaveData.extractArticulation(wavetableRegion.artIndex);
        applyToEnvelope(articulationExtractArticulation.eg1, this.ampEnv, c0406c);
        applyToEnvelope(articulationExtractArticulation.eg2, this.filterEnv, c0406c);
        short s2 = articulationExtractArticulation.filterCutoff;
        if (s2 > 0) {
            this.cutoff.v(AbstractC0401a.b(((double) s2) * 0.01d));
        } else {
            this.cutoff.v(DEFAULT_FILTER_CUTOFF);
        }
        return wavetableRegion;
    }

    @Override // com.jsyn.unitgen.w
    public l getOutput() {
        return this.ampEnv.f3291b;
    }

    @Override // com.jsyn.unitgen.y
    public void noteOff(C0406c c0406c) {
        if (this.region == null) {
            return;
        }
        this.ampEnv.f3290a.K(c0406c);
        this.filterEnv.f3290a.K(c0406c);
        SonivoxWaveData.WavetableRegion wavetableRegion = (SonivoxWaveData.WavetableRegion) Assertions.checkNotNull(this.region);
        if (wavetableRegion.useNoise() && wavetableRegion.isLooped()) {
            int i2 = wavetableRegion.loopEnd;
            int i3 = i2 + 1;
            int i4 = this.waveSize;
            if (i3 < i4) {
                int i5 = this.waveOffset + i2;
                this.waveOscillator.f3278a.y(this.sonivoxSample, i5, i4 - i5, c0406c);
            }
        }
    }

    @Override // com.jsyn.unitgen.y
    public void noteOn(double d2, double d3, C0406c c0406c) {
        double dA = AbstractC0401a.a(d2);
        SonivoxWaveData.WavetableRegion wavetableRegionSelectRegionByNoteNumber = selectRegionByNoteNumber(this.region, (int) (0.5d + dA), c0406c);
        this.region = wavetableRegionSelectRegionByNoteNumber;
        if (wavetableRegionSelectRegionByNoteNumber == null) {
            return;
        }
        SonivoxWaveData.WavetableRegion wavetableRegion = (SonivoxWaveData.WavetableRegion) Assertions.checkNotNull(wavetableRegionSelectRegionByNoteNumber);
        double dCalculateRateScaler = calculateRateScaler(wavetableRegion, dA) * 22050.0d;
        double d4 = d3 * d3 * ((double) wavetableRegion.gain) * 3.0517578125E-5d * AMPLITUDE_SCALER;
        this.frequency.w(dCalculateRateScaler, c0406c);
        this.amplitude.w(d4, c0406c);
        this.ampEnv.f3290a.L(c0406c);
        this.filterEnv.f3290a.L(c0406c);
        this.waveOscillator.f3280c.i();
        this.whiteNoise.f3226c.i();
        if (wavetableRegion.useNoise()) {
            this.whiteNoise.f3226c.r(this.amplitudeMultiplier.f3285a);
            return;
        }
        this.waveOscillator.f3280c.r(this.amplitudeMultiplier.f3285a);
        this.waveOscillator.f3278a.o(c0406c);
        if (!wavetableRegion.isLooped()) {
            this.waveOscillator.f3278a.y(this.sonivoxSample, this.waveOffset, this.waveSize, c0406c);
            return;
        }
        int i2 = this.waveOffset;
        int i3 = wavetableRegion.loopStart + i2;
        int i4 = (wavetableRegion.loopEnd + i2) - i3;
        if (i3 > i2) {
            this.waveOscillator.f3278a.y(this.sonivoxSample, i2, i3 - i2, c0406c);
        }
        this.waveOscillator.f3278a.A(this.sonivoxSample, i3, i4, c0406c);
    }

    @Override // com.jsyn.unitgen.y
    public void usePreset(int i2) {
        if (this.presetIndex == i2) {
            return;
        }
        reset();
        this.presetIndex = i2;
        if (i2 == 0) {
            this.ampEnv.f3228d.v(0.1d);
            this.ampEnv.f3230f.v(0.9d);
            this.ampEnv.f3231g.v(0.1d);
            this.ampEnv.f3232h.v(0.1d);
            this.cutoff.v(300.0d);
            this.filterEnvDepth.v(500.0d);
            this.filter.f3260r.v(3.0d);
        }
    }
}
