Add volume boost (#6573)

This commit is contained in:
Matej Drobnič 2023-09-08 14:28:20 +02:00 committed by GitHub
parent 9ed5485ae3
commit 8ebf153970
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 141 additions and 31 deletions

View File

@ -73,7 +73,7 @@ public class FeedSettingsTest {
clickPreference(R.string.auto_delete_label);
onView(withText(R.string.cancel_label)).perform(click());
clickPreference(R.string.feed_volume_reduction);
clickPreference(R.string.feed_volume_adapdation);
onView(withText(R.string.cancel_label)).perform(click());
}
}

View File

@ -156,7 +156,7 @@ public class FeedSettingsFragment extends Fragment {
setupAutoDownloadPreference();
setupKeepUpdatedPreference();
setupAutoDeletePreference();
setupVolumeReductionPreferences();
setupVolumeAdaptationPreferences();
setupNewEpisodesAction();
setupAuthentificationPreference();
setupEpisodeFilterPreference();
@ -166,7 +166,7 @@ public class FeedSettingsFragment extends Fragment {
setupTags();
updateAutoDeleteSummary();
updateVolumeReductionValue();
updateVolumeAdaptationValue();
updateAutoDownloadEnabled();
updateNewEpisodesAction();
@ -317,9 +317,9 @@ public class FeedSettingsFragment extends Fragment {
}
}
private void setupVolumeReductionPreferences() {
ListPreference volumeReductionPreference = findPreference("volumeReduction");
volumeReductionPreference.setOnPreferenceChangeListener((preference, newValue) -> {
private void setupVolumeAdaptationPreferences() {
ListPreference volumeAdaptationPreference = findPreference("volumeReduction");
volumeAdaptationPreference.setOnPreferenceChangeListener((preference, newValue) -> {
switch ((String) newValue) {
case "off":
feedPreferences.setVolumeAdaptionSetting(VolumeAdaptionSetting.OFF);
@ -330,28 +330,46 @@ public class FeedSettingsFragment extends Fragment {
case "heavy":
feedPreferences.setVolumeAdaptionSetting(VolumeAdaptionSetting.HEAVY_REDUCTION);
break;
case "light_boost":
feedPreferences.setVolumeAdaptionSetting(VolumeAdaptionSetting.LIGHT_BOOST);
break;
case "medium_boost":
feedPreferences.setVolumeAdaptionSetting(VolumeAdaptionSetting.MEDIUM_BOOST);
break;
case "heavy_boost":
feedPreferences.setVolumeAdaptionSetting(VolumeAdaptionSetting.HEAVY_BOOST);
break;
default:
}
DBWriter.setFeedPreferences(feedPreferences);
updateVolumeReductionValue();
updateVolumeAdaptationValue();
EventBus.getDefault().post(
new VolumeAdaptionChangedEvent(feedPreferences.getVolumeAdaptionSetting(), feed.getId()));
return false;
});
}
private void updateVolumeReductionValue() {
ListPreference volumeReductionPreference = findPreference("volumeReduction");
private void updateVolumeAdaptationValue() {
ListPreference volumeAdaptationPreference = findPreference("volumeReduction");
switch (feedPreferences.getVolumeAdaptionSetting()) {
case OFF:
volumeReductionPreference.setValue("off");
volumeAdaptationPreference.setValue("off");
break;
case LIGHT_REDUCTION:
volumeReductionPreference.setValue("light");
volumeAdaptationPreference.setValue("light");
break;
case HEAVY_REDUCTION:
volumeReductionPreference.setValue("heavy");
volumeAdaptationPreference.setValue("heavy");
break;
case LIGHT_BOOST:
volumeAdaptationPreference.setValue("light_boost");
break;
case MEDIUM_BOOST:
volumeAdaptationPreference.setValue("medium_boost");
break;
case HEAVY_BOOST:
volumeAdaptationPreference.setValue("heavy_boost");
break;
}
}

View File

@ -50,12 +50,12 @@
<de.danoeh.antennapod.preferences.MaterialListPreference
android:defaultValue="off"
android:entries="@array/spnVolumeReductionItems"
android:entryValues="@array/spnVolumeReductionValues"
android:entries="@array/spnVolumeAdaptationItems"
android:entryValues="@array/spnVolumeAdaptationValues"
android:icon="@drawable/ic_volume_adaption"
android:key="volumeReduction"
android:summary="@string/feed_volume_reduction_summary"
android:title="@string/feed_volume_reduction" />
android:summary="@string/feed_volume_adaptation_summary"
android:title="@string/feed_volume_adapdation" />
<de.danoeh.antennapod.preferences.MaterialListPreference
android:entries="@array/feedNewEpisodesActionItems"

View File

@ -1,6 +1,7 @@
package de.danoeh.antennapod.core.service.playback;
import android.content.Context;
import android.media.audiofx.LoudnessEnhancer;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
@ -68,6 +69,8 @@ public class ExoPlayerWrapper {
private PlaybackParameters playbackParameters;
private DefaultTrackSelector trackSelector;
private LoudnessEnhancer loudnessEnhancer;
ExoPlayerWrapper(Context context) {
this.context = context;
createPlayer();
@ -133,7 +136,14 @@ public class ExoPlayerWrapper {
audioSeekCompleteListener.run();
}
}
@Override
public void onAudioSessionIdChanged(int audioSessionId) {
initLoudnessEnhancer(audioSessionId);
}
});
initLoudnessEnhancer(exoPlayer.getAudioSessionId());
}
public int getCurrentPosition() {
@ -235,7 +245,14 @@ public class ExoPlayerWrapper {
}
public void setVolume(float v, float v1) {
exoPlayer.setVolume(v);
if (v > 1) {
exoPlayer.setVolume(1f);
loudnessEnhancer.setEnabled(true);
loudnessEnhancer.setTargetGain((int) (1000 * (v - 1)));
} else {
exoPlayer.setVolume(v);
loudnessEnhancer.setEnabled(false);
}
}
public void start() {
@ -335,4 +352,16 @@ public class ExoPlayerWrapper {
void setOnBufferingUpdateListener(Consumer<Integer> bufferingUpdateListener) {
this.bufferingUpdateListener = bufferingUpdateListener;
}
private void initLoudnessEnhancer(int audioStreamId) {
LoudnessEnhancer newEnhancer = new LoudnessEnhancer(audioStreamId);
LoudnessEnhancer oldEnhancer = this.loudnessEnhancer;
if (oldEnhancer != null) {
newEnhancer.setEnabled(oldEnhancer.getEnabled());
newEnhancer.setTargetGain((int) oldEnhancer.getTargetGain());
oldEnhancer.release();
}
this.loudnessEnhancer = newEnhancer;
}
}

View File

@ -13,16 +13,22 @@
<item>never</item>
</string-array>
<string-array name="spnVolumeReductionItems">
<item>@string/feed_volume_reduction_off</item>
<item>@string/feed_volume_reduction_light</item>
<string-array name="spnVolumeAdaptationItems">
<item>@string/feed_volume_reduction_heavy</item>
<item>@string/feed_volume_reduction_light</item>
<item>@string/feed_volume_reduction_off</item>
<item>@string/feed_volume_boost_light</item>
<item>@string/feed_volume_boost_medium</item>
<item>@string/feed_volume_boost_heavy</item>
</string-array>
<string-array name="spnVolumeReductionValues">
<item>off</item>
<item>light</item>
<string-array name="spnVolumeAdaptationValues">
<item>heavy</item>
<item>light</item>
<item>off</item>
<item>light_boost</item>
<item>medium_boost</item>
<item>heavy_boost</item>
</string-array>
<string-array name="feed_refresh_interval_entries">

View File

@ -31,11 +31,35 @@ public class VolumeAdaptionSettingTest {
assertThat(setting.toInteger(), is(equalTo(2)));
}
@Test
public void mapLightBoostToInteger() {
VolumeAdaptionSetting setting = VolumeAdaptionSetting.LIGHT_BOOST;
assertThat(setting.toInteger(), is(equalTo(3)));
}
@Test
public void mapMediumBoostToInteger() {
VolumeAdaptionSetting setting = VolumeAdaptionSetting.MEDIUM_BOOST;
assertThat(setting.toInteger(), is(equalTo(4)));
}
@Test
public void mapHeavyBoostToInteger() {
VolumeAdaptionSetting setting = VolumeAdaptionSetting.HEAVY_BOOST;
assertThat(setting.toInteger(), is(equalTo(5)));
}
@Test
public void mapIntegerToVolumeAdaptionSetting() {
assertThat(VolumeAdaptionSetting.fromInteger(0), is(equalTo(VolumeAdaptionSetting.OFF)));
assertThat(VolumeAdaptionSetting.fromInteger(1), is(equalTo(VolumeAdaptionSetting.LIGHT_REDUCTION)));
assertThat(VolumeAdaptionSetting.fromInteger(2), is(equalTo(VolumeAdaptionSetting.HEAVY_REDUCTION)));
assertThat(VolumeAdaptionSetting.fromInteger(3), is(equalTo(VolumeAdaptionSetting.LIGHT_BOOST)));
assertThat(VolumeAdaptionSetting.fromInteger(4), is(equalTo(VolumeAdaptionSetting.MEDIUM_BOOST)));
assertThat(VolumeAdaptionSetting.fromInteger(5), is(equalTo(VolumeAdaptionSetting.HEAVY_BOOST)));
}
@Test(expected = IllegalArgumentException.class)
@ -45,7 +69,7 @@ public class VolumeAdaptionSettingTest {
@Test(expected = IllegalArgumentException.class)
public void cannotMapValuesOutOfRange() {
VolumeAdaptionSetting.fromInteger(3);
VolumeAdaptionSetting.fromInteger(6);
}
@Test
@ -62,4 +86,31 @@ public class VolumeAdaptionSettingTest {
assertTrue("Light reduction must have higher factor than heavy reduction", lightReductionFactor > heavyReductionFactor);
}
}
@Test
public void lightBoostYieldsHigherValueThanLightReduction() {
float lightReductionFactor = VolumeAdaptionSetting.LIGHT_REDUCTION.getAdaptionFactor();
float lightBoostFactor = VolumeAdaptionSetting.LIGHT_BOOST.getAdaptionFactor();
assertTrue("Light boost must have higher factor than light reduction", lightBoostFactor > lightReductionFactor);
}
@Test
public void mediumBoostYieldsHigherValueThanLightBoost() {
float lightBoostFactor = VolumeAdaptionSetting.LIGHT_BOOST.getAdaptionFactor();
float mediumBoostFactor = VolumeAdaptionSetting.MEDIUM_BOOST.getAdaptionFactor();
assertTrue("Medium boost must have higher factor than light boost", mediumBoostFactor > lightBoostFactor);
}
@Test
public void heavyBoostYieldsHigherValueThanMediumBoost() {
float mediumBoostFactor = VolumeAdaptionSetting.MEDIUM_BOOST.getAdaptionFactor();
float heavyBoostFactor = VolumeAdaptionSetting.HEAVY_BOOST.getAdaptionFactor();
assertTrue("Heavy boost must have higher factor than medium boost", heavyBoostFactor > mediumBoostFactor);
}
}

View File

@ -3,7 +3,10 @@ package de.danoeh.antennapod.model.feed;
public enum VolumeAdaptionSetting {
OFF(0, 1.0f),
LIGHT_REDUCTION(1, 0.5f),
HEAVY_REDUCTION(2, 0.2f);
HEAVY_REDUCTION(2, 0.2f),
LIGHT_BOOST(3, 1.5f),
MEDIUM_BOOST(4, 2f),
HEAVY_BOOST(5, 2.5f);
private final int value;
private float adaptionFactor;

View File

@ -124,11 +124,14 @@
<string name="retry_label">Retry</string>
<string name="auto_download_label">Include in auto downloads</string>
<string name="auto_delete_label">Auto delete episode</string>
<string name="feed_volume_reduction">Volume reduction</string>
<string name="feed_volume_reduction_summary">Turn down volume for episodes of this podcast: %1$s</string>
<string name="feed_volume_reduction_off">Off</string>
<string name="feed_volume_reduction_light">Light</string>
<string name="feed_volume_reduction_heavy">Heavy</string>
<string name="feed_volume_adapdation">Volume adaptation</string>
<string name="feed_volume_adaptation_summary">Turn volume for episodes of this podcast up or down: %1$s</string>
<string name="feed_volume_reduction_off">No adaption</string>
<string name="feed_volume_reduction_light">Light reduction</string>
<string name="feed_volume_reduction_heavy">Heavy reduction</string>
<string name="feed_volume_boost_light">Light boost</string>
<string name="feed_volume_boost_medium">Medium boost</string>
<string name="feed_volume_boost_heavy">Heavy boost</string>
<string name="feed_auto_download_always">Always</string>
<string name="feed_auto_download_never">Never</string>
<string name="feed_new_episodes_action_add_to_inbox">Add to inbox</string>