package t0;

import androidx.media3.decoder.midi.MidiDecoder;
import c1.C0405b;
import c1.C0406c;
import c1.InterfaceC0404a;
import com.jsyn.unitgen.u;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger;
import q0.InterfaceC0696b;
import s0.AbstractC0712a;
import s0.InterfaceC0713b;

/* JADX INFO: renamed from: t0.b, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public class C0719b implements InterfaceC0696b {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    static Logger f6485q = Logger.getLogger(C0719b.class.getName());

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private d f6486a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final C0405b f6487b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private e f6488c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private e f6489d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private double f6490e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private long f6491f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private boolean f6492g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private boolean f6493h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private boolean f6494i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private int f6495j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private double f6496k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private final ArrayList f6497l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private final ArrayList f6498m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private final ArrayList f6499n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private C0718a f6500o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private final CopyOnWriteArrayList f6501p;

    /* JADX INFO: renamed from: t0.b$a */
    class a implements InterfaceC0404a {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ u f6502b;

        a(u uVar) {
            this.f6502b = uVar;
        }

        @Override // c1.InterfaceC0404a
        public void run() {
            C0719b.this.x(this.f6502b);
        }
    }

    /* JADX INFO: renamed from: t0.b$b, reason: collision with other inner class name */
    class C0096b implements InterfaceC0404a {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ u f6504b;

        C0096b(u uVar) {
            this.f6504b = uVar;
        }

        @Override // c1.InterfaceC0404a
        public void run() {
            C0719b.this.y(this.f6504b);
        }
    }

    /* JADX INFO: renamed from: t0.b$c */
    static class c {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final double[] f6506a;

        c(int i2) {
            this.f6506a = new double[i2];
        }

        void b() {
            int i2 = 0;
            while (true) {
                double[] dArr = this.f6506a;
                if (i2 >= dArr.length) {
                    return;
                }
                dArr[i2] = 0.0d;
                i2++;
            }
        }
    }

    /* JADX INFO: renamed from: t0.b$d */
    private class d extends Thread {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private volatile boolean f6507b = true;

        d(int i2, int i3, int i4, int i5) {
            if (i3 > 0) {
                C0719b.j(C0719b.this);
                int unused = C0719b.this.f6495j;
                throw null;
            }
            if (i5 <= 0) {
                return;
            }
            C0719b.j(C0719b.this);
            int unused2 = C0719b.this.f6495j;
            throw null;
        }

        public void a() {
            this.f6507b = false;
            interrupt();
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public void run() {
            C0719b.f6485q.fine("JSyn synthesis thread starting.");
            try {
                C0719b.this.f6500o = new C0718a();
                while (this.f6507b) {
                    C0719b.this.f6500o.a();
                    C0719b.this.A();
                    C0719b.this.r();
                    C0719b.this.f6500o.b();
                    if (C0719b.this.b()) {
                        Thread.sleep(2L);
                    }
                }
            } finally {
                try {
                } finally {
                }
            }
            C0719b.f6485q.fine("JSyn synthesis thread exiting.");
        }
    }

    /* JADX INFO: renamed from: t0.b$e */
    class e {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final double[] f6509a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        c[] f6510b;

        e(int i2, int i3, int i4) {
            this.f6509a = new double[i2 * i4];
            this.f6510b = new c[i4];
            int i5 = 0;
            while (true) {
                c[] cVarArr = this.f6510b;
                if (i5 >= cVarArr.length) {
                    return;
                }
                cVarArr[i5] = new c(i3);
                i5++;
            }
        }

        public void a() {
            int i2 = 0;
            while (true) {
                c[] cVarArr = this.f6510b;
                if (i2 >= cVarArr.length) {
                    return;
                }
                cVarArr[i2].b();
                i2++;
            }
        }

        int b(int i2) {
            for (int i3 = 0; i3 < 8; i3++) {
                int i4 = 0;
                while (true) {
                    c[] cVarArr = this.f6510b;
                    if (i4 < cVarArr.length) {
                        cVarArr[i4].f6506a[i3] = this.f6509a[i2];
                        i4++;
                        i2++;
                    }
                }
            }
            return i2;
        }

        int c(int i2) {
            for (int i3 = 0; i3 < 8; i3++) {
                int i4 = 0;
                while (true) {
                    c[] cVarArr = this.f6510b;
                    if (i4 < cVarArr.length) {
                        this.f6509a[i2] = cVarArr[i4].f6506a[i3];
                        i4++;
                        i2++;
                    }
                }
            }
            return i2;
        }
    }

    public C0719b(InterfaceC0713b interfaceC0713b) {
        this.f6487b = new C0405b();
        this.f6492g = true;
        this.f6493h = true;
        this.f6495j = MidiDecoder.DEFAULT_SAMPLE_RATE;
        this.f6496k = 1.0d / ((double) MidiDecoder.DEFAULT_SAMPLE_RATE);
        this.f6497l = new ArrayList();
        this.f6498m = new ArrayList();
        this.f6499n = new ArrayList();
        this.f6501p = new CopyOnWriteArrayList();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void A() {
        Iterator it = this.f6501p.iterator();
        while (it.hasNext()) {
            ((Runnable) it.next()).run();
        }
    }

    private void B(int i2, int i3) {
        this.f6488c = new e(64, 8, i2);
        this.f6489d = new e(64, 8, i3);
    }

    private void I() {
        synchronized (this.f6498m) {
            try {
                ListIterator listIterator = this.f6498m.listIterator();
                while (listIterator.hasNext()) {
                    u uVar = (u) listIterator.next();
                    if (this.f6492g) {
                        uVar.pullData(t(), 0, 8);
                    } else {
                        uVar.generate(0, 8);
                    }
                }
                for (u uVar2 : this.f6499n) {
                    this.f6498m.remove(uVar2);
                    uVar2.flattenOutputs();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        this.f6499n.clear();
    }

    static /* synthetic */ InterfaceC0713b j(C0719b c0719b) {
        c0719b.getClass();
        return null;
    }

    private void o() {
        this.f6489d.a();
    }

    public static double p(short s2) {
        return ((double) s2) * 3.051850947599719E-5d;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void x(u uVar) {
        if (uVar.getCircuit() == null) {
            synchronized (this.f6498m) {
                try {
                    if (!this.f6498m.contains(uVar)) {
                        this.f6498m.add(uVar);
                    }
                } finally {
                }
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void y(u uVar) {
        synchronized (this.f6498m) {
            this.f6498m.remove(uVar);
        }
        uVar.flattenOutputs();
    }

    private void z(C0406c c0406c) {
        List listB = this.f6487b.b(c0406c);
        while (listB != null) {
            while (!listB.isEmpty()) {
                InterfaceC0404a interfaceC0404a = (InterfaceC0404a) listB.remove(0);
                f6485q.fine("processing " + interfaceC0404a + ", at time " + c0406c.b());
                interfaceC0404a.run();
            }
            listB = this.f6487b.b(c0406c);
        }
    }

    public void C(double d2) throws InterruptedException {
        double dS = s();
        while (true) {
            double d3 = d2 - dS;
            if (d3 <= u.FALSE) {
                return;
            }
            if (this.f6493h) {
                long j2 = (long) (d3 * 1000.0d);
                if (j2 <= 0) {
                    j2 = 1;
                }
                Thread.sleep(j2);
            } else {
                r();
            }
            dS = s();
        }
    }

    public synchronized void D(int i2, int i3, int i4, int i5, int i6) {
        try {
            if (this.f6494i) {
                return;
            }
            this.f6495j = i2;
            double d2 = i2;
            this.f6496k = 1.0d / d2;
            B(i4, i6);
            this.f6490e = 2.0d / d2;
            if (this.f6493h) {
                this.f6486a = new d(i3, i4, i5, i6);
                f6485q.fine("Synth thread old priority = " + this.f6486a.getPriority());
                int priority = 10;
                if (this.f6486a.getPriority() + 2 <= 10) {
                    priority = this.f6486a.getPriority() + 2;
                }
                this.f6486a.setPriority(priority);
                f6485q.fine("Synth thread new priority = " + this.f6486a.getPriority());
                this.f6486a.start();
            }
            this.f6494i = true;
        } catch (Throwable th) {
            throw th;
        }
    }

    public void E(u uVar) {
        F(uVar, e());
    }

    public void F(u uVar, C0406c c0406c) {
        if (uVar.getCircuit() == null) {
            c(c0406c, new a(uVar));
        }
    }

    public void G(u uVar) {
        H(uVar, e());
    }

    public void H(u uVar, C0406c c0406c) {
        c(c0406c, new C0096b(uVar));
    }

    @Override // q0.InterfaceC0696b
    public void a(u uVar) {
        uVar.setSynthesisEngine(this);
        this.f6497l.add(uVar);
    }

    @Override // q0.InterfaceC0696b
    public boolean b() {
        return this.f6493h;
    }

    @Override // q0.InterfaceC0696b
    public void c(C0406c c0406c, InterfaceC0404a interfaceC0404a) {
        if (Thread.currentThread() == this.f6486a && c0406c.b() <= s()) {
            interfaceC0404a.run();
            return;
        }
        f6485q.fine("scheduling " + interfaceC0404a + ", at time " + c0406c.b());
        this.f6487b.a(c0406c, interfaceC0404a);
    }

    @Override // q0.InterfaceC0696b
    public void d(double d2) throws InterruptedException {
        C(s() + d2);
    }

    @Override // q0.InterfaceC0696b
    public C0406c e() {
        return new C0406c(s());
    }

    @Override // q0.InterfaceC0696b
    public void f(boolean z2) {
        this.f6493h = z2;
    }

    public void n(u uVar) {
        synchronized (this.f6499n) {
            this.f6499n.add(uVar);
        }
    }

    public double q(double d2) {
        return Math.pow(3.0517578125E-5d, 1.0d / (d2 * ((double) v())));
    }

    public void r() {
        int iB = 0;
        int iC = 0;
        for (int i2 = 0; i2 < 8; i2++) {
            e eVar = this.f6488c;
            if (eVar != null) {
                iB = eVar.b(iB);
            }
            z(e());
            o();
            I();
            e eVar2 = this.f6489d;
            if (eVar2 != null) {
                iC = eVar2.c(iC);
            }
            this.f6491f += 8;
        }
    }

    public double s() {
        return this.f6491f * this.f6496k;
    }

    @Override // q0.InterfaceC0696b
    public void start() {
        D(MidiDecoder.DEFAULT_SAMPLE_RATE, -1, 0, -1, 2);
    }

    /* JADX WARN: Removed duplicated region for block: B:33:0x002a A[EXC_TOP_SPLITTER, SYNTHETIC] */
    @Override // q0.InterfaceC0696b
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public synchronized void stop() {
        /*
            r3 = this;
            monitor-enter(r3)
            boolean r0 = r3.f6494i     // Catch: java.lang.Throwable -> Le
            if (r0 != 0) goto L10
            java.util.logging.Logger r0 = t0.C0719b.f6485q     // Catch: java.lang.Throwable -> Le
            java.lang.String r1 = "JSyn already stopped."
            r0.info(r1)     // Catch: java.lang.Throwable -> Le
            monitor-exit(r3)
            return
        Le:
            r0 = move-exception
            goto L38
        L10:
            boolean r0 = r3.f6493h     // Catch: java.lang.Throwable -> Le
            if (r0 == 0) goto L27
            t0.b$d r0 = r3.f6486a     // Catch: java.lang.Throwable -> Le
            if (r0 == 0) goto L27
            r0.a()     // Catch: java.lang.Throwable -> Le java.lang.InterruptedException -> L23
            t0.b$d r0 = r3.f6486a     // Catch: java.lang.Throwable -> Le java.lang.InterruptedException -> L23
            r1 = 2000(0x7d0, double:9.88E-321)
            r0.join(r1)     // Catch: java.lang.Throwable -> Le java.lang.InterruptedException -> L23
            goto L27
        L23:
            r0 = move-exception
            r0.printStackTrace()     // Catch: java.lang.Throwable -> Le
        L27:
            java.util.ArrayList r0 = r3.f6498m     // Catch: java.lang.Throwable -> Le
            monitor-enter(r0)     // Catch: java.lang.Throwable -> Le
            java.util.ArrayList r1 = r3.f6498m     // Catch: java.lang.Throwable -> L35
            r1.clear()     // Catch: java.lang.Throwable -> L35
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L35
            r0 = 0
            r3.f6494i = r0     // Catch: java.lang.Throwable -> Le
            monitor-exit(r3)
            return
        L35:
            r1 = move-exception
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L35
            throw r1     // Catch: java.lang.Throwable -> Le
        L38:
            monitor-exit(r3)     // Catch: java.lang.Throwable -> Le
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: t0.C0719b.stop():void");
    }

    public long t() {
        return this.f6491f;
    }

    public String toString() {
        return "JSyn V17.2.0 (build 467, 2025-06-29)";
    }

    public double u() {
        return this.f6496k;
    }

    public int v() {
        return this.f6495j;
    }

    public double w() {
        return this.f6490e;
    }

    /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
    public C0719b() {
        this(null);
        AbstractC0712a.a();
    }
}
