Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if volume boost effect is supported on the device #6808

Merged
merged 8 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package de.danoeh.antennapod.preferences;

import android.content.Context;
import android.util.AttributeSet;

import java.util.Arrays;

import de.danoeh.antennapod.model.feed.VolumeAdaptionSetting;

public class VolumeAdaptationPreference extends MaterialListPreference {
public VolumeAdaptationPreference(Context context) {
super(context);
}

public VolumeAdaptationPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public CharSequence[] getEntries() {
if (VolumeAdaptionSetting.isBoostSupported()) {
return super.getEntries();
} else {
return Arrays.copyOfRange(super.getEntries(), 0, 3);
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/xml/feed_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
android:summary="@string/global_default"
android:title="@string/auto_delete_label" />

<de.danoeh.antennapod.preferences.MaterialListPreference
<de.danoeh.antennapod.preferences.VolumeAdaptationPreference
android:defaultValue="off"
android:entries="@array/spnVolumeAdaptationItems"
android:entryValues="@array/spnVolumeAdaptationValues"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.util.Log;
import android.view.SurfaceHolder;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Consumer;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.DefaultLoadControl;
Expand Down Expand Up @@ -37,6 +38,7 @@
import com.google.android.exoplayer2.upstream.HttpDataSource;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.model.feed.VolumeAdaptionSetting;
import de.danoeh.antennapod.storage.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
import de.danoeh.antennapod.core.service.download.HttpCredentialEncoder;
Expand Down Expand Up @@ -69,7 +71,8 @@ public class ExoPlayerWrapper {
private PlaybackParameters playbackParameters;
private DefaultTrackSelector trackSelector;

private LoudnessEnhancer loudnessEnhancer;
@Nullable
private LoudnessEnhancer loudnessEnhancer = null;

ExoPlayerWrapper(Context context) {
this.context = context;
Expand Down Expand Up @@ -247,11 +250,15 @@ public void setPlaybackParams(float speed, boolean skipSilence) {
public void setVolume(float v, float v1) {
if (v > 1) {
exoPlayer.setVolume(1f);
loudnessEnhancer.setEnabled(true);
loudnessEnhancer.setTargetGain((int) (1000 * (v - 1)));
if (loudnessEnhancer != null) {
loudnessEnhancer.setEnabled(true);
loudnessEnhancer.setTargetGain((int) (1000 * (v - 1)));
}
} else {
exoPlayer.setVolume(v);
loudnessEnhancer.setEnabled(false);
if (loudnessEnhancer != null) {
loudnessEnhancer.setEnabled(false);
}
}
}

Expand Down Expand Up @@ -354,6 +361,10 @@ void setOnBufferingUpdateListener(Consumer<Integer> bufferingUpdateListener) {
}

private void initLoudnessEnhancer(int audioStreamId) {
if (!VolumeAdaptionSetting.isBoostSupported()) {
return;
}

LoudnessEnhancer newEnhancer = new LoudnessEnhancer(audioStreamId);
LoudnessEnhancer oldEnhancer = this.loudnessEnhancer;
if (oldEnhancer != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package de.danoeh.antennapod.core.feed;

import de.danoeh.antennapod.model.feed.VolumeAdaptionSetting;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.Matchers.equalTo;
Expand All @@ -11,6 +14,16 @@

public class VolumeAdaptionSettingTest {

@Before
public void setUp() throws Exception {
VolumeAdaptionSetting.setBoostSupported(false);
}

@After
public void tearDown() throws Exception {
VolumeAdaptionSetting.setBoostSupported(null);
}

@Test
public void mapOffToInteger() {
VolumeAdaptionSetting setting = VolumeAdaptionSetting.OFF;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package de.danoeh.antennapod.model.feed;

import android.media.audiofx.AudioEffect;

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

public enum VolumeAdaptionSetting {
OFF(0, 1.0f),
LIGHT_REDUCTION(1, 0.5f),
Expand Down Expand Up @@ -32,4 +37,29 @@ public int toInteger() {
public float getAdaptionFactor() {
return adaptionFactor;
}

@Nullable
private static Boolean boostSupported = null;

public static boolean isBoostSupported() {
if (boostSupported != null) {
return boostSupported;
}
final AudioEffect.Descriptor[] audioEffects = AudioEffect.queryEffects();
if (audioEffects != null) {
for (AudioEffect.Descriptor effect : audioEffects) {
if (effect.type.equals(AudioEffect.EFFECT_TYPE_LOUDNESS_ENHANCER)) {
boostSupported = true;
return boostSupported;
}
}
}
boostSupported = false;
return boostSupported;
}

@VisibleForTesting
public static void setBoostSupported(@Nullable Boolean boostSupported) {
VolumeAdaptionSetting.boostSupported = boostSupported;
}
}