package org.fmod;

import android.media.AudioTrack;
import android.util.Log;
import java.nio.ByteBuffer;

/* loaded from: /storage/emulated/0/Documents/jadec/sources/com.UBI.A90.WW/dex-files/2.dex */
public class FMODAudioDevice implements Runnable {
    private static int h = 1;
    private static int i = 2;
    private static int j = 3;
    private volatile Thread a = null;
    private volatile boolean b = false;
    private AudioTrack c = null;
    private boolean d = false;
    private ByteBuffer e = null;
    private byte[] f = null;
    private volatile a g;

    private native int fmodGetInfo(int i2);

    private native int fmodInitJni();

    private native int fmodProcess(ByteBuffer byteBuffer);

    private void releaseAudioTrack() {
        if (this.c != null) {
            if (this.c.getState() == 1) {
                this.c.stop();
            }
            this.c.release();
            this.c = null;
        }
        this.e = null;
        this.f = null;
        this.d = false;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public native int fmodProcessMicData(ByteBuffer byteBuffer, int i2);

    public boolean isInitialized() {
        return fmodGetInfo(0) > 0;
    }

    public boolean isRunning() {
        return this.a != null && this.a.isAlive();
    }

    @Override // java.lang.Runnable
    public void run() {
        int i2;
        int i3 = 3;
        while (this.b) {
            if (isInitialized()) {
                if (this.d || i3 <= 0) {
                    i2 = i3;
                } else {
                    releaseAudioTrack();
                    int fmodGetInfo = fmodGetInfo(0);
                    int round = Math.round(AudioTrack.getMinBufferSize(fmodGetInfo, 3, 2) * 1.1f) & (-4);
                    int fmodGetInfo2 = fmodGetInfo(h);
                    int fmodGetInfo3 = fmodGetInfo(i);
                    if (fmodGetInfo2 * fmodGetInfo3 * 4 > round) {
                        round = fmodGetInfo3 * fmodGetInfo2 * 4;
                    }
                    this.c = new AudioTrack(3, fmodGetInfo, 3, 2, round, 1);
                    this.d = this.c.getState() == 1;
                    if (this.d) {
                        this.e = ByteBuffer.allocateDirect(fmodGetInfo2 * 2 * 2);
                        this.f = new byte[this.e.capacity()];
                        this.c.play();
                        i2 = 3;
                    } else {
                        Log.e("FMOD", "AudioTrack failed to initialize (status " + this.c.getState() + ")");
                        releaseAudioTrack();
                        i2 = i3 - 1;
                    }
                }
                if (!this.d) {
                    i3 = i2;
                } else if (fmodGetInfo(j) == 1) {
                    fmodProcess(this.e);
                    this.e.get(this.f, 0, this.e.capacity());
                    this.c.write(this.f, 0, this.e.capacity());
                    this.e.position(0);
                    i3 = i2;
                } else {
                    releaseAudioTrack();
                    i3 = i2;
                }
            } else {
                try {
                    Thread.sleep(10L);
                } catch (InterruptedException e) {
                    this.b = false;
                }
            }
        }
        releaseAudioTrack();
    }

    public synchronized void start() {
        if (this.a != null) {
            stop();
        }
        this.a = new Thread(this, "FMODAudioDevice");
        this.a.setPriority(10);
        this.b = true;
        fmodInitJni();
        this.a.start();
        if (this.g != null) {
            this.g.b();
        }
    }

    public synchronized int startAudioRecord(int i2, int i3, int i4) {
        if (this.g == null) {
            this.g = new a(this, i2, i3);
            this.g.b();
        }
        return this.g.a();
    }

    public synchronized void stop() {
        while (this.a != null) {
            this.b = false;
            try {
                this.a.join();
                this.a = null;
            } catch (InterruptedException e) {
            }
        }
        if (this.g != null) {
            this.g.c();
        }
    }

    public synchronized void stopAudioRecord() {
        if (this.g != null) {
            this.g.c();
            this.g = null;
        }
    }
}
