mirror of
https://github.com/kavishdevar/librepods.git
synced 2026-02-10 19:52:24 +00:00
android: improve conversational awareness (fixes #122)
This commit is contained in:
@@ -26,6 +26,9 @@ import android.os.Looper
|
|||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.KeyEvent
|
import android.view.KeyEvent
|
||||||
import me.kavishdevar.librepods.services.ServiceManager
|
import me.kavishdevar.librepods.services.ServiceManager
|
||||||
|
import kotlin.div
|
||||||
|
import kotlin.text.compareTo
|
||||||
|
import kotlin.times
|
||||||
|
|
||||||
object MediaController {
|
object MediaController {
|
||||||
private var initialVolume: Int? = null
|
private var initialVolume: Int? = null
|
||||||
@@ -38,7 +41,7 @@ object MediaController {
|
|||||||
var pausedForCrossDevice = false
|
var pausedForCrossDevice = false
|
||||||
|
|
||||||
private var relativeVolume: Boolean = false
|
private var relativeVolume: Boolean = false
|
||||||
private var conversationalAwarenessVolume: Int = 1/12
|
private var conversationalAwarenessVolume: Int = 2
|
||||||
private var conversationalAwarenessPauseMusic: Boolean = false
|
private var conversationalAwarenessPauseMusic: Boolean = false
|
||||||
|
|
||||||
fun initialize(audioManager: AudioManager, sharedPreferences: SharedPreferences) {
|
fun initialize(audioManager: AudioManager, sharedPreferences: SharedPreferences) {
|
||||||
@@ -49,7 +52,7 @@ object MediaController {
|
|||||||
this.sharedPreferences = sharedPreferences
|
this.sharedPreferences = sharedPreferences
|
||||||
Log.d("MediaController", "Initializing MediaController")
|
Log.d("MediaController", "Initializing MediaController")
|
||||||
relativeVolume = sharedPreferences.getBoolean("relative_conversational_awareness_volume", false)
|
relativeVolume = sharedPreferences.getBoolean("relative_conversational_awareness_volume", false)
|
||||||
conversationalAwarenessVolume = sharedPreferences.getInt("conversational_awareness_volume", 100/12)
|
conversationalAwarenessVolume = sharedPreferences.getInt("conversational_awareness_volume", audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) / 12)
|
||||||
conversationalAwarenessPauseMusic = sharedPreferences.getBoolean("conversational_awareness_pause_music", false)
|
conversationalAwarenessPauseMusic = sharedPreferences.getBoolean("conversational_awareness_pause_music", false)
|
||||||
|
|
||||||
sharedPreferences.registerOnSharedPreferenceChangeListener { _, key ->
|
sharedPreferences.registerOnSharedPreferenceChangeListener { _, key ->
|
||||||
@@ -134,11 +137,18 @@ object MediaController {
|
|||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun startSpeaking() {
|
fun startSpeaking() {
|
||||||
Log.d("MediaController", "Starting speaking")
|
Log.d("MediaController", "Starting speaking max vol: ${audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)}, current vol: ${audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)}, conversationalAwarenessVolume: $conversationalAwarenessVolume, relativeVolume: $relativeVolume")
|
||||||
|
|
||||||
if (initialVolume == null) {
|
if (initialVolume == null) {
|
||||||
initialVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
|
initialVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
|
||||||
Log.d("MediaController", "Initial Volume Set: $initialVolume")
|
Log.d("MediaController", "Initial Volume: $initialVolume")
|
||||||
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!!
|
val targetVolume = if (relativeVolume) {
|
||||||
|
(initialVolume!! * conversationalAwarenessVolume / 100)
|
||||||
|
} else if (initialVolume!! > (audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * conversationalAwarenessVolume / 100)) {
|
||||||
|
(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * conversationalAwarenessVolume / 100)
|
||||||
|
} else {
|
||||||
|
initialVolume!!
|
||||||
|
}
|
||||||
smoothVolumeTransition(initialVolume!!, targetVolume.toInt())
|
smoothVolumeTransition(initialVolume!!, targetVolume.toInt())
|
||||||
if (conversationalAwarenessPauseMusic) {
|
if (conversationalAwarenessPauseMusic) {
|
||||||
sendPause(force = true)
|
sendPause(force = true)
|
||||||
@@ -160,6 +170,7 @@ object MediaController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun smoothVolumeTransition(fromVolume: Int, toVolume: Int) {
|
private fun smoothVolumeTransition(fromVolume: Int, toVolume: Int) {
|
||||||
|
Log.d("MediaController", "Smooth volume transition from $fromVolume to $toVolume")
|
||||||
val step = if (fromVolume < toVolume) 1 else -1
|
val step = if (fromVolume < toVolume) 1 else -1
|
||||||
val delay = 50L
|
val delay = 50L
|
||||||
var currentVolume = fromVolume
|
var currentVolume = fromVolume
|
||||||
|
|||||||
Reference in New Issue
Block a user