package androidx.media3.decoder.midi;

import android.content.Context;
import androidx.annotation.Nullable;
import androidx.media3.common.Format;
import androidx.media3.common.MediaLibraryInfo;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Util;
import androidx.media3.decoder.DecoderInputBuffer;
import androidx.media3.decoder.DecoderOutputBuffer;
import androidx.media3.decoder.SimpleDecoder;
import androidx.media3.decoder.SimpleDecoderOutputBuffer;
import java.nio.ByteBuffer;
import org.checkerframework.checker.nullness.qual.EnsuresNonNull;
import q0.AbstractC0695a;
import q0.InterfaceC0696b;
import y0.C0744a;
import y0.C0746c;

/* JADX INFO: loaded from: classes.dex */
final class MidiDecoder extends SimpleDecoder<DecoderInputBuffer, SimpleDecoderOutputBuffer, MidiDecoderException> {
    public static final int CHANNEL_COUNT = 16;
    private static final int DEFAULT_AUDIO_OUTPUT_BUFFER_SIZE = 8820;
    public static final int DEFAULT_INPUT_BUFFER_COUNT = 16;
    public static final int DEFAULT_OUTPUT_BUFFER_COUNT = 16;
    public static final int DEFAULT_SAMPLE_RATE = 44100;
    public static final int NUM_OUTPUT_CHANNELS = 2;
    private static final double PCM_GENERATION_STEP_SECS = 0.1d;
    private static final int PCM_SAMPLE_SIZE_BYTES = 2;
    private double[] audioStreamOutputBuffer;
    private final Context context;
    private long lastReceivedTimestampUs;
    private w0.b midiSynthesizer;
    private C0746c multiSynth;
    private long outputTimeUs;
    private C0744a reader;
    private InterfaceC0696b synth;

    static {
        MediaLibraryInfo.registerModule("media3.decoder.midi");
    }

    public MidiDecoder(Context context) throws MidiDecoderException {
        this(context, 16, 16);
    }

    @EnsuresNonNull({"synth", "multiSynth", "reader", "midiSynthesizer"})
    private void createSynthesizers() throws MidiDecoderException {
        InterfaceC0696b interfaceC0696bA = AbstractC0695a.a();
        this.synth = interfaceC0696bA;
        interfaceC0696bA.f(false);
        C0746c c0746c = new C0746c();
        this.multiSynth = c0746c;
        c0746c.p(this.synth, 0, 16, 4, SonivoxVoiceDescription.getInstance((Context) Assertions.checkNotNull(this.context)));
        this.midiSynthesizer = new w0.b(this.multiSynth);
        this.reader = new C0744a(this.synth, 2);
        this.multiSynth.c().q(0, this.reader.b(), 0);
        this.multiSynth.c().q(0, this.reader.b(), 1);
        this.synth.start();
    }

    public static Format getDecoderOutputFormat() {
        return Util.getPcmFormat(4, 2, DEFAULT_SAMPLE_RATE);
    }

    private void resetSynthesizers() throws MidiDecoderException {
        this.synth.stop();
        this.multiSynth.c().i();
        createSynthesizers();
    }

    @Override // androidx.media3.decoder.SimpleDecoder
    protected DecoderInputBuffer createInputBuffer() {
        return new DecoderInputBuffer(1);
    }

    @Override // androidx.media3.decoder.Decoder
    public String getName() {
        return "MidiDecoder";
    }

    @Override // androidx.media3.decoder.SimpleDecoder, androidx.media3.decoder.Decoder
    public void release() {
        this.synth.stop();
        super.release();
    }

    public MidiDecoder(Context context, int i2, int i3) throws MidiDecoderException {
        super(new DecoderInputBuffer[i2], new SimpleDecoderOutputBuffer[i3]);
        this.context = context;
        this.audioStreamOutputBuffer = new double[DEFAULT_AUDIO_OUTPUT_BUFFER_SIZE];
        this.lastReceivedTimestampUs = -9223372036854775807L;
        createSynthesizers();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // androidx.media3.decoder.SimpleDecoder
    public SimpleDecoderOutputBuffer createOutputBuffer() {
        return new SimpleDecoderOutputBuffer(new DecoderOutputBuffer.Owner() { // from class: androidx.media3.decoder.midi.a
            @Override // androidx.media3.decoder.DecoderOutputBuffer.Owner
            public final void releaseOutputBuffer(DecoderOutputBuffer decoderOutputBuffer) {
                this.f2240a.releaseOutputBuffer((SimpleDecoderOutputBuffer) decoderOutputBuffer);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // androidx.media3.decoder.SimpleDecoder
    public MidiDecoderException createUnexpectedDecodeException(Throwable th) {
        return new MidiDecoderException("Unexpected decode error", th);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // androidx.media3.decoder.SimpleDecoder
    @Nullable
    public MidiDecoderException decode(DecoderInputBuffer decoderInputBuffer, SimpleDecoderOutputBuffer simpleDecoderOutputBuffer, boolean z2) {
        ByteBuffer byteBuffer = (ByteBuffer) Assertions.checkNotNull(decoderInputBuffer.data);
        if (z2) {
            this.lastReceivedTimestampUs = -9223372036854775807L;
            try {
                resetSynthesizers();
            } catch (MidiDecoderException e2) {
                return e2;
            }
        }
        if (this.lastReceivedTimestampUs == -9223372036854775807L) {
            this.outputTimeUs = decoderInputBuffer.timeUs;
        }
        boolean zIsAtLeastOutputStartTimeUs = isAtLeastOutputStartTimeUs(decoderInputBuffer.timeUs);
        if (zIsAtLeastOutputStartTimeUs) {
            try {
                if (this.lastReceivedTimestampUs != -9223372036854775807L) {
                    this.synth.d((decoderInputBuffer.timeUs - r3) * 1.0E-6d);
                }
                this.lastReceivedTimestampUs = decoderInputBuffer.timeUs;
            } catch (InterruptedException e3) {
                Thread.currentThread().interrupt();
                throw new IllegalStateException(e3);
            }
        }
        if (byteBuffer.remaining() > 0) {
            this.midiSynthesizer.n(byteBuffer.array(), byteBuffer.position(), byteBuffer.remaining());
        }
        int iA = this.reader.a();
        Assertions.checkState(zIsAtLeastOutputStartTimeUs || iA == 0);
        if (iA > this.audioStreamOutputBuffer.length) {
            this.audioStreamOutputBuffer = new double[(iA * 125) / 100];
        }
        int iC = 0;
        while (iC < iA) {
            iC += this.reader.c(this.audioStreamOutputBuffer, iC, iA - iC);
        }
        simpleDecoderOutputBuffer.init(this.outputTimeUs, iC * 4);
        ByteBuffer byteBuffer2 = (ByteBuffer) Assertions.checkNotNull(simpleDecoderOutputBuffer.data);
        for (int i2 = 0; i2 < iC; i2++) {
            byteBuffer2.putFloat((float) this.audioStreamOutputBuffer[i2]);
        }
        byteBuffer2.flip();
        this.outputTimeUs += ((((long) iC) * 1000000) / 2) / 44100;
        return null;
    }
}
