mirror of
https://github.com/kalarina-bit/Fogwave.git
synced 2026-11-22 08:06:09 +00:00
Cache equalizer state instead of querying audioserver on every slider move
This commit is contained in:
@@ -31,6 +31,8 @@ data class AudioEffectsState(
|
|||||||
val dynamicsProcessingEnabled: Boolean = false
|
val dynamicsProcessingEnabled: Boolean = false
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Effect parameters are cached here: every getter on an AudioEffect is a call into audioserver,
|
||||||
|
// and the state is rebuilt on every frame while an equalizer slider is dragged.
|
||||||
class AudioEffectsEngine(context: Context, audioSessionId: Int) {
|
class AudioEffectsEngine(context: Context, audioSessionId: Int) {
|
||||||
private val prefs: SharedPreferences =
|
private val prefs: SharedPreferences =
|
||||||
context.applicationContext.getSharedPreferences("fogwave_audio_effects", Context.MODE_PRIVATE)
|
context.applicationContext.getSharedPreferences("fogwave_audio_effects", Context.MODE_PRIVATE)
|
||||||
@@ -42,37 +44,53 @@ class AudioEffectsEngine(context: Context, audioSessionId: Int) {
|
|||||||
private var loudnessEnhancer: LoudnessEnhancer? = null
|
private var loudnessEnhancer: LoudnessEnhancer? = null
|
||||||
private var dynamicsProcessing: DynamicsProcessing? = null
|
private var dynamicsProcessing: DynamicsProcessing? = null
|
||||||
|
|
||||||
private val presetNames = mutableListOf<String>()
|
private var presetNames: List<String> = emptyList()
|
||||||
|
private var centerFreqsHz = IntArray(0)
|
||||||
|
private var bandLevels = IntArray(0)
|
||||||
|
private var minBandLevel = 0
|
||||||
|
private var maxBandLevel = 0
|
||||||
|
|
||||||
|
private var equalizerEnabled = prefs.getBoolean(KEY_EQ_ENABLED, false)
|
||||||
|
private var loudnessEnabled = prefs.getBoolean(KEY_LOUDNESS_ENABLED, false)
|
||||||
|
private var loudnessGain = prefs.getInt(KEY_LOUDNESS_GAIN, 0).coerceIn(0, MAX_LOUDNESS_GAIN)
|
||||||
|
private var dynamicsEnabled = prefs.getBoolean(KEY_DYNAMICS_ENABLED, false)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
try {
|
try {
|
||||||
equalizer = Equalizer(0, audioSessionId).apply {
|
equalizer = Equalizer(0, audioSessionId).apply {
|
||||||
for (p in 0 until numberOfPresets) {
|
presetNames = (0 until numberOfPresets).map { getPresetName(it.toShort()) }
|
||||||
presetNames.add(getPresetName(p.toShort()))
|
|
||||||
}
|
val bandCount = numberOfBands.toInt()
|
||||||
|
centerFreqsHz = IntArray(bandCount) { getCenterFreq(it.toShort()) / 1000 }
|
||||||
|
val range = bandLevelRange
|
||||||
|
minBandLevel = range[0].toInt()
|
||||||
|
maxBandLevel = range[1].toInt()
|
||||||
|
|
||||||
val preset = currentPresetIndex
|
val preset = currentPresetIndex
|
||||||
if (preset in 0 until numberOfPresets) {
|
if (preset in 0 until numberOfPresets) {
|
||||||
usePreset(preset.toShort())
|
usePreset(preset.toShort())
|
||||||
} else {
|
} else {
|
||||||
for (b in 0 until numberOfBands) {
|
for (b in 0 until bandCount) {
|
||||||
val saved = prefs.getInt(KEY_BAND_PREFIX + b, Int.MIN_VALUE)
|
val saved = prefs.getInt(KEY_BAND_PREFIX + b, Int.MIN_VALUE)
|
||||||
if (saved != Int.MIN_VALUE) setBandLevel(b.toShort(), saved.toShort())
|
if (saved != Int.MIN_VALUE) setBandLevel(b.toShort(), saved.toShort())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
enabled = prefs.getBoolean(KEY_EQ_ENABLED, false)
|
bandLevels = IntArray(bandCount) { getBandLevel(it.toShort()).toInt() }
|
||||||
|
enabled = equalizerEnabled
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "Equalizer unavailable", e)
|
Log.e(TAG, "Equalizer unavailable", e)
|
||||||
|
equalizer = null
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
loudnessEnhancer = LoudnessEnhancer(audioSessionId).apply {
|
loudnessEnhancer = LoudnessEnhancer(audioSessionId).apply {
|
||||||
setTargetGain(prefs.getInt(KEY_LOUDNESS_GAIN, 0))
|
setTargetGain(loudnessGain)
|
||||||
enabled = prefs.getBoolean(KEY_LOUDNESS_ENABLED, false)
|
enabled = loudnessEnabled
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "LoudnessEnhancer unavailable", e)
|
Log.e(TAG, "LoudnessEnhancer unavailable", e)
|
||||||
|
loudnessEnhancer = null
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||||
@@ -134,94 +152,103 @@ class AudioEffectsEngine(context: Context, audioSessionId: Int) {
|
|||||||
dp.setLimiterByChannelIndex(ch, limiter)
|
dp.setLimiterByChannelIndex(ch, limiter)
|
||||||
}
|
}
|
||||||
|
|
||||||
dp.enabled = prefs.getBoolean(KEY_DYNAMICS_ENABLED, false)
|
dp.enabled = dynamicsEnabled
|
||||||
return dp
|
return dp
|
||||||
}
|
}
|
||||||
|
|
||||||
fun currentState(): AudioEffectsState {
|
fun currentState(): AudioEffectsState {
|
||||||
val eq = equalizer
|
val eqAvailable = equalizer != null
|
||||||
val bands = if (eq != null) {
|
val bands = if (eqAvailable) {
|
||||||
(0 until eq.numberOfBands).map { i ->
|
bandLevels.indices.map { i ->
|
||||||
val idx = i.toShort()
|
|
||||||
val range = eq.bandLevelRange
|
|
||||||
EqualizerBand(
|
EqualizerBand(
|
||||||
index = i,
|
index = i,
|
||||||
centerFreqHz = eq.getCenterFreq(idx) / 1000,
|
centerFreqHz = centerFreqsHz[i],
|
||||||
levelMillibel = eq.getBandLevel(idx).toInt(),
|
levelMillibel = bandLevels[i],
|
||||||
minLevelMillibel = range[0].toInt(),
|
minLevelMillibel = minBandLevel,
|
||||||
maxLevelMillibel = range[1].toInt()
|
maxLevelMillibel = maxBandLevel
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else emptyList()
|
} else emptyList()
|
||||||
|
|
||||||
return AudioEffectsState(
|
return AudioEffectsState(
|
||||||
equalizerAvailable = eq != null,
|
equalizerAvailable = eqAvailable,
|
||||||
equalizerEnabled = eq?.enabled ?: false,
|
equalizerEnabled = eqAvailable && equalizerEnabled,
|
||||||
bands = bands,
|
bands = bands,
|
||||||
presets = presetNames,
|
presets = presetNames,
|
||||||
currentPreset = if (eq != null) currentPresetIndex else -1,
|
currentPreset = if (eqAvailable) currentPresetIndex else -1,
|
||||||
loudnessAvailable = loudnessEnhancer != null,
|
loudnessAvailable = loudnessEnhancer != null,
|
||||||
loudnessEnabled = loudnessEnhancer?.enabled ?: false,
|
loudnessEnabled = loudnessEnhancer != null && loudnessEnabled,
|
||||||
loudnessGainMillibel = try {
|
loudnessGainMillibel = if (loudnessEnhancer != null) loudnessGain.toFloat() else 0f,
|
||||||
loudnessEnhancer?.targetGain ?: 0f
|
|
||||||
} catch (_: Exception) {
|
|
||||||
0f
|
|
||||||
},
|
|
||||||
dynamicsProcessingAvailable = dynamicsProcessing != null,
|
dynamicsProcessingAvailable = dynamicsProcessing != null,
|
||||||
dynamicsProcessingEnabled = dynamicsProcessing?.enabled ?: false
|
dynamicsProcessingEnabled = dynamicsProcessing != null && dynamicsEnabled
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setEqualizerEnabled(enabled: Boolean) {
|
fun setEqualizerEnabled(enabled: Boolean) {
|
||||||
|
val eq = equalizer ?: return
|
||||||
try {
|
try {
|
||||||
equalizer?.enabled = enabled
|
eq.enabled = enabled
|
||||||
|
equalizerEnabled = enabled
|
||||||
prefs.edit { putBoolean(KEY_EQ_ENABLED, enabled) }
|
prefs.edit { putBoolean(KEY_EQ_ENABLED, enabled) }
|
||||||
} catch (_: Exception) {}
|
} catch (_: Exception) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setBandLevel(band: Int, levelMillibel: Int) {
|
fun setBandLevel(band: Int, levelMillibel: Int) {
|
||||||
|
val eq = equalizer ?: return
|
||||||
|
if (band !in bandLevels.indices) return
|
||||||
|
val level = levelMillibel.coerceIn(minBandLevel, maxBandLevel)
|
||||||
|
if (bandLevels[band] == level && currentPresetIndex == -1) return
|
||||||
try {
|
try {
|
||||||
equalizer?.setBandLevel(band.toShort(), levelMillibel.toShort())
|
eq.setBandLevel(band.toShort(), level.toShort())
|
||||||
|
bandLevels[band] = level
|
||||||
currentPresetIndex = -1
|
currentPresetIndex = -1
|
||||||
prefs.edit {
|
prefs.edit {
|
||||||
putInt(KEY_BAND_PREFIX + band, levelMillibel)
|
putInt(KEY_BAND_PREFIX + band, level)
|
||||||
putInt(KEY_PRESET, -1)
|
putInt(KEY_PRESET, -1)
|
||||||
}
|
}
|
||||||
} catch (_: Exception) {}
|
} catch (_: Exception) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setPreset(preset: Int) {
|
fun setPreset(preset: Int) {
|
||||||
|
val eq = equalizer ?: return
|
||||||
try {
|
try {
|
||||||
val eq = equalizer ?: return
|
|
||||||
eq.usePreset(preset.toShort())
|
eq.usePreset(preset.toShort())
|
||||||
currentPresetIndex = preset
|
currentPresetIndex = preset
|
||||||
|
for (b in bandLevels.indices) {
|
||||||
|
bandLevels[b] = eq.getBandLevel(b.toShort()).toInt()
|
||||||
|
}
|
||||||
prefs.edit {
|
prefs.edit {
|
||||||
putInt(KEY_PRESET, preset)
|
putInt(KEY_PRESET, preset)
|
||||||
for (b in 0 until eq.numberOfBands) {
|
for (b in bandLevels.indices) putInt(KEY_BAND_PREFIX + b, bandLevels[b])
|
||||||
putInt(KEY_BAND_PREFIX + b, eq.getBandLevel(b.toShort()).toInt())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (_: Exception) {}
|
} catch (_: Exception) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setLoudnessEnabled(enabled: Boolean) {
|
fun setLoudnessEnabled(enabled: Boolean) {
|
||||||
|
val le = loudnessEnhancer ?: return
|
||||||
try {
|
try {
|
||||||
loudnessEnhancer?.enabled = enabled
|
le.enabled = enabled
|
||||||
|
loudnessEnabled = enabled
|
||||||
prefs.edit { putBoolean(KEY_LOUDNESS_ENABLED, enabled) }
|
prefs.edit { putBoolean(KEY_LOUDNESS_ENABLED, enabled) }
|
||||||
} catch (_: Exception) {}
|
} catch (_: Exception) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setLoudnessGain(gainMillibel: Float) {
|
fun setLoudnessGain(gainMillibel: Float) {
|
||||||
|
val le = loudnessEnhancer ?: return
|
||||||
|
val gain = gainMillibel.coerceIn(0f, MAX_LOUDNESS_GAIN.toFloat()).toInt()
|
||||||
|
if (gain == loudnessGain) return
|
||||||
try {
|
try {
|
||||||
val gain = gainMillibel.coerceIn(0f, 2000f).toInt()
|
le.setTargetGain(gain)
|
||||||
loudnessEnhancer?.setTargetGain(gain)
|
loudnessGain = gain
|
||||||
prefs.edit { putInt(KEY_LOUDNESS_GAIN, gain) }
|
prefs.edit { putInt(KEY_LOUDNESS_GAIN, gain) }
|
||||||
} catch (_: Exception) {}
|
} catch (_: Exception) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setDynamicsProcessingEnabled(enabled: Boolean) {
|
fun setDynamicsProcessingEnabled(enabled: Boolean) {
|
||||||
|
val dp = dynamicsProcessing ?: return
|
||||||
try {
|
try {
|
||||||
dynamicsProcessing?.enabled = enabled
|
dp.enabled = enabled
|
||||||
|
dynamicsEnabled = enabled
|
||||||
prefs.edit { putBoolean(KEY_DYNAMICS_ENABLED, enabled) }
|
prefs.edit { putBoolean(KEY_DYNAMICS_ENABLED, enabled) }
|
||||||
} catch (_: Exception) {}
|
} catch (_: Exception) {}
|
||||||
}
|
}
|
||||||
@@ -237,6 +264,7 @@ class AudioEffectsEngine(context: Context, audioSessionId: Int) {
|
|||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
const val TAG = "AudioEffectsEngine"
|
const val TAG = "AudioEffectsEngine"
|
||||||
|
const val MAX_LOUDNESS_GAIN = 2000
|
||||||
const val KEY_EQ_ENABLED = "eq_enabled"
|
const val KEY_EQ_ENABLED = "eq_enabled"
|
||||||
const val KEY_PRESET = "eq_preset"
|
const val KEY_PRESET = "eq_preset"
|
||||||
const val KEY_BAND_PREFIX = "eq_band_"
|
const val KEY_BAND_PREFIX = "eq_band_"
|
||||||
|
|||||||
Reference in New Issue
Block a user