audio: Apply square curve to volume slider
AudioTrack.setVolume() takes a linear amplitude value, but human hearing is logarithmic — a linear 0-1 slider sounds like all the volume is crammed into the bottom quarter of the range. Squaring the slider value (amplitude = linear²) spreads perceived loudness evenly across the full slider travel.
This commit is contained in:
parent
d79fcebc41
commit
da3b760f43
1 changed files with 7 additions and 2 deletions
|
|
@ -22,6 +22,11 @@ class AudioEngine {
|
||||||
companion object {
|
companion object {
|
||||||
private const val BEEP_DURATION_MS = 40
|
private const val BEEP_DURATION_MS = 40
|
||||||
private const val SILENCE_CHUNK_MS = 10
|
private const val SILENCE_CHUNK_MS = 10
|
||||||
|
|
||||||
|
// Apply a square curve so the slider feels linear to human hearing.
|
||||||
|
// AudioTrack.setVolume() takes a linear amplitude; our 0-1 slider
|
||||||
|
// value is perceived as "all the volume at the bottom" without this.
|
||||||
|
private fun toAmplitude(linear: Float): Float = linear * linear
|
||||||
}
|
}
|
||||||
|
|
||||||
private val mainHandler = Handler(Looper.getMainLooper())
|
private val mainHandler = Handler(Looper.getMainLooper())
|
||||||
|
|
@ -69,7 +74,7 @@ class AudioEngine {
|
||||||
.setTransferMode(AudioTrack.MODE_STREAM)
|
.setTransferMode(AudioTrack.MODE_STREAM)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
audioTrack?.setVolume(currentVolume)
|
audioTrack?.setVolume(toAmplitude(currentVolume))
|
||||||
audioTrack?.play()
|
audioTrack?.play()
|
||||||
|
|
||||||
handlerThread = HandlerThread("PacerAudio").also { it.start() }
|
handlerThread = HandlerThread("PacerAudio").also { it.start() }
|
||||||
|
|
@ -94,7 +99,7 @@ class AudioEngine {
|
||||||
|
|
||||||
fun setVolume(volume: Float) {
|
fun setVolume(volume: Float) {
|
||||||
currentVolume = BpmCalculator.clampVolume(volume)
|
currentVolume = BpmCalculator.clampVolume(volume)
|
||||||
audioTrack?.setVolume(currentVolume)
|
audioTrack?.setVolume(toAmplitude(currentVolume))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun audioLoop() {
|
private fun audioLoop() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue