diff options
author | Matej Drobnič <services+github@matejdro.com> | 2023-09-08 14:28:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-08 13:28:20 +0100 |
commit | 8ebf1539705aa2251f0e7f795e72bb214e51750b (patch) | |
tree | 3675e58a12cc70a2a9aa9c6d0e79befa08abd7b0 /core/src | |
parent | 9ed5485ae353c0cb3c8b2f6a97f2e905d77afd71 (diff) | |
download | AntennaPod-8ebf1539705aa2251f0e7f795e72bb214e51750b.zip |
Add volume boost (#6573)
Diffstat (limited to 'core/src')
3 files changed, 95 insertions, 9 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java index e0c5da00b..c275444d9 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java @@ -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; + } } diff --git a/core/src/main/res/values/arrays.xml b/core/src/main/res/values/arrays.xml index 7749c6f3c..50afe630b 100644 --- a/core/src/main/res/values/arrays.xml +++ b/core/src/main/res/values/arrays.xml @@ -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"> diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java index 4241707bc..30767bdc8 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java @@ -32,10 +32,34 @@ public class VolumeAdaptionSettingTest { } @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); } -}
\ No newline at end of file + + @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); + } +} |