Skip to content

Commit

Permalink
Shutdown audio service in idle mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jurihock committed Jun 9, 2024
1 parent c040088 commit ec52f87
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 98 deletions.
105 changes: 54 additions & 51 deletions voicesmith/src/main/java/de/jurihock/voicesmith/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class MainActivity : AudioServiceActivity() {
private val timbre = mutableIntStateOf(0)
private val state = mutableStateOf(false)

private fun sync() {
delay.intValue = preferences.delay
pitch.intValue = preferences.pitch
timbre.intValue = preferences.timbre
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
try {
Expand All @@ -51,55 +57,58 @@ class MainActivity : AudioServiceActivity() {
} catch (exception: NameNotFoundException) {
Log.e("Unable to determine the package version!", exception)
}
setContent {
MainTheme {
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = {
DeviceSelectorScreen(
modifier = Modifier.padding(Dp(UI.PADDING)),
textInput = getString(R.string.input),
textOutput = getString(R.string.output),
onSelectInputDevice = { onSelectInputDevice() },
onSelectOutputDevice = { onSelectOutputDevice() })
},
bottomBar = {
BigToggleButtonScreen(
modifier = Modifier.padding(Dp(UI.PADDING)),
textOn = getString(R.string.start), textOff = getString(R.string.stop), value = state,
onToggle = { onStartStopAudioService() })
}) { padding ->
Column(modifier = Modifier.padding(padding).padding(Dp(UI.PADDING))) {
Spacer(modifier = Modifier.weight(1f))
IntParameterScreen(
name = getString(R.string.delay), unit = getString(R.string.milliseconds), value = delay,
min = 0, max = 1000, inc = 50,
onChange = {
delay.intValue = it
preferences.delay = it
})
Spacer(modifier = Modifier.height(Dp(UI.PADDING)))
IntParameterScreen(
name = getString(R.string.pitch), unit = getString(R.string.semitones), value = pitch,
min = -12, max = +12, inc = 1,
onChange = {
pitch.intValue = it
preferences.pitch = it
})
Spacer(modifier = Modifier.height(Dp(UI.PADDING)))
IntParameterScreen(
name = getString(R.string.timbre), unit = getString(R.string.semitones), value = timbre,
min = -12, max = +12, inc = 1,
onChange = {
timbre.intValue = it
preferences.timbre = it
})
Spacer(modifier = Modifier.weight(1f))
try {
setContent {
MainTheme {
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = {
DeviceSelectorScreen(
modifier = Modifier.padding(Dp(UI.PADDING)),
textInput = getString(R.string.input),
textOutput = getString(R.string.output),
onSelectInputDevice = { onSelectInputDevice() },
onSelectOutputDevice = { onSelectOutputDevice() })
},
bottomBar = {
BigToggleButtonScreen(
modifier = Modifier.padding(Dp(UI.PADDING)),
textOn = getString(R.string.start), textOff = getString(R.string.stop), value = state,
onToggle = { onStartStopAudioService() })
}) { padding ->
Column(modifier = Modifier.padding(padding).padding(Dp(UI.PADDING))) {
Spacer(modifier = Modifier.weight(1f))
IntParameterScreen(
name = getString(R.string.delay), unit = getString(R.string.milliseconds), value = delay,
min = 0, max = 1000, inc = 50,
onChange = {
delay.intValue = it
preferences.delay = it
})
Spacer(modifier = Modifier.height(Dp(UI.PADDING)))
IntParameterScreen(
name = getString(R.string.pitch), unit = getString(R.string.semitones), value = pitch,
min = -12, max = +12, inc = 1,
onChange = {
pitch.intValue = it
preferences.pitch = it
})
Spacer(modifier = Modifier.height(Dp(UI.PADDING)))
IntParameterScreen(
name = getString(R.string.timbre), unit = getString(R.string.semitones), value = timbre,
min = -12, max = +12, inc = 1,
onChange = {
timbre.intValue = it
preferences.timbre = it
})
Spacer(modifier = Modifier.weight(1f))
}
}
}
}
} finally {
sync()
}
sync()
}

override fun onAudioServiceStarted() {
Expand Down Expand Up @@ -132,11 +141,5 @@ class MainActivity : AudioServiceActivity() {
}
}

private fun sync() {
delay.intValue = preferences.delay
pitch.intValue = preferences.pitch
timbre.intValue = preferences.timbre
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ class AudioService : Service(), SharedPreferences.OnSharedPreferenceChangeListen
val isStarted: Boolean
get() = plugin?.isStarted ?: false

private fun sync() {
Log.i("Syncing audio plugin parameters")
try {
plugin?.setup(
preferences.input,
preferences.output,
preferences.samplerate,
preferences.blocksize)
plugin?.set("delay", preferences.delay.toString())
plugin?.set("pitch", preferences.pitch.toString())
plugin?.set("timbre", preferences.timbre.toString())
} catch (exception: Throwable) {
Log.e(exception)
}
}

private fun reset() {
Log.i("Resetting audio plugin")
val restart = isStarted
stop()
sync()
if (restart) {
start()
}
}

fun start() {
Log.i("Starting audio plugin")
try {
Expand All @@ -37,6 +63,9 @@ class AudioService : Service(), SharedPreferences.OnSharedPreferenceChangeListen
}
}

override fun onBind(intent: Intent?): IBinder = bindAudioService()
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int = startAudioService()

override fun onCreate() {
Log.i("Creating audio service")
try {
Expand Down Expand Up @@ -76,9 +105,6 @@ class AudioService : Service(), SharedPreferences.OnSharedPreferenceChangeListen
}
}

override fun onBind(intent: Intent?): IBinder = bindAudioService()
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int = startAudioService()

fun onServiceError(callback: (exception: Throwable) -> Unit) {
error = callback
}
Expand All @@ -91,30 +117,4 @@ class AudioService : Service(), SharedPreferences.OnSharedPreferenceChangeListen
}
}

private fun sync() {
Log.i("Syncing audio plugin parameters")
try {
plugin?.setup(
preferences.input,
preferences.output,
preferences.samplerate,
preferences.blocksize)
plugin?.set("delay", preferences.delay.toString())
plugin?.set("pitch", preferences.pitch.toString())
plugin?.set("timbre", preferences.timbre.toString())
} catch (exception: Throwable) {
Log.e(exception)
}
}

private fun reset() {
Log.i("Resetting audio plugin")
val restart = isStarted
stop()
sync()
if (restart) {
start()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ abstract class AudioServiceActivity : ComponentActivity(), ServiceConnection {
when {
permissions.getOrDefault(permissionToRecordAudio, false) == true -> {
Log.i("Record audio permission has been granted")
Log.i("Starting audio service")
startAudioService()
Log.i("Binding audio service")
bindAudioService()
enableAudioService()
}
permissions.getOrDefault(permissionToRecordAudio, false) == false -> {
Log.w("Record audio permission has been denied")
Expand All @@ -46,8 +43,7 @@ abstract class AudioServiceActivity : ComponentActivity(), ServiceConnection {
val uri = Uri.parse("package:${packageName}")
val intent = Intent(action, uri)
startActivity(intent)
}
finally {
} finally {
dialog.dismiss()
}
}
Expand All @@ -60,6 +56,30 @@ abstract class AudioServiceActivity : ComponentActivity(), ServiceConnection {

private var service: AudioService? = null

private fun enableAudioService() {
try {
Log.i("Starting audio service")
startAudioService()
Log.i("Binding audio service")
bindAudioService()
} finally {
service = null
}
}

private fun disableAudioService() {
service?.let {
try {
Log.i("Unbinding audio service")
unbindAudioService()
Log.i("Stopping audio service")
stopAudioService()
} finally {
service = null
}
}
}

protected abstract fun onAudioServiceStarted()
protected abstract fun onAudioServiceStopped()
protected abstract fun onAudioServiceFailed()
Expand All @@ -78,23 +98,16 @@ abstract class AudioServiceActivity : ComponentActivity(), ServiceConnection {
it.start()
onAudioServiceStarted()
}

if (!it.isStarted) {
disableAudioService()
}
}
}

override fun onDestroy() {
super.onDestroy()

if (service != null) {
try {
Log.i("Unbinding audio service")
unbindAudioService()
Log.i("Stopping audio service")
stopAudioService()
}
finally {
service = null
}
}
disableAudioService()
}

final override fun onServiceConnected(serviceName: ComponentName?, serviceBinder: IBinder?) {
Expand All @@ -117,6 +130,7 @@ abstract class AudioServiceActivity : ComponentActivity(), ServiceConnection {
service?.onServiceError { exception ->
Toast.makeText(this, exception.message, Toast.LENGTH_LONG).show()
onAudioServiceFailed()
disableAudioService()
}

onStartStopAudioService()
Expand Down

0 comments on commit ec52f87

Please sign in to comment.