Skip to content

Commit

Permalink
Merge pull request #81 from ZTFtrue/develope
Browse files Browse the repository at this point in the history
optimize performance
  • Loading branch information
ZTFtrue authored Oct 23, 2024
2 parents c3992b2 + ba17ef1 commit 8e24127
Show file tree
Hide file tree
Showing 20 changed files with 408 additions and 719 deletions.
14 changes: 7 additions & 7 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ android {
applicationId = "com.ztftrue.music"
minSdk = 30
targetSdk = 34
versionCode = 38
versionName = "0.1.38"
versionCode = 39
versionName = "0.1.39"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down Expand Up @@ -106,8 +106,8 @@ dependencies {
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.6")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.6")
implementation("androidx.activity:activity-compose:1.9.2")
implementation("androidx.navigation:navigation-compose:2.8.2")
implementation("androidx.activity:activity-compose:1.9.3")
implementation("androidx.navigation:navigation-compose:2.8.3")
implementation("androidx.media3:media3-exoplayer:1.4.1")
implementation ("androidx.media:media:1.7.0")

Expand All @@ -126,7 +126,7 @@ dependencies {
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.2.1")
androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2024.09.03"))
androidTestImplementation(platform("androidx.compose:compose-bom:2024.10.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
Expand All @@ -147,7 +147,7 @@ dependencies {

implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.collection:collection-ktx:1.4.4")
implementation("androidx.fragment:fragment-ktx:1.8.3")
implementation("androidx.fragment:fragment-ktx:1.8.4")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.6")

implementation("androidx.palette:palette-ktx:1.0.0")
Expand All @@ -166,7 +166,7 @@ dependencies {
// optional - Paging 3 Integration
implementation("androidx.room:room-paging:$roomVersion")

implementation("com.google.code.gson:gson:2.10.1")
implementation("com.google.code.gson:gson:2.11.0")

implementation("uk.me.berndporr:iirj:1.7")

Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/com/ztftrue/music/ErrorTipActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.ztftrue.music

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
Expand Down Expand Up @@ -60,9 +61,13 @@ class ErrorTipActivity : ComponentActivity() {
LaunchedEffect(Unit) {
try {
errorMessage = intent.getStringExtra("error") ?: ""
errorMessage= "If possible, please tell me what caused this error to occur, or when the BUG occurs, you are clicking on the button of the app. \n\n $errorMessage"
errorMessage=errorMessage+"\n\nVERSION_CODE "+BuildConfig.VERSION_CODE
errorMessage=errorMessage+"\n\nBrand: "+Build.BRAND
errorMessage=errorMessage+"\n\nAndroidVersion: "+ Build.VERSION.SDK_INT
errorMessage=errorMessage+"\n\n:DEVICE: "+ Build.DEVICE
} catch (e: Exception) {
Log.e("ERROR", e.toString())
// Log.e("ERROR", e.toString())
}
}
BackHandler(enabled = true) {
Expand Down
15 changes: 5 additions & 10 deletions app/src/main/java/com/ztftrue/music/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ import androidx.core.splashscreen.SplashScreen
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import com.ztftrue.music.play.ACTION_IS_CONNECTED
import com.ztftrue.music.play.ACTION_PlayLIST_CHANGE
import com.ztftrue.music.play.ACTION_TRACKS_DELETE
import com.ztftrue.music.play.ACTION_WILL_DISCONNECTED
import com.ztftrue.music.play.EVENT_MEDIA_ITEM_Change
import com.ztftrue.music.play.EVENT_SLEEP_TIME_Change
import com.ztftrue.music.play.EVENT_Visualization_Change
Expand Down Expand Up @@ -371,7 +373,6 @@ class MainActivity : ComponentActivity() {
private fun openAppSettings() {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.setData(Uri.fromParts("package", packageName, null))
// Check if there is an activity that can handle this intent
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
finish()
Expand Down Expand Up @@ -411,6 +412,7 @@ class MainActivity : ComponentActivity() {
connectionCallbacks,
null // optional Bundle
)

setContent {
MusicPitchTheme(musicViewModel) {
BaseLayout(musicViewModel, this@MainActivity)
Expand Down Expand Up @@ -452,10 +454,6 @@ class MainActivity : ComponentActivity() {
compatSplashScreen?.setKeepOnScreenCondition { musicViewModel.mainTabList.isEmpty() }
Utils.initSettingsData(musicViewModel, this)
musicViewModel.prepareArtistAndGenreCover(this@MainActivity)
musicViewModel.musicVisualizationEnable.value =
SharedPreferencesUtils.getEnableMusicVisualization(this@MainActivity)
musicViewModel.showMusicCover.value =
SharedPreferencesUtils.getShowMusicCover(this@MainActivity)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {

if (ActivityCompat.checkSelfPermission(
Expand Down Expand Up @@ -529,6 +527,7 @@ class MainActivity : ComponentActivity() {
}

public override fun onStop() {
musicViewModel.mediaBrowser?.sendCustomAction(ACTION_WILL_DISCONNECTED,null,null)
MediaControllerCompat.getMediaController(this)?.unregisterCallback(callback)
musicViewModel.mediaController = null
mediaBrowser?.disconnect()
Expand Down Expand Up @@ -565,11 +564,6 @@ class MainActivity : ComponentActivity() {
lock.unlock()
}

// Add extension function to MediaControllerCompat.Callback
fun MediaControllerCompat.Callback.onVisualizerChangedExtension(data: FloatArray?) {
println("Handling visualizer data: $data")
}

val callback = object : MediaControllerCompat.Callback() {
override fun onPlaybackStateChanged(state: PlaybackStateCompat?) {
super.onPlaybackStateChanged(state)
Expand Down Expand Up @@ -678,6 +672,7 @@ class MainActivity : ComponentActivity() {
}
MediaControllerCompat.setMediaController(this@MainActivity, mediaController)
mediaController?.registerCallback(callback)
musicViewModel.mediaBrowser?.sendCustomAction(ACTION_IS_CONNECTED,null,null)
}
}

Expand Down
9 changes: 4 additions & 5 deletions app/src/main/java/com/ztftrue/music/PlayMusicWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,12 @@ class PlayMusicWidget : AppWidgetProvider() {
override fun onDeleted(context: Context?, appWidgetIds: IntArray?) {
super.onDeleted(context, appWidgetIds)
appWidgetIds?.forEach { hashMap.remove(it) }

Log.d("PlayMusicWidget", "onAppWidgetOptionsChanged")
// Log.d("PlayMusicWidget", "onAppWidgetOptionsChanged")
}

override fun onRestored(context: Context?, oldWidgetIds: IntArray?, newWidgetIds: IntArray?) {
super.onRestored(context, oldWidgetIds, newWidgetIds)
Log.d("PlayMusicWidget", "onRestored")
// Log.d("PlayMusicWidget", "onRestored")
}

@OptIn(UnstableApi::class)
Expand All @@ -159,7 +158,7 @@ class PlayMusicWidget : AppWidgetProvider() {
@OptIn(UnstableApi::class)
override fun onEnabled(context: Context) {
// TODO send broadcast
Log.d("PlayMusicWidget", "onEnabled")
// Log.d("PlayMusicWidget", "onEnabled")
context.getSharedPreferences("Widgets", Context.MODE_PRIVATE).edit().apply {
putBoolean("enable", true)
apply()
Expand All @@ -174,7 +173,7 @@ class PlayMusicWidget : AppWidgetProvider() {
putBoolean("enable", false)
apply()
}
Log.d("PlayMusicWidget", "onDisabled")
// Log.d("PlayMusicWidget", "onDisabled")
}


Expand Down
71 changes: 31 additions & 40 deletions app/src/main/java/com/ztftrue/music/effects/BiQuadraticFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ final public class BiQuadraticFilter {
final static int LOWSHELF = 5;
final static int HIGHSHELF = 6;
final static int Gain = 7;
private double a0, a1, a2, b0, b1, b2;
private double x1, x2, y, y1, y2;
private double gain_abs;
private float a0, a1, a2, b0, b1, b2;
private float x1, x2, y, y1, y2;
private float gain_abs;
private int type;
BIND_TYPE bindType = BIND_TYPE.Q;

Expand All @@ -26,17 +26,17 @@ enum BIND_TYPE {
S
}

double center_freq, sample_rate, Q, gainDB, bw;
float center_freq, sample_rate, Q, gainDB, bw;

public BiQuadraticFilter() {
}

public BiQuadraticFilter(int type, double center_freq, double sample_rate, double Q, double gainDB) {
public BiQuadraticFilter(int type, float center_freq, float sample_rate, float Q, float gainDB) {
configure(type, center_freq, sample_rate, Q, gainDB);
}

// constructor without gain setting
public BiQuadraticFilter(int type, double center_freq, double sample_rate, double Q) {
public BiQuadraticFilter(int type, float center_freq, float sample_rate, float Q) {
configure(type, center_freq, sample_rate, Q, 0);
}

Expand All @@ -48,55 +48,46 @@ public double frequency() {
return center_freq;
}

public void configure(int type, double center_freq, double sample_rate, double Q, double gainDB) {
public void configure(int type, float center_freq, float sample_rate, float Q, float gainDB) {
reset();
Q = (Q == 0) ? 1e-9 : Q;
Q = (Q == 0f) ? (float) 1e-9 : Q;
this.type = type;
this.sample_rate = sample_rate;
this.Q = Q;
this.gainDB = gainDB;
reconfigure(center_freq);
}

public void configure(int type, double center_freq, double sample_rate, double Q) {
public void configure(int type, float center_freq, float sample_rate, float Q) {
configure(type, center_freq, sample_rate, Q, 0);
}

public void configureBw(int type, BIND_TYPE bindType, double center_freq, double sample_rate, double bw, double gainDB) {
reset();
this.type = type;
this.bindType = bindType;
this.bw = bw;
this.sample_rate = sample_rate;
this.gainDB = gainDB;
this.Q = bw / 60.0;
reconfigure(center_freq);
}


// allow parameter change while running
public void reconfigure(double cf) {
public void reconfigure(float cf) {
center_freq = cf;
// only used for peaking and shelving filters
gain_abs = FastMath.pow(10, gainDB / 40);
gain_abs = (float) FastMath.pow(10, gainDB / 40);
double omega = 2 * FastMath.PI * cf / sample_rate;
double sn = FastMath.sin(omega);
double cs = FastMath.cos(omega);
double alpha = 0.0;
float sn = (float) FastMath.sin(omega);
float cs = (float) FastMath.cos(omega);
float alpha = 0.0f;
if (bindType == (BIND_TYPE.BW)) {
double bwq = bw / 60.0;
float bwq = bw / 60.0f;
double LN2 = 0.69314718055994530942;
alpha = sn * FastMath.sinh(LN2 / 2.0 * bwq * omega / FastMath.sin(omega));
alpha = (float) (sn * FastMath.sinh(LN2 / 2.0 * bwq * omega / FastMath.sin(omega)));
} else if (bindType == BIND_TYPE.Q) {
alpha = sn / (2 * Q);
} else if (bindType == BIND_TYPE.S) {
alpha = sn * FastMath.sqrt((gain_abs + 1.0 / gain_abs) * (1 / Q - 1) + 2) / 2.0;
alpha = (float) (sn * FastMath.sqrt((gain_abs + 1.0 / gain_abs) * (1 / Q - 1) + 2) / 2.0);
}
double beta = FastMath.sqrt(gain_abs + gain_abs);
float beta = (float) FastMath.sqrt(gain_abs + gain_abs);
switch (type) {
case Gain -> {
b0 = gain_abs;
a0 = 1.0;
a1 = a2 = b1 = b2 = 0.0;
a0 = 1.0f;
a1 = a2 = b1 = b2 = 0.0f;
}
case BANDPASS -> {
b0 = alpha;
Expand All @@ -110,15 +101,15 @@ public void reconfigure(double cf) {
b0 = (1 - cs) / 2;
b1 = 1 - cs;
b2 = (1 - cs) / 2;
a0 = 1.0 + alpha;
a1 = -2.0 * cs;
a2 = 1.0 - alpha;
a0 = 1.0f + alpha;
a1 = -2.0f * cs;
a2 = 1.0f - alpha;
}
case HIGHPASS -> {
b0 = b2 = (1 + cs) / 2;
b1 = -(1.0 + cs);
a0 = 1.0 + alpha;
a1 = -2.0 * cs;
b1 = -(1.0f + cs);
a0 = 1.0f + alpha;
a1 = -2.0f * cs;
a2 = 1 - alpha;
}
case NOTCH -> {
Expand All @@ -138,7 +129,7 @@ public void reconfigure(double cf) {
a2 = 1 - (alpha / gain_abs);
}
case LOWSHELF -> {
b0 = gain_abs * ((gain_abs + 1) - (gain_abs - 1) * cs + beta * sn);
b0 = gain_abs * ((gain_abs + 1f) - (gain_abs - 1f) * cs + beta * sn);
b1 = 2 * gain_abs * ((gain_abs - 1) - (gain_abs + 1) * cs);
b2 = gain_abs * ((gain_abs + 1) - (gain_abs - 1) * cs - beta * sn);
a0 = (gain_abs + 1) + (gain_abs - 1) * cs + beta * sn;
Expand Down Expand Up @@ -187,12 +178,12 @@ public double log_result(double f) {
}

// return the constant set for this filter
public double[] constants() {
return new double[]{a1, a2, b0, b1, b2};
public float[] constants() {
return new float[]{a1, a2, b0, b1, b2};
}

// perform one filtering step
public double filter(double x) {
public float filter(float x) {
y = b0 * x + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2;
x2 = x1;
x1 = x;
Expand Down
29 changes: 16 additions & 13 deletions app/src/main/java/com/ztftrue/music/effects/DelayEffect.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
* from <a href="https://github.com/JorenSix/TarsosDSP/blob/master/core/src/main/java/be/tarsos/dsp/effects/DelayEffect.java">TarsosDSP DelayEffect</a>
*/
public class DelayEffect {
private final double sampleRate;
private final float sampleRate;
private float[] echoBuffer;//in seconds
private int position;
private float decay;
private boolean withFeedBack = false;
private boolean withFeedBackDeal = false;
private double newEchoLength;
private float newEchoLength;

/**
* @param echoLength in seconds
* @param sampleRate the sample rate in Hz.
* @param decay The decay of the echo, a value between 0 and 1. 1 meaning no decay, 0 means immediate decay (not echo effect).
*/
public DelayEffect(double echoLength, float decay, double sampleRate) {
public DelayEffect(float echoLength, float decay, float sampleRate) {
this.sampleRate = sampleRate;
setDecay(decay);
setEchoLength(echoLength);
Expand All @@ -27,7 +27,7 @@ public DelayEffect(double echoLength, float decay, double sampleRate) {
/**
* @param newEchoLength A new echo buffer length in seconds.
*/
public void setEchoLength(double newEchoLength) {
public void setEchoLength(float newEchoLength) {
this.newEchoLength = newEchoLength;
}

Expand Down Expand Up @@ -55,10 +55,11 @@ private void applyNewEchoLength() {
* @param newDecay the new decay (preferably between zero and one).
*/
public void setDecay(float newDecay) {
this.decay = newDecay;
this.decay = newDecay;
}

public void process(float[] floatBuffer) {
public float process(float[] floatBuffer) {
float max = 0;
for (int i = 0; i < floatBuffer.length; i++) {
if (position >= echoBuffer.length) {
position = 0;
Expand All @@ -67,19 +68,21 @@ public void process(float[] floatBuffer) {
//output is the input added with the decayed echo
floatBuffer[i] = floatBuffer[i] + echoBuffer[position] * decay;
//store the sample in the buffer;
if (floatBuffer[i] > 1.0f) {
floatBuffer[i] = 1.0f;
} else if (floatBuffer[i] < -1.0f) {
floatBuffer[i] = -1.0f;
}
if(withFeedBackDeal){
// if (floatBuffer[i] > 1.0f) {
// floatBuffer[i] = 1.0f;
// } else if (floatBuffer[i] < -1.0f) {
// floatBuffer[i] = -1.0f;
// }
max = Math.max(max, Math.abs(floatBuffer[i]));
if (withFeedBackDeal) {
echoBuffer[position] = floatBuffer[i];// multiple times, defined delay times
}else{
} else {
echoBuffer[position] = f;// Just once
}
position++;
}
applyNewEchoLength();
return max;
}

public boolean isWithFeedBack() {
Expand Down
Loading

0 comments on commit 8e24127

Please sign in to comment.