package androidx.media3.decoder.midi;

import android.util.Pair;
import androidx.media3.common.ParserException;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.extractor.TrackOutput;
import com.google.common.collect.Iterables;
import java.util.ArrayList;

/* JADX INFO: loaded from: classes.dex */
final class TrackChunk implements Comparable<TrackChunk> {
    private static final int DEFAULT_TRACK_TEMPO_BPM = 120;
    private final int fileFormat;
    private long lastOutputEventTimestampUs;
    private int previousEventStatus;
    private final ArrayList<Pair<Long, Integer>> tempoChanges;
    private final TempoChangedListener tempoListener;
    private final int ticksPerQuarterNote;
    private long totalElapsedTicks;
    private final ParsableByteArray trackEventsBytes;
    private final ParsableByteArray scratch = new ParsableByteArray(3);
    private final TrackEvent currentTrackEvent = new TrackEvent();

    public interface TempoChangedListener {
        void onTempoChanged(int i2, long j2);
    }

    public TrackChunk(int i2, int i3, ParsableByteArray parsableByteArray, TempoChangedListener tempoChangedListener) {
        this.fileFormat = i2;
        this.ticksPerQuarterNote = i3;
        this.trackEventsBytes = parsableByteArray;
        this.tempoListener = tempoChangedListener;
        ArrayList<Pair<Long, Integer>> arrayList = new ArrayList<>();
        this.tempoChanges = arrayList;
        this.previousEventStatus = Integer.MIN_VALUE;
        arrayList.add(Pair.create(0L, 120));
    }

    private static long adjustTicksToUs(ArrayList<Pair<Long, Integer>> arrayList, long j2, long j3, int i2) {
        long j4 = j2;
        long j5 = j3;
        long jTicksToUs = 0;
        for (int size = arrayList.size() - 1; size >= 0; size--) {
            Pair<Long, Integer> pair = arrayList.get(size);
            long jMin = Math.min(j4, j5 - ((Long) pair.first).longValue());
            Assertions.checkState(jMin >= 0);
            jTicksToUs += ticksToUs(((Integer) pair.second).intValue(), jMin, i2);
            j4 -= jMin;
            j5 -= jMin;
        }
        return jTicksToUs + ticksToUs(((Integer) ((Pair) Iterables.getLast(arrayList)).second).intValue(), j4, i2);
    }

    private void notifyTempoChange(int i2, long j2) {
        if (this.fileFormat == 2) {
            addTempoChange(i2, j2);
        } else {
            this.tempoListener.onTempoChanged(i2, j2);
        }
    }

    private static long ticksToUs(int i2, long j2, int i3) {
        return (j2 * 60000000) / (((long) i2) * ((long) i3));
    }

    public void addTempoChange(int i2, long j2) {
        this.tempoChanges.add(Pair.create(Long.valueOf(j2), Integer.valueOf(i2)));
    }

    public void outputFrontSample(TrackOutput trackOutput, boolean z2) {
        if (this.currentTrackEvent.isPopulated()) {
            this.lastOutputEventTimestampUs += adjustTicksToUs(this.tempoChanges, this.currentTrackEvent.elapsedTimeDeltaTicks, this.totalElapsedTicks, this.ticksPerQuarterNote);
            if (z2 && this.currentTrackEvent.isNoteChannelEvent()) {
                this.trackEventsBytes.skipBytes(this.currentTrackEvent.eventFileSizeBytes);
                TrackEvent trackEvent = this.currentTrackEvent;
                this.previousEventStatus = trackEvent.statusByte;
                trackEvent.reset();
                return;
            }
            ParsableByteArray parsableByteArray = this.trackEventsBytes;
            TrackEvent trackEvent2 = this.currentTrackEvent;
            int i2 = trackEvent2.eventFileSizeBytes;
            int i3 = trackEvent2.timestampSize;
            int i4 = i2 - i3;
            parsableByteArray.skipBytes(i3);
            if (this.currentTrackEvent.isMidiEvent()) {
                this.trackEventsBytes.skipBytes(i4);
                this.scratch.setPosition(0);
                this.currentTrackEvent.writeTo(this.scratch.getData());
                parsableByteArray = this.scratch;
                i4 = this.currentTrackEvent.eventDecoderSizeBytes;
            } else if (this.currentTrackEvent.isMetaEvent()) {
                long j2 = this.currentTrackEvent.usPerQuarterNote;
                if (j2 != -9223372036854775807L) {
                    notifyTempoChange((int) (60000000 / j2), this.totalElapsedTicks);
                }
            }
            int i5 = i4;
            trackOutput.sampleData(parsableByteArray, i5);
            trackOutput.sampleMetadata(this.lastOutputEventTimestampUs, 0, i5, 0, (TrackOutput.CryptoData) null);
            if (this.tempoChanges.size() > 1) {
                Pair<Long, Integer> pair = (Pair) Iterables.getLast(this.tempoChanges);
                this.tempoChanges.clear();
                this.tempoChanges.add(pair);
            }
            TrackEvent trackEvent3 = this.currentTrackEvent;
            this.previousEventStatus = trackEvent3.statusByte;
            trackEvent3.reset();
        }
    }

    public long peekNextTimestampUs() {
        if (this.currentTrackEvent.isPopulated()) {
            return this.lastOutputEventTimestampUs + adjustTicksToUs(this.tempoChanges, this.currentTrackEvent.elapsedTimeDeltaTicks, this.totalElapsedTicks, this.ticksPerQuarterNote);
        }
        return -9223372036854775807L;
    }

    public void populateFrontTrackEvent() throws ParserException {
        if (this.currentTrackEvent.isPopulated() || !this.currentTrackEvent.populateFrom(this.trackEventsBytes, this.previousEventStatus)) {
            return;
        }
        this.totalElapsedTicks += this.currentTrackEvent.elapsedTimeDeltaTicks;
    }

    public void reset() {
        this.lastOutputEventTimestampUs = 0L;
        this.totalElapsedTicks = 0L;
        this.previousEventStatus = Integer.MIN_VALUE;
        this.trackEventsBytes.setPosition(0);
        this.scratch.setPosition(0);
        this.currentTrackEvent.reset();
        this.tempoChanges.clear();
        this.tempoChanges.add(Pair.create(0L, 120));
    }

    @Override // java.lang.Comparable
    public int compareTo(TrackChunk trackChunk) {
        long jPeekNextTimestampUs = peekNextTimestampUs();
        long jPeekNextTimestampUs2 = trackChunk.peekNextTimestampUs();
        if (jPeekNextTimestampUs == jPeekNextTimestampUs2) {
            return 0;
        }
        if (jPeekNextTimestampUs == -9223372036854775807L) {
            return 1;
        }
        if (jPeekNextTimestampUs2 == -9223372036854775807L) {
            return -1;
        }
        return Long.compare(jPeekNextTimestampUs, jPeekNextTimestampUs2);
    }
}
