Mulberry/android/src/com/mulberry/player/Spectrum.java
astar76 320f644bc8 Android app: buildable project recovered from the 1.2.2 APK
The original Android source was lost; android/ holds the jadx-decompiled
Java (com.mulberry.player) and resources from Android 1.2.2, fixed so it
compiles again (broken methods rewritten by hand), plus:

- android/build.sh: builds and signs Mulberry-<version>-Android.apk with the
  plain SDK tools (aapt2, javac, d8, apksigner), no Gradle; fetches
  jaudiotagger 2.0.3 (the version the old APK bundled) with a pinned checksum
- versionName 2.0.4, versionCode 6
- recoloured launcher icon (pink cookie, berry note) to match assets/mulberry.svg

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-12-02 12:32:30 -06:00

420 lines
17 KiB
Java

package com.mulberry.player;
import android.content.Context;
import android.media.MediaCodec;
import android.media.MediaCrypto;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.net.Uri;
import android.os.Build;
import android.view.Surface;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Map;
/* JADX INFO: loaded from: classes.dex */
public final class Spectrum {
private static final long AHEAD_MS = 15000;
public static final int BANDS = 48;
public static final int FPS = 30;
private static final int N = 1024;
private volatile byte[] bassFrames;
private volatile Clock clock;
private final Context ctx;
private volatile byte[] frames;
private volatile int gen;
private volatile boolean[] have;
private int tableRate;
private volatile long seekReqMs = -1;
private final float[] cos = new float[512];
private final float[] sin = new float[512];
private final float[] win = new float[N];
private final int[] rev = new int[N];
private final int[] bandLo = new int[48];
private final int[] bandHi = new int[48];
public interface Clock {
long positionMs();
}
public Spectrum(Context context) {
this.ctx = context.getApplicationContext();
for (int i = 0; i < 512; i++) {
this.cos[i] = (float) Math.cos((((double) i) * (-6.283185307179586d)) / 1024.0d);
this.sin[i] = (float) Math.sin((((double) i) * (-6.283185307179586d)) / 1024.0d);
}
for (int i2 = 0; i2 < N; i2++) {
this.win[i2] = (float) (0.5d - (Math.cos((6.283185307179586d * ((double) i2)) / 1023.0d) * 0.5d));
}
int iNumberOfTrailingZeros = Integer.numberOfTrailingZeros(N);
for (int i3 = 0; i3 < N; i3++) {
this.rev[i3] = Integer.reverse(i3) >>> (32 - iNumberOfTrailingZeros);
}
}
public static boolean enabled() {
return !App.lowPowerFlag && App.visualizerFlag;
}
public void setClock(Clock clock) {
this.clock = clock;
}
public void stop() {
this.gen++;
this.frames = null;
this.have = null;
this.bassFrames = null;
}
public void start(final Song song, long j) {
int i;
final int i2 = this.gen + 1;
this.gen = i2;
this.frames = null;
this.have = null;
this.seekReqMs = -1L;
if (song != null && !song.isStream() && enabled() && j > 0 && (i = ((int) ((30 * j) / 1000)) + 30) <= 162000) {
this.frames = new byte[i * 48];
this.bassFrames = new byte[i];
this.have = new boolean[i];
App.HEAVY.execute(new Runnable() { // from class: com.mulberry.player.Spectrum.1
@Override // java.lang.Runnable
public void run() {
try {
Spectrum.this.decode(song.toUri(), i2);
} catch (Throwable th) {
}
}
});
}
}
public void seek(long j) {
int i;
boolean[] zArr = this.have;
if (zArr != null && (i = (int) ((30 * j) / 1000)) >= 0 && i < zArr.length && !zArr[i]) {
this.seekReqMs = j;
}
}
public boolean levels(long j, float[] fArr) {
int i;
byte[] bArr = this.frames;
boolean[] zArr = this.have;
if (bArr == null || zArr == null || (i = (int) ((30 * j) / 1000)) < 0 || i >= zArr.length || !zArr[i]) {
return false;
}
int i2 = i * 48;
for (int i3 = 0; i3 < 48 && i3 < fArr.length; i3++) {
fArr[i3] = (bArr[i2 + i3] & 255) / 255.0f;
}
return true;
}
public float bass(long j) {
int i;
byte[] bArr = this.bassFrames;
boolean[] zArr = this.have;
if (bArr == null || zArr == null || (i = (int) ((30 * j) / 1000)) < 0 || i >= zArr.length || i >= bArr.length || !zArr[i]) {
return -1.0f;
}
return (bArr[i] & 255) / 255.0f;
}
private void buildBands(int i) {
if (i != this.tableRate) {
this.tableRate = i;
double dMin = Math.min(16000.0d, (((double) i) / 2.0d) - 1.0d);
for (int i2 = 0; i2 < 48; i2++) {
double dPow = Math.pow(dMin / 40.0d, ((double) i2) / 48.0d) * 40.0d;
double dPow2 = Math.pow(dMin / 40.0d, ((double) (i2 + 1)) / 48.0d) * 40.0d;
int iFloor = (int) Math.floor((dPow * 1024.0d) / ((double) i));
int iCeil = (int) Math.ceil((1024.0d * dPow2) / ((double) i));
int iMax = Math.max(1, Math.min(511, iFloor));
int iMax2 = Math.max(iMax + 1, Math.min(512, iCeil));
this.bandLo[i2] = iMax;
this.bandHi[i2] = iMax2;
}
}
}
/* JADX INFO: Access modifiers changed from: private */
public void decode(Uri uri, int i) throws Exception {
Throwable th;
boolean z;
long j;
long j2;
int i2;
int i3;
int iDequeueInputBuffer;
byte[] bArr = this.frames;
byte[] bArr2 = this.bassFrames;
boolean[] zArr = this.have;
if (bArr == null || zArr == null || bArr2 == null) {
return;
}
MediaExtractor mediaExtractor = new MediaExtractor();
MediaCodec mediaCodec = null;
try {
mediaExtractor.setDataSource(this.ctx, uri, (Map<String, String>) null);
MediaFormat mediaFormat = null;
int i4 = 0;
while (true) {
if (i4 >= mediaExtractor.getTrackCount()) {
i4 = -1;
break;
}
MediaFormat trackFormat = mediaExtractor.getTrackFormat(i4);
String string = trackFormat.getString("mime");
if (string != null && string.startsWith("audio/")) {
mediaFormat = trackFormat;
break;
}
i4++;
}
if (i4 < 0) {
if (0 != 0) {
try {
mediaCodec.stop();
mediaCodec.release();
} catch (Exception e) {
}
}
mediaExtractor.release();
return;
}
mediaExtractor.selectTrack(i4);
MediaCodec mediaCodecCreateDecoderByType = MediaCodec.createDecoderByType(mediaFormat.getString("mime"));
try {
mediaCodecCreateDecoderByType.configure(mediaFormat, (Surface) null, (MediaCrypto) null, 0);
mediaCodecCreateDecoderByType.start();
int integer = mediaFormat.containsKey("sample-rate") ? mediaFormat.getInteger("sample-rate") : 44100;
int integer2 = mediaFormat.containsKey("channel-count") ? mediaFormat.getInteger("channel-count") : 2;
boolean z2 = false;
buildBands(integer);
float[] fArr = new float[N];
int i5 = 0;
int i6 = 0;
long j3 = 0;
long j4 = 0;
float[] fArr2 = new float[N];
float[] fArr3 = new float[N];
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
boolean z3 = false;
while (this.gen == i) {
long j5 = this.seekReqMs;
if (j5 >= 0) {
this.seekReqMs = -1L;
mediaExtractor.seekTo(1000 * j5, 0);
mediaCodecCreateDecoderByType.flush();
z3 = false;
i6 = 0;
j3 = -1;
j4 = j5 * 1000;
}
Clock clock = this.clock;
if (clock == null || j4 / 1000 <= clock.positionMs() + AHEAD_MS) {
if (z3 || (iDequeueInputBuffer = mediaCodecCreateDecoderByType.dequeueInputBuffer(10000L)) < 0) {
z = z3;
} else {
ByteBuffer inputBuffer = mediaCodecCreateDecoderByType.getInputBuffer(iDequeueInputBuffer);
int sampleData = inputBuffer == null ? -1 : mediaExtractor.readSampleData(inputBuffer, 0);
if (sampleData < 0) {
mediaCodecCreateDecoderByType.queueInputBuffer(iDequeueInputBuffer, 0, 0, 0L, 4);
z = true;
} else {
mediaCodecCreateDecoderByType.queueInputBuffer(iDequeueInputBuffer, 0, sampleData, mediaExtractor.getSampleTime(), 0);
mediaExtractor.advance();
z = z3;
}
}
int iDequeueOutputBuffer = mediaCodecCreateDecoderByType.dequeueOutputBuffer(bufferInfo, 10000L);
if (iDequeueOutputBuffer == -2) {
MediaFormat outputFormat = mediaCodecCreateDecoderByType.getOutputFormat();
int integer3 = outputFormat.containsKey("sample-rate") ? outputFormat.getInteger("sample-rate") : integer;
int iMax = outputFormat.containsKey("channel-count") ? Math.max(1, outputFormat.getInteger("channel-count")) : integer2;
boolean z4 = (Build.VERSION.SDK_INT < 24 || !outputFormat.containsKey("pcm-encoding")) ? z2 : outputFormat.getInteger("pcm-encoding") == 4;
buildBands(integer3);
z3 = z;
z2 = z4;
integer2 = iMax;
integer = integer3;
} else if (iDequeueOutputBuffer < 0) {
z3 = z;
} else {
ByteBuffer outputBuffer = mediaCodecCreateDecoderByType.getOutputBuffer(iDequeueOutputBuffer);
if (outputBuffer == null || bufferInfo.size <= 0) {
j = j4;
j2 = j3;
i2 = i6;
} else {
outputBuffer.position(bufferInfo.offset);
outputBuffer.limit(bufferInfo.offset + bufferInfo.size);
outputBuffer.order(ByteOrder.nativeOrder());
if (j3 < 0 || Math.abs(bufferInfo.presentationTimeUs - j4) > 500000) {
j4 = bufferInfo.presentationTimeUs;
j2 = ((((30 * j4) / 1000000) + 1) * 1000000) / 30;
i3 = 0;
} else {
j2 = j3;
i3 = i6;
}
long jMax = 1000000 / ((long) Math.max(1, integer));
int i7 = bufferInfo.size / ((z2 ? 4 : 2) * integer2);
int i8 = 0;
int i9 = i5;
while (i8 < i7) {
float f = 0.0f;
for (int i10 = 0; i10 < integer2; i10++) {
f += z2 ? outputBuffer.getFloat() : outputBuffer.getShort() / 32768.0f;
}
fArr[i9] = f / integer2;
int i11 = (i9 + 1) & 1023;
int i12 = i3 < N ? i3 + 1 : i3;
long j6 = j4 + jMax;
if (j6 >= j2) {
int i13 = (int) ((30 * j2) / 1000000);
long j7 = 33333 + j2;
if (i12 == N) {
analyse(fArr, i11, fArr2, fArr3, i13, bArr, zArr, bArr2);
}
j2 = j7;
}
i8++;
j4 = j6;
i3 = i12;
i9 = i11;
}
j = j4;
i2 = i3;
i5 = i9;
}
mediaCodecCreateDecoderByType.releaseOutputBuffer(iDequeueOutputBuffer, false);
if ((bufferInfo.flags & 4) != 0) {
while (this.gen == i && this.seekReqMs < 0) {
Thread.sleep(300L);
}
}
z3 = z;
j4 = j;
j3 = j2;
i6 = i2;
}
} else {
Thread.sleep(250L);
}
}
if (mediaCodecCreateDecoderByType != null) {
try {
mediaCodecCreateDecoderByType.stop();
mediaCodecCreateDecoderByType.release();
} catch (Exception e2) {
}
}
mediaExtractor.release();
return;
} catch (Throwable th2) {
th = th2;
mediaCodec = mediaCodecCreateDecoderByType;
}
} catch (Throwable th3) {
th = th3;
}
if (mediaCodec != null) {
try {
mediaCodec.stop();
mediaCodec.release();
} catch (Exception e3) {
}
}
mediaExtractor.release();
if (th instanceof Exception) {
throw (Exception) th;
}
throw new Exception(th);
}
private void analyse(float[] fArr, int i, float[] fArr2, float[] fArr3, int i2, byte[] bArr, boolean[] zArr, byte[] bArr2) {
if (i2 >= 0 && i2 < zArr.length) {
for (int i3 = 0; i3 < N; i3++) {
int i4 = this.rev[i3];
fArr2[i4] = fArr[(i + i3) & 1023] * this.win[i3];
fArr3[i4] = 0.0f;
}
int i5 = 2;
while (true) {
int i6 = i5;
if (i6 > N) {
break;
}
int i7 = i6 >> 1;
int i8 = N / i6;
int i9 = 0;
while (true) {
int i10 = i9;
if (i10 < N) {
for (int i11 = 0; i11 < i7; i11++) {
float f = this.cos[i11 * i8];
float f2 = this.sin[i11 * i8];
int i12 = i10 + i11;
int i13 = i12 + i7;
float f3 = (fArr2[i13] * f) - (fArr3[i13] * f2);
float f4 = (f * fArr3[i13]) + (f2 * fArr2[i13]);
fArr2[i13] = fArr2[i12] - f3;
fArr3[i13] = fArr3[i12] - f4;
fArr2[i12] = fArr2[i12] + f3;
fArr3[i12] = f4 + fArr3[i12];
}
i9 = i10 + i6;
} else {
break;
}
}
i5 = i6 << 1;
}
int i14 = i2 * 48;
int i15 = 0;
while (true) {
int i16 = i15;
if (i16 >= 48) {
break;
}
float f5 = 0.0f;
for (int i17 = this.bandLo[i16]; i17 < this.bandHi[i16]; i17++) {
float f6 = (fArr2[i17] * fArr2[i17]) + (fArr3[i17] * fArr3[i17]);
if (f6 > f5) {
f5 = f6;
}
}
double dLog10 = (((Math.log10((((double) f5) / 65536.0d) + 1.0E-12d) * 10.0d) + 62.0d) / 56.0d) + (((double) i16) * 0.004d);
if (dLog10 < 0.0d) {
dLog10 = 0.0d;
}
if (dLog10 > 1.0d) {
dLog10 = 1.0d;
}
bArr[i14 + i16] = (byte) (dLog10 * 255.0d);
i15 = i16 + 1;
}
int iMax = Math.max(1, (int) Math.floor(35840.0d / ((double) Math.max(1, this.tableRate))));
int iMax2 = Math.max(iMax, (int) Math.ceil(153600.0d / ((double) Math.max(1, this.tableRate))));
double d = 0.0d;
while (iMax <= iMax2 && iMax < 512) {
d += (double) ((fArr2[iMax] * fArr2[iMax]) + (fArr3[iMax] * fArr3[iMax]));
iMax++;
}
double dLog11 = ((10.0d * Math.log10((d / 65536.0d) + 1.0E-12d)) + 36.0d) / 42.0d;
if (dLog11 < 0.0d) {
dLog11 = 0.0d;
}
if (dLog11 > 1.0d) {
dLog11 = 1.0d;
}
if (i2 < bArr2.length) {
bArr2[i2] = (byte) (dLog11 * 255.0d);
}
zArr[i2] = true;
}
}
}