Skip to content

Commit

Permalink
equalizer bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearfog committed Aug 22, 2023
1 parent abb81bb commit a92a08f
Show file tree
Hide file tree
Showing 22 changed files with 182 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import androidx.core.app.NotificationManagerCompat;

import org.nuclearfog.apollo.cache.ImageCache;
import org.nuclearfog.apollo.player.AudioEffects;

import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -54,10 +55,12 @@ public void onLowMemory() {
*/
@Override
public void onTerminate() {
// remove notification
try {
// remove notification
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.cancelAll();
// release audioeffects
AudioEffects.release();
} catch (SecurityException exception) {
// ignore
}
Expand Down
177 changes: 130 additions & 47 deletions app/src/main/java/org/nuclearfog/apollo/player/AudioEffects.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,32 @@ public static AudioEffects getInstance(Context context, int sessionId) {
if (instance == null || instance.sessionId != sessionId) {
instance = new AudioEffects(context, sessionId);
if (BuildConfig.DEBUG) {
Log.v(TAG, "session_id=" + sessionId);
Log.d(TAG, "audio_session_id=" + sessionId);
}
}
return instance;
} catch (Exception e) {
// thrown if there is no support for audio effects
if (BuildConfig.DEBUG) {
e.printStackTrace();
}
return null;
}
return instance;
}

/**
* release all audioeffects from usage
*/
public static void release() {
if (instance != null) {
try {
instance.equalizer.release();
instance.bassBooster.release();
instance.reverb.release();
} catch (RuntimeException exception) {
if (BuildConfig.DEBUG) {
exception.printStackTrace();
}
}
}
}

Expand All @@ -87,40 +103,55 @@ private AudioEffects(Context context, int sessionId) {

equalizer.setEnabled(active);
bassBooster.setEnabled(active);
reverb.setEnabled(active);
if (active) {
setEffectValues();
}
}

/**
* @return true if audio FX is enabled
*/
public boolean isAudioFxEnabled() {
return prefs.isAudioFxEnabled();
}

/**
* enable/disable audio effects
*
* @param enable true to enable all audio effects
*/
public void enableAudioFx(boolean enable) {
equalizer.setEnabled(enable);
bassBooster.setEnabled(enable);
prefs.setAudioFxEnabled(enable);
if (enable) {
setEffectValues();
try {
equalizer.setEnabled(enable);
bassBooster.setEnabled(enable);
reverb.setEnabled(enable);
prefs.setAudioFxEnabled(enable);
if (enable) {
setEffectValues();
}
} catch (RuntimeException exception) {
if (BuildConfig.DEBUG) {
exception.printStackTrace();
}
}
}

/**
* @return true if audio FX is enabled
*/
public boolean isAudioFxEnabled() {
return prefs.isAudioFxEnabled();
}

/**
* get min, max limits of the eq band
*
* @return array with min and max limits
*/
public int[] getBandLevelRange() {
short[] ranges = equalizer.getBandLevelRange();
return new int[]{ranges[0], ranges[1]};
try {
short[] ranges = equalizer.getBandLevelRange();
return new int[]{ranges[0], ranges[1]};
} catch (RuntimeException exception) {
if (BuildConfig.DEBUG) {
exception.printStackTrace();
}
}
return new int[2];
}

/**
Expand All @@ -129,12 +160,19 @@ public int[] getBandLevelRange() {
* @return array of band frequencies, starting with the lowest frequency
*/
public int[] getBandFrequencies() {
short bandCount = equalizer.getNumberOfBands();
int[] freq = new int[bandCount];
for (short i = 0; i < bandCount; i++) {
freq[i] = equalizer.getCenterFreq(i) / 1000;
try {
short bandCount = equalizer.getNumberOfBands();
int[] freq = new int[bandCount];
for (short i = 0; i < bandCount; i++) {
freq[i] = equalizer.getCenterFreq(i) / 1000;
}
return freq;
} catch (RuntimeException exception) {
if (BuildConfig.DEBUG) {
exception.printStackTrace();
}
}
return freq;
return new int[0];
}

/**
Expand All @@ -143,12 +181,19 @@ public int[] getBandFrequencies() {
* @return array of band levels and frequencies starting from the lowest equalizer frequency
*/
public int[] getBandLevel() {
short bandCount = equalizer.getNumberOfBands();
int[] level = new int[bandCount];
for (short i = 0; i < bandCount; i++) {
level[i] = equalizer.getBandLevel(i);
try {
short bandCount = equalizer.getNumberOfBands();
int[] level = new int[bandCount];
for (short i = 0; i < bandCount; i++) {
level[i] = equalizer.getBandLevel(i);
}
return level;
} catch (RuntimeException exception) {
if (BuildConfig.DEBUG) {
exception.printStackTrace();
}
}
return level;
return new int[0];
}

/**
Expand All @@ -158,15 +203,21 @@ public int[] getBandLevel() {
* @param level level of the band
*/
public void setBandLevel(int band, int level) {
// set single band level
equalizer.setBandLevel((short) band, (short) level);
// save all equalizer band levels
short bandCount = equalizer.getNumberOfBands();
int[] bands = new int[bandCount];
for (short i = 0; i < bandCount; i++) {
bands[i] = equalizer.getBandLevel(i);
try {
// set single band level
equalizer.setBandLevel((short) band, (short) level);
// save all equalizer band levels
short bandCount = equalizer.getNumberOfBands();
int[] bands = new int[bandCount];
for (short i = 0; i < bandCount; i++) {
bands[i] = equalizer.getBandLevel(i);
}
prefs.setEqualizerBands(bands);
} catch (RuntimeException exception) {
if (BuildConfig.DEBUG) {
exception.printStackTrace();
}
}
prefs.setEqualizerBands(bands);
}

/**
Expand All @@ -175,7 +226,14 @@ public void setBandLevel(int band, int level) {
* @return bassbost strength value from 0 to 1000
*/
public int getBassLevel() {
return bassBooster.getRoundedStrength();
try {
return bassBooster.getRoundedStrength();
} catch (RuntimeException exception) {
if (BuildConfig.DEBUG) {
exception.printStackTrace();
}
}
return 0;
}

/**
Expand All @@ -184,8 +242,14 @@ public int getBassLevel() {
* @param level bassbost strength value from 0 to 1000
*/
public void setBassLevel(int level) {
bassBooster.setStrength((short) level);
prefs.setBassLevel(level);
try {
bassBooster.setStrength((short) level);
prefs.setBassLevel(level);
} catch (RuntimeException exception) {
if (BuildConfig.DEBUG) {
exception.printStackTrace();
}
}
}

/**
Expand All @@ -194,7 +258,14 @@ public void setBassLevel(int level) {
* @return reverb level
*/
public int getReverbLevel() {
return reverb.getPreset();
try {
return reverb.getPreset();
} catch (RuntimeException exception) {
if (BuildConfig.DEBUG) {
exception.printStackTrace();
}
}
return 0;
}

/**
Expand All @@ -203,20 +274,32 @@ public int getReverbLevel() {
* @param level reverb level
*/
public void setReverbLevel(int level) {
reverb.setPreset((short) level);
prefs.setReverbLevel(level);
try {
reverb.setPreset((short) level);
prefs.setReverbLevel(level);
} catch (RuntimeException exception) {
if (BuildConfig.DEBUG) {
exception.printStackTrace();
}
}
}

/**
* set saved values for audio effects
*/
private void setEffectValues() {
// setup audio effects
bassBooster.setStrength((short) prefs.getBassLevel());
reverb.setPreset((short) prefs.getReverbLevel());
int[] bandLevel = prefs.getEqualizerBands();
for (short i = 0; i < bandLevel.length; i++) {
equalizer.setBandLevel(i, (short) bandLevel[i]);
try {
// setup audio effects
bassBooster.setStrength((short) prefs.getBassLevel());
reverb.setPreset((short) prefs.getReverbLevel());
int[] bandLevel = prefs.getEqualizerBands();
for (short i = 0; i < bandLevel.length; i++) {
equalizer.setBandLevel(i, (short) bandLevel[i]);
}
} catch (RuntimeException exception) {
if (BuildConfig.DEBUG) {
exception.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -430,17 +430,17 @@ else if (item.getItemId() == R.id.menu_shuffle) {
switch (type) {
case ARTIST:
long[] list = MusicUtils.getSongListForArtist(this, ids[0]);
MusicUtils.playAll(list, 0, true);
MusicUtils.playAll(getApplicationContext(), list, 0, true);
break;

case ALBUM:
list = MusicUtils.getSongListForAlbum(this, ids[0]);
MusicUtils.playAll(list, 0, true);
MusicUtils.playAll(getApplicationContext(), list, 0, true);
break;

case GENRE:
list = MusicUtils.getSongListForGenres(this, ids);
MusicUtils.playAll(list, 0, true);
MusicUtils.playAll(getApplicationContext(), list, 0, true);
break;

case PLAYLIST:
Expand All @@ -463,7 +463,7 @@ else if (item.getItemId() == R.id.menu_shuffle) {
list = MusicUtils.getSongListForFolder(this, folderName);
if (list.length > 0) {
// play list at random position
MusicUtils.playAll(list, r.nextInt(list.length - 1), true);
MusicUtils.playAll(getApplicationContext(), list, r.nextInt(list.length - 1), true);
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public boolean onContextItemSelected(@NonNull MenuItem item) {
}
switch (item.getItemId()) {
case ContextMenuItems.PLAY_SELECTION:
MusicUtils.playAll(ids, 0, false);
MusicUtils.playAll(getApplicationContext(), ids, 0, false);
return true;

case ContextMenuItems.ADD_TO_QUEUE:
Expand Down Expand Up @@ -362,7 +362,7 @@ else if (music instanceof Album) {
else if (music instanceof Song) {
Song song = (Song) music;
long[] list = new long[]{song.getId()};
MusicUtils.playAll(list, 0, false);
MusicUtils.playAll(getApplicationContext(), list, 0, false);
}
// All done
finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ private void allDone() {
boolean shouldOpenAudioPlayer = mIntent.getBooleanExtra(OPEN_AUDIO_PLAYER, true);
// Play the list
if (mList.length > 0) {
MusicUtils.playAll(mList, 0, mShouldShuffle);
MusicUtils.playAll(getApplicationContext(), mList, 0, mShouldShuffle);
}
// Open the now playing screen
if (shouldOpenAudioPlayer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public boolean onContextItemSelected(@NonNull MenuItem item) {
long[] mAlbumList = MusicUtils.getSongListForAlbum(requireContext(), mAlbum.getId());
switch (item.getItemId()) {
case ContextMenuItems.PLAY_SELECTION:
MusicUtils.playAll(mAlbumList, 0, false);
MusicUtils.playAll(requireContext(), mAlbumList, 0, false);
return true;

case ContextMenuItems.ADD_TO_QUEUE:
Expand Down Expand Up @@ -261,7 +261,7 @@ public void onScrollStateChanged(AbsListView view, int scrollState) {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (view.getId() == R.id.image) {
long[] list = MusicUtils.getSongListForAlbum(getContext(), id);
MusicUtils.playAll(list, 0, false);
MusicUtils.playAll(requireContext(), list, 0, false);
} else {
Album selectedAlbum = mAdapter.getItem(position);
if (selectedAlbum != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public boolean onContextItemSelected(@NonNull MenuItem item) {
long[] mArtistList = MusicUtils.getSongListForArtist(requireContext(), mArtist.getId());
switch (item.getItemId()) {
case ContextMenuItems.PLAY_SELECTION:
MusicUtils.playAll(mArtistList, 0, true);
MusicUtils.playAll(requireContext(), mArtistList, 0, true);
return true;

case ContextMenuItems.ADD_TO_QUEUE:
Expand Down Expand Up @@ -257,7 +257,7 @@ public void onScrollStateChanged(AbsListView view, int scrollState) {
public void onItemClick(AdapterView<?> parent, @NonNull View view, int position, long id) {
if (view.getId() == R.id.image) {
long[] list = MusicUtils.getSongListForArtist(getContext(), id);
MusicUtils.playAll(list, 0, false);
MusicUtils.playAll(requireContext(), list, 0, false);
} else {
Artist selectedArtist = mAdapter.getItem(position);
if (selectedArtist != null) {
Expand Down
Loading

0 comments on commit a92a08f

Please sign in to comment.