Skip to content
Snippets Groups Projects
Commit 14b9cfea authored by Robert Stone's avatar Robert Stone Committed by Nicolas Pomepuy
Browse files

Add preferences to configure replay gain

Fixes #1912
parent 2eb4b5a2
No related branches found
No related tags found
1 merge request!1322Add preferences to configure replay gain
......@@ -141,6 +141,16 @@ object VLCOptions {
options.add("--hrtf-file")
options.add(hstfPath)
}
val replayGainMode = pref.getString("audio-replay-gain-mode", "none")
if (replayGainMode != "none") {
options.add("--audio-replay-gain-mode=$replayGainMode")
options.add("--audio-replay-gain-preamp=${pref.getString("audio-replay-gain-preamp", "0.0")}")
options.add("--audio-replay-gain-default=${pref.getString("audio-replay-gain-default", "-7.0")}")
if (pref.getBoolean("audio-replay-gain-peak-protection", true))
options.add("--audio-replay-gain-peak-protection")
else
options.add("--no-audio-replay-gain-peak-protection")
}
val soundFontFile = getSoundFontFile(context)
if (soundFontFile.exists()) {
options.add("--soundfont=${soundFontFile.path}")
......
......@@ -352,6 +352,17 @@
<item>Windows-1258</item>
</string-array>
<string-array name="replaygain">
<item>@string/replaygain_none</item>
<item>@string/replaygain_track</item>
<item>@string/replaygain_album</item>
</string-array>
<string-array name="replaygain_values" translatable="false">
<item>none</item>
<item>track</item>
<item>album</item>
</string-array>
<string-array name="aouts">
<item>@string/aout_audiotrack</item>
<item>@string/aout_opensles</item>
......
......@@ -382,6 +382,18 @@
<string name="enable_headset_detection_summary">Detect headset insertion and removal</string>
<string name="enable_play_on_headset_insertion">Resume on headset insertion</string>
<string name="enable_play_on_headset_insertion_summary">Pause otherwise</string>
<string name="replaygain_prefs_category">Replay Gain</string>
<string name="replaygain_mode">Replay gain mode</string>
<string name="replaygain_mode_summary">Track mode plays streams with replay gain information at the same loudness. Album mode preserves relative stream loudness on the same album.</string>
<string name="replaygain_preamp">Replay preamp</string>
<string name="replaygain_preamp_summary">This allows you to change the default target level (89 dB) for streams with replay gain information</string>
<string name="replaygain_default">Default replay gain</string>
<string name="replaygain_default_summary">This is the gain used for streams without replay gain information</string>
<string name="replaygain_peak_protection">Peak protection</string>
<string name="replaygain_peak_protection_summary">Protect against sound clipping</string>
<string name="replaygain_none">None</string>
<string name="replaygain_track">Track</string>
<string name="replaygain_album">Album</string>
<string name="aout">Audio output</string>
<string name="aout_summary">Change the method that VLC uses to output audio</string>
<string name="aout_audiotrack" translatable="false">AudioTrack</string>
......
......@@ -26,7 +26,12 @@ import android.annotation.TargetApi
import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle
import android.text.InputFilter
import android.text.InputType
import android.util.Log
import androidx.core.content.edit
import androidx.preference.CheckBoxPreference
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
import androidx.preference.Preference
import kotlinx.coroutines.ExperimentalCoroutinesApi
......@@ -41,6 +46,9 @@ import org.videolan.tools.Settings
import org.videolan.vlc.BuildConfig
import org.videolan.vlc.R
import org.videolan.vlc.util.LocaleUtil
import java.text.DecimalFormat
private const val TAG = "VLC/PreferencesAudio"
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
......@@ -79,6 +87,20 @@ class PreferencesAudio : BasePreferenceFragment(), SharedPreferences.OnSharedPre
prepareLocaleList()
}
override fun onDisplayPreferenceDialog(preference: Preference) {
val f = super.buildPreferenceDialogFragment(preference)
if (f is CustomEditTextPreferenceDialogFragment) {
when (preference.key) {
"audio-replay-gain-preamp", "audio-replay-gain-default" -> {
f.setFilters(arrayOf(InputFilter.LengthFilter(6)))
f.setInputType(InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_NUMBER_FLAG_SIGNED)
}
}
return
}
super.onDisplayPreferenceDialog(preference)
}
private fun updatePreferredAudioTrack() {
val value = Settings.getInstance(activity).getString("audio_preferred_language", null)
if (value.isNullOrEmpty())
......@@ -101,17 +123,40 @@ class PreferencesAudio : BasePreferenceFragment(), SharedPreferences.OnSharedPre
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
when (key) {
"aout" -> {
VLCInstance.restart()
if (activity != null) (activity as PreferencesActivity).restartMediaPlayer()
restartLibVLC()
val opensles = "1" == preferenceManager.sharedPreferences.getString("aout", "0")
if (opensles) findPreference<CheckBoxPreference>("audio_digital_output")?.isChecked = false
findPreference<Preference>("audio_digital_output")?.isVisible = !opensles
}
"audio_digital_output" -> updatePassThroughSummary()
"audio_preferred_language" -> updatePreferredAudioTrack()
"audio-replay-gain-mode", "audio-replay-gain-peak-protection" -> restartLibVLC()
"audio-replay-gain-default", "audio-replay-gain-preamp" -> {
val defValue = if (key == "audio-replay-gain-default") "-7.0" else "0.0"
val newValue = sharedPreferences.getString(key, defValue)
var fmtValue = defValue
try {
fmtValue = DecimalFormat("###0.0###").format(newValue?.toDouble())
} catch (e: IllegalArgumentException) {
Log.w(TAG, "Could not parse value: $newValue. Setting $key to $fmtValue", e)
} finally {
if (fmtValue != newValue) {
sharedPreferences.edit {
// putString will trigger another preference change event. Restart libVLC when it settles.
putString(key, fmtValue)
findPreference<EditTextPreference>(key)?.let { it.text = fmtValue }
}
} else restartLibVLC()
}
}
}
}
private fun restartLibVLC() {
VLCInstance.restart()
if (activity != null) (activity as PreferencesActivity).restartMediaPlayer()
}
private fun prepareLocaleList() {
val localePair = LocaleUtils.getLocalesUsedInProject(activity, BuildConfig.TRANSLATION_ARRAY, getString(R.string.no_track_preference))
preferredAudioTrack.entries = localePair.localeEntries
......
......@@ -70,6 +70,31 @@
android:title="@string/enable_android_auto_seek_buttons"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/replaygain_prefs_category">
<ListPreference
android:defaultValue="none"
android:entries="@array/replaygain"
android:entryValues="@array/replaygain_values"
android:key="audio-replay-gain-mode"
android:summary="@string/replaygain_mode_summary"
android:title="@string/replaygain_mode"/>
<EditTextPreference
android:defaultValue="0.0"
android:key="audio-replay-gain-preamp"
android:summary="@string/replaygain_preamp_summary"
android:title="@string/replaygain_preamp"/>
<EditTextPreference
android:defaultValue="-7.0"
android:key="audio-replay-gain-default"
android:summary="@string/replaygain_default_summary"
android:title="@string/replaygain_default"/>
<CheckBoxPreference
app:singleLineTitle="false"
android:defaultValue="true"
android:key="audio-replay-gain-peak-protection"
android:summary="@string/replaygain_peak_protection_summary"
android:title="@string/replaygain_peak_protection"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/advanced_prefs_category">
<CheckBoxPreference
app:singleLineTitle="false"
......
......@@ -26,11 +26,12 @@ import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
import android.os.Bundle
import android.text.InputFilter
import android.text.InputType
import android.util.Log
import androidx.core.content.edit
import androidx.lifecycle.lifecycleScope
import androidx.preference.CheckBoxPreference
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.TwoStatePreference
import androidx.preference.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.launch
......@@ -51,7 +52,9 @@ import org.videolan.vlc.gui.helpers.UiTools
import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.providers.PickerType
import org.videolan.vlc.util.LocaleUtil
import java.text.DecimalFormat
private const val TAG = "VLC/PreferencesAudio"
private const val FILE_PICKER_RESULT_CODE = 10000
@ExperimentalCoroutinesApi
......@@ -76,6 +79,13 @@ class PreferencesAudio : BasePreferenceFragment(), SharedPreferences.OnSharedPre
updatePassThroughSummary()
val opensles = "1" == preferenceManager.sharedPreferences.getString("aout", "0")
if (opensles) findPreference<Preference>("audio_digital_output")?.isVisible = false
for (key in arrayOf("audio-replay-gain-preamp", "audio-replay-gain-default")) {
findPreference<EditTextPreference>(key)?.setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_NUMBER_FLAG_SIGNED
it.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(6))
it.setSelection(it.editableText.length)
}
}
preferredAudioTrack = findPreference("audio_preferred_language")!!
updatePreferredAudioTrack()
prepareLocaleList()
......@@ -138,17 +148,40 @@ class PreferencesAudio : BasePreferenceFragment(), SharedPreferences.OnSharedPre
val activity = activity ?: return
when (key) {
"aout" -> {
VLCInstance.restart()
(activity as PreferencesActivity).restartMediaPlayer()
restartLibVLC()
val opensles = "1" == preferenceManager.sharedPreferences.getString("aout", "0")
if (opensles) findPreference<CheckBoxPreference>("audio_digital_output")?.isChecked = false
findPreference<Preference>("audio_digital_output")?.isVisible = !opensles
}
"audio_digital_output" -> updatePassThroughSummary()
"audio_preferred_language" -> updatePreferredAudioTrack()
"audio-replay-gain-mode", "audio-replay-gain-peak-protection" -> restartLibVLC()
"audio-replay-gain-default", "audio-replay-gain-preamp" -> {
val defValue = if (key == "audio-replay-gain-default") "-7.0" else "0.0"
val newValue = sharedPreferences.getString(key, defValue)
var fmtValue = defValue
try {
fmtValue = DecimalFormat("###0.0###").format(newValue?.toDouble())
} catch (e: IllegalArgumentException) {
Log.w(TAG, "Could not parse value: $newValue. Setting $key to $fmtValue", e)
} finally {
if (fmtValue != newValue) {
sharedPreferences.edit {
// putString will trigger another preference change event. Restart libVLC when it settles.
putString(key, fmtValue)
findPreference<EditTextPreference>(key)?.let { it.text = fmtValue }
}
} else restartLibVLC()
}
}
}
}
private fun restartLibVLC() {
VLCInstance.restart()
(activity as? PreferencesActivity)?.restartMediaPlayer()
}
private fun prepareLocaleList() {
val localePair = LocaleUtils.getLocalesUsedInProject(requireActivity(), BuildConfig.TRANSLATION_ARRAY, getString(R.string.no_track_preference))
preferredAudioTrack.entries = localePair.localeEntries
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment