Skip to content

Commit 4c73200

Browse files
committed
android: improve conversational awareness (fixes #122)
1 parent 06de276 commit 4c73200

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

android/app/src/main/java/me/kavishdevar/librepods/utils/MediaController.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import android.os.Looper
2626
import android.util.Log
2727
import android.view.KeyEvent
2828
import me.kavishdevar.librepods.services.ServiceManager
29+
import kotlin.div
30+
import kotlin.text.compareTo
31+
import kotlin.times
2932

3033
object MediaController {
3134
private var initialVolume: Int? = null
@@ -38,7 +41,7 @@ object MediaController {
3841
var pausedForCrossDevice = false
3942

4043
private var relativeVolume: Boolean = false
41-
private var conversationalAwarenessVolume: Int = 1/12
44+
private var conversationalAwarenessVolume: Int = 2
4245
private var conversationalAwarenessPauseMusic: Boolean = false
4346

4447
fun initialize(audioManager: AudioManager, sharedPreferences: SharedPreferences) {
@@ -49,7 +52,7 @@ object MediaController {
4952
this.sharedPreferences = sharedPreferences
5053
Log.d("MediaController", "Initializing MediaController")
5154
relativeVolume = sharedPreferences.getBoolean("relative_conversational_awareness_volume", false)
52-
conversationalAwarenessVolume = sharedPreferences.getInt("conversational_awareness_volume", 100/12)
55+
conversationalAwarenessVolume = sharedPreferences.getInt("conversational_awareness_volume", audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) / 12)
5356
conversationalAwarenessPauseMusic = sharedPreferences.getBoolean("conversational_awareness_pause_music", false)
5457

5558
sharedPreferences.registerOnSharedPreferenceChangeListener { _, key ->
@@ -134,11 +137,18 @@ object MediaController {
134137

135138
@Synchronized
136139
fun startSpeaking() {
137-
Log.d("MediaController", "Starting speaking")
140+
Log.d("MediaController", "Starting speaking max vol: ${audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)}, current vol: ${audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)}, conversationalAwarenessVolume: $conversationalAwarenessVolume, relativeVolume: $relativeVolume")
141+
138142
if (initialVolume == null) {
139143
initialVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
140-
Log.d("MediaController", "Initial Volume Set: $initialVolume")
141-
val targetVolume = if (relativeVolume) initialVolume!! * conversationalAwarenessVolume * 1/100 else if ( initialVolume!! > audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * conversationalAwarenessVolume * 1/100) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * conversationalAwarenessVolume * 1/100 else initialVolume!!
144+
Log.d("MediaController", "Initial Volume: $initialVolume")
145+
val targetVolume = if (relativeVolume) {
146+
(initialVolume!! * conversationalAwarenessVolume / 100)
147+
} else if (initialVolume!! > (audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * conversationalAwarenessVolume / 100)) {
148+
(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * conversationalAwarenessVolume / 100)
149+
} else {
150+
initialVolume!!
151+
}
142152
smoothVolumeTransition(initialVolume!!, targetVolume.toInt())
143153
if (conversationalAwarenessPauseMusic) {
144154
sendPause(force = true)
@@ -160,6 +170,7 @@ object MediaController {
160170
}
161171

162172
private fun smoothVolumeTransition(fromVolume: Int, toVolume: Int) {
173+
Log.d("MediaController", "Smooth volume transition from $fromVolume to $toVolume")
163174
val step = if (fromVolume < toVolume) 1 else -1
164175
val delay = 50L
165176
var currentVolume = fromVolume

0 commit comments

Comments
 (0)