package androidx.media3.common.audio;

import androidx.media3.common.util.Assertions;
import java.nio.ShortBuffer;
import java.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
final class Sonic {
    private static final int AMDF_FREQUENCY = 4000;
    private static final int BYTES_PER_SAMPLE = 2;
    private static final int MAXIMUM_PITCH = 400;
    private static final int MINIMUM_PITCH = 65;
    private final int channelCount;
    private final short[] downSampleBuffer;
    private short[] inputBuffer;
    private int inputFrameCount;
    private final int inputSampleRateHz;
    private int maxDiff;
    private final int maxPeriod;
    private final int maxRequiredFrameCount;
    private int minDiff;
    private final int minPeriod;
    private int newRatePosition;
    private int oldRatePosition;
    private short[] outputBuffer;
    private int outputFrameCount;
    private final float pitch;
    private short[] pitchBuffer;
    private int pitchFrameCount;
    private int prevMinDiff;
    private int prevPeriod;
    private final float rate;
    private int remainingInputToCopyFrameCount;
    private final float speed;

    public Sonic(int i6, int i7, float f6, float f7, int i8) {
        this.inputSampleRateHz = i6;
        this.channelCount = i7;
        this.speed = f6;
        this.pitch = f7;
        this.rate = i6 / i8;
        this.minPeriod = i6 / 400;
        int i9 = i6 / 65;
        this.maxPeriod = i9;
        int i10 = i9 * 2;
        this.maxRequiredFrameCount = i10;
        this.downSampleBuffer = new short[i10];
        this.inputBuffer = new short[i10 * i7];
        this.outputBuffer = new short[i10 * i7];
        this.pitchBuffer = new short[i10 * i7];
    }

    private void adjustRate(float f6, int i6) {
        int i7;
        int i8;
        if (this.outputFrameCount == i6) {
            return;
        }
        int i9 = this.inputSampleRateHz;
        int i10 = (int) (i9 / f6);
        while (true) {
            if (i10 <= 16384 && i9 <= 16384) {
                break;
            }
            i10 /= 2;
            i9 /= 2;
        }
        moveNewSamplesToPitchBuffer(i6);
        int i11 = 0;
        while (true) {
            int i12 = this.pitchFrameCount;
            if (i11 >= i12 - 1) {
                removePitchFrames(i12 - 1);
                return;
            }
            while (true) {
                i7 = this.oldRatePosition;
                int i13 = (i7 + 1) * i10;
                i8 = this.newRatePosition;
                if (i13 <= i8 * i9) {
                    break;
                }
                this.outputBuffer = ensureSpaceForAdditionalFrames(this.outputBuffer, this.outputFrameCount, 1);
                int i14 = 0;
                while (true) {
                    int i15 = this.channelCount;
                    if (i14 < i15) {
                        this.outputBuffer[(this.outputFrameCount * i15) + i14] = interpolate(this.pitchBuffer, (i15 * i11) + i14, i9, i10);
                        i14++;
                    }
                }
                this.newRatePosition++;
                this.outputFrameCount++;
            }
            int i16 = i7 + 1;
            this.oldRatePosition = i16;
            if (i16 == i9) {
                this.oldRatePosition = 0;
                Assertions.checkState(i8 == i10);
                this.newRatePosition = 0;
            }
            i11++;
        }
    }

    private void changeSpeed(float f6) {
        int iInsertPitchPeriod;
        int i6 = this.inputFrameCount;
        if (i6 < this.maxRequiredFrameCount) {
            return;
        }
        int iSkipPitchPeriod = 0;
        do {
            if (this.remainingInputToCopyFrameCount > 0) {
                iInsertPitchPeriod = copyInputToOutput(iSkipPitchPeriod);
            } else {
                int iFindPitchPeriod = findPitchPeriod(this.inputBuffer, iSkipPitchPeriod);
                if (f6 > 1.0d) {
                    iSkipPitchPeriod = iFindPitchPeriod + skipPitchPeriod(this.inputBuffer, iSkipPitchPeriod, f6, iFindPitchPeriod) + iSkipPitchPeriod;
                } else {
                    iInsertPitchPeriod = insertPitchPeriod(this.inputBuffer, iSkipPitchPeriod, f6, iFindPitchPeriod);
                }
            }
            iSkipPitchPeriod += iInsertPitchPeriod;
        } while (this.maxRequiredFrameCount + iSkipPitchPeriod <= i6);
        removeProcessedInputFrames(iSkipPitchPeriod);
    }

    private int copyInputToOutput(int i6) {
        int iMin = Math.min(this.maxRequiredFrameCount, this.remainingInputToCopyFrameCount);
        copyToOutput(this.inputBuffer, i6, iMin);
        this.remainingInputToCopyFrameCount -= iMin;
        return iMin;
    }

    private void copyToOutput(short[] sArr, int i6, int i7) {
        short[] sArrEnsureSpaceForAdditionalFrames = ensureSpaceForAdditionalFrames(this.outputBuffer, this.outputFrameCount, i7);
        this.outputBuffer = sArrEnsureSpaceForAdditionalFrames;
        int i8 = this.channelCount;
        System.arraycopy(sArr, i6 * i8, sArrEnsureSpaceForAdditionalFrames, this.outputFrameCount * i8, i8 * i7);
        this.outputFrameCount += i7;
    }

    private void downSampleInput(short[] sArr, int i6, int i7) {
        int i8 = this.maxRequiredFrameCount / i7;
        int i9 = this.channelCount;
        int i10 = i7 * i9;
        int i11 = i6 * i9;
        for (int i12 = 0; i12 < i8; i12++) {
            int i13 = 0;
            for (int i14 = 0; i14 < i10; i14++) {
                i13 += sArr[(i12 * i10) + i11 + i14];
            }
            this.downSampleBuffer[i12] = (short) (i13 / i10);
        }
    }

    private short[] ensureSpaceForAdditionalFrames(short[] sArr, int i6, int i7) {
        int length = sArr.length;
        int i8 = this.channelCount;
        int i9 = length / i8;
        return i6 + i7 <= i9 ? sArr : Arrays.copyOf(sArr, (((i9 * 3) / 2) + i7) * i8);
    }

    private int findPitchPeriod(short[] sArr, int i6) {
        int iFindPitchPeriodInRange;
        int i7 = this.inputSampleRateHz;
        int i8 = i7 > AMDF_FREQUENCY ? i7 / AMDF_FREQUENCY : 1;
        if (this.channelCount == 1 && i8 == 1) {
            iFindPitchPeriodInRange = findPitchPeriodInRange(sArr, i6, this.minPeriod, this.maxPeriod);
        } else {
            downSampleInput(sArr, i6, i8);
            int iFindPitchPeriodInRange2 = findPitchPeriodInRange(this.downSampleBuffer, 0, this.minPeriod / i8, this.maxPeriod / i8);
            if (i8 != 1) {
                int i9 = iFindPitchPeriodInRange2 * i8;
                int i10 = i8 * 4;
                int i11 = i9 - i10;
                int i12 = i9 + i10;
                int i13 = this.minPeriod;
                if (i11 < i13) {
                    i11 = i13;
                }
                int i14 = this.maxPeriod;
                if (i12 > i14) {
                    i12 = i14;
                }
                if (this.channelCount == 1) {
                    iFindPitchPeriodInRange = findPitchPeriodInRange(sArr, i6, i11, i12);
                } else {
                    downSampleInput(sArr, i6, 1);
                    iFindPitchPeriodInRange = findPitchPeriodInRange(this.downSampleBuffer, 0, i11, i12);
                }
            } else {
                iFindPitchPeriodInRange = iFindPitchPeriodInRange2;
            }
        }
        int i15 = previousPeriodBetter(this.minDiff, this.maxDiff) ? this.prevPeriod : iFindPitchPeriodInRange;
        this.prevMinDiff = this.minDiff;
        this.prevPeriod = iFindPitchPeriodInRange;
        return i15;
    }

    private int findPitchPeriodInRange(short[] sArr, int i6, int i7, int i8) {
        int i9 = i6 * this.channelCount;
        int i10 = 255;
        int i11 = 1;
        int i12 = 0;
        int i13 = 0;
        while (i7 <= i8) {
            int iAbs = 0;
            for (int i14 = 0; i14 < i7; i14++) {
                iAbs += Math.abs(sArr[i9 + i14] - sArr[(i9 + i7) + i14]);
            }
            if (iAbs * i12 < i11 * i7) {
                i12 = i7;
                i11 = iAbs;
            }
            if (iAbs * i10 > i13 * i7) {
                i10 = i7;
                i13 = iAbs;
            }
            i7++;
        }
        this.minDiff = i11 / i12;
        this.maxDiff = i13 / i10;
        return i12;
    }

    private int insertPitchPeriod(short[] sArr, int i6, float f6, int i7) {
        int i8;
        if (f6 < 0.5f) {
            i8 = (int) ((i7 * f6) / (1.0f - f6));
        } else {
            this.remainingInputToCopyFrameCount = (int) ((((2.0f * f6) - 1.0f) * i7) / (1.0f - f6));
            i8 = i7;
        }
        int i9 = i7 + i8;
        short[] sArrEnsureSpaceForAdditionalFrames = ensureSpaceForAdditionalFrames(this.outputBuffer, this.outputFrameCount, i9);
        this.outputBuffer = sArrEnsureSpaceForAdditionalFrames;
        int i10 = this.channelCount;
        System.arraycopy(sArr, i6 * i10, sArrEnsureSpaceForAdditionalFrames, this.outputFrameCount * i10, i10 * i7);
        overlapAdd(i8, this.channelCount, this.outputBuffer, this.outputFrameCount + i7, sArr, i6 + i7, sArr, i6);
        this.outputFrameCount += i9;
        return i8;
    }

    private short interpolate(short[] sArr, int i6, int i7, int i8) {
        short s6 = sArr[i6];
        short s7 = sArr[i6 + this.channelCount];
        int i9 = this.newRatePosition * i7;
        int i10 = this.oldRatePosition;
        int i11 = i10 * i8;
        int i12 = (i10 + 1) * i8;
        int i13 = i12 - i9;
        int i14 = i12 - i11;
        return (short) ((((i14 - i13) * s7) + (s6 * i13)) / i14);
    }

    private void moveNewSamplesToPitchBuffer(int i6) {
        int i7 = this.outputFrameCount - i6;
        short[] sArrEnsureSpaceForAdditionalFrames = ensureSpaceForAdditionalFrames(this.pitchBuffer, this.pitchFrameCount, i7);
        this.pitchBuffer = sArrEnsureSpaceForAdditionalFrames;
        short[] sArr = this.outputBuffer;
        int i8 = this.channelCount;
        System.arraycopy(sArr, i6 * i8, sArrEnsureSpaceForAdditionalFrames, this.pitchFrameCount * i8, i8 * i7);
        this.outputFrameCount = i6;
        this.pitchFrameCount += i7;
    }

    private static void overlapAdd(int i6, int i7, short[] sArr, int i8, short[] sArr2, int i9, short[] sArr3, int i10) {
        for (int i11 = 0; i11 < i7; i11++) {
            int i12 = (i8 * i7) + i11;
            int i13 = (i10 * i7) + i11;
            int i14 = (i9 * i7) + i11;
            for (int i15 = 0; i15 < i6; i15++) {
                sArr[i12] = (short) (((sArr3[i13] * i15) + ((i6 - i15) * sArr2[i14])) / i6);
                i12 += i7;
                i14 += i7;
                i13 += i7;
            }
        }
    }

    private boolean previousPeriodBetter(int i6, int i7) {
        return i6 != 0 && this.prevPeriod != 0 && i7 <= i6 * 3 && i6 * 2 > this.prevMinDiff * 3;
    }

    private void processStreamInput() {
        int i6 = this.outputFrameCount;
        float f6 = this.speed;
        float f7 = this.pitch;
        float f8 = f6 / f7;
        float f9 = this.rate * f7;
        double d6 = f8;
        if (d6 > 1.00001d || d6 < 0.99999d) {
            changeSpeed(f8);
        } else {
            copyToOutput(this.inputBuffer, 0, this.inputFrameCount);
            this.inputFrameCount = 0;
        }
        if (f9 != 1.0f) {
            adjustRate(f9, i6);
        }
    }

    private void removePitchFrames(int i6) {
        if (i6 == 0) {
            return;
        }
        short[] sArr = this.pitchBuffer;
        int i7 = this.channelCount;
        System.arraycopy(sArr, i6 * i7, sArr, 0, (this.pitchFrameCount - i6) * i7);
        this.pitchFrameCount -= i6;
    }

    private void removeProcessedInputFrames(int i6) {
        int i7 = this.inputFrameCount - i6;
        short[] sArr = this.inputBuffer;
        int i8 = this.channelCount;
        System.arraycopy(sArr, i6 * i8, sArr, 0, i8 * i7);
        this.inputFrameCount = i7;
    }

    private int skipPitchPeriod(short[] sArr, int i6, float f6, int i7) {
        int i8;
        if (f6 >= 2.0f) {
            i8 = (int) (i7 / (f6 - 1.0f));
        } else {
            this.remainingInputToCopyFrameCount = (int) (((2.0f - f6) * i7) / (f6 - 1.0f));
            i8 = i7;
        }
        short[] sArrEnsureSpaceForAdditionalFrames = ensureSpaceForAdditionalFrames(this.outputBuffer, this.outputFrameCount, i8);
        this.outputBuffer = sArrEnsureSpaceForAdditionalFrames;
        overlapAdd(i8, this.channelCount, sArrEnsureSpaceForAdditionalFrames, this.outputFrameCount, sArr, i6, sArr, i6 + i7);
        this.outputFrameCount += i8;
        return i8;
    }

    public void flush() {
        this.inputFrameCount = 0;
        this.outputFrameCount = 0;
        this.pitchFrameCount = 0;
        this.oldRatePosition = 0;
        this.newRatePosition = 0;
        this.remainingInputToCopyFrameCount = 0;
        this.prevPeriod = 0;
        this.prevMinDiff = 0;
        this.minDiff = 0;
        this.maxDiff = 0;
    }

    public void getOutput(ShortBuffer shortBuffer) {
        int iMin = Math.min(shortBuffer.remaining() / this.channelCount, this.outputFrameCount);
        shortBuffer.put(this.outputBuffer, 0, this.channelCount * iMin);
        int i6 = this.outputFrameCount - iMin;
        this.outputFrameCount = i6;
        short[] sArr = this.outputBuffer;
        int i7 = this.channelCount;
        System.arraycopy(sArr, iMin * i7, sArr, 0, i6 * i7);
    }

    public int getOutputSize() {
        return this.outputFrameCount * this.channelCount * 2;
    }

    public int getPendingInputBytes() {
        return this.inputFrameCount * this.channelCount * 2;
    }

    public void queueEndOfStream() {
        int i6;
        int i7 = this.inputFrameCount;
        float f6 = this.speed;
        float f7 = this.pitch;
        int i8 = this.outputFrameCount + ((int) ((((i7 / (f6 / f7)) + this.pitchFrameCount) / (this.rate * f7)) + 0.5f));
        this.inputBuffer = ensureSpaceForAdditionalFrames(this.inputBuffer, i7, (this.maxRequiredFrameCount * 2) + i7);
        int i9 = 0;
        while (true) {
            i6 = this.maxRequiredFrameCount;
            int i10 = this.channelCount;
            if (i9 >= i6 * 2 * i10) {
                break;
            }
            this.inputBuffer[(i10 * i7) + i9] = 0;
            i9++;
        }
        this.inputFrameCount = (i6 * 2) + this.inputFrameCount;
        processStreamInput();
        if (this.outputFrameCount > i8) {
            this.outputFrameCount = i8;
        }
        this.inputFrameCount = 0;
        this.remainingInputToCopyFrameCount = 0;
        this.pitchFrameCount = 0;
    }

    public void queueInput(ShortBuffer shortBuffer) {
        int iRemaining = shortBuffer.remaining();
        int i6 = this.channelCount;
        int i7 = iRemaining / i6;
        short[] sArrEnsureSpaceForAdditionalFrames = ensureSpaceForAdditionalFrames(this.inputBuffer, this.inputFrameCount, i7);
        this.inputBuffer = sArrEnsureSpaceForAdditionalFrames;
        shortBuffer.get(sArrEnsureSpaceForAdditionalFrames, this.inputFrameCount * this.channelCount, ((i6 * i7) * 2) / 2);
        this.inputFrameCount += i7;
        processStreamInput();
    }
}
