-
Notifications
You must be signed in to change notification settings - Fork 2
feat(android): add privacy-aware TTS foundation #1494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
agent-p1p
wants to merge
3
commits into
master
Choose a base branch
from
pip/whitenoise-android-1479
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
app/src/main/java/dev/ipf/whitenoise/android/audio/AudioFocusOwner.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| package dev.ipf.whitenoise.android.audio | ||
|
|
||
| import android.content.Context | ||
| import android.media.AudioAttributes | ||
| import android.media.AudioFocusRequest | ||
| import android.media.AudioManager | ||
|
|
||
| /** | ||
| * Process-wide audio-focus arbiter shared by voice-note playback (#1479) and | ||
| * the future TTS controller (#1480). Only one owner holds focus at a time; | ||
| * handoff occurs only after the new owner is granted focus. | ||
| */ | ||
| object AudioFocusOwner { | ||
| enum class Owner { | ||
| Voice, | ||
| Tts, | ||
| } | ||
|
|
||
| val ttsSpeechAttributes: AudioAttributes = | ||
| AudioAttributes | ||
| .Builder() | ||
| .setUsage(AudioAttributes.USAGE_MEDIA) | ||
| .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) | ||
| .build() | ||
|
|
||
| private var audioManager: AudioManager? = null | ||
| private var focusRequest: AudioFocusRequest? = null | ||
| private var currentOwner: Owner? = null | ||
| private var onLoss: (() -> Unit)? = null | ||
| private var onSurrender: (() -> Unit)? = null | ||
| private var focusListener: AudioManager.OnAudioFocusChangeListener? = null | ||
| private var nextGeneration = 0L | ||
| private var currentGeneration = 0L | ||
| private var activeCallbacks = 0 | ||
| private val focusLock = Any() | ||
|
|
||
| fun attach(context: Context) { | ||
| synchronized(focusLock) { | ||
| audioManager = context.applicationContext.getSystemService(AudioManager::class.java) | ||
| } | ||
| } | ||
|
|
||
| fun acquire( | ||
| owner: Owner, | ||
| audioAttributes: AudioAttributes, | ||
| focusGain: Int, | ||
| onFocusLoss: () -> Unit, | ||
| onOwnerSurrender: () -> Unit, | ||
| ): Boolean { | ||
| val result = | ||
| synchronized(focusLock) { | ||
| acquireLocked(owner, audioAttributes, focusGain, onFocusLoss, onOwnerSurrender) | ||
| } | ||
| try { | ||
| result.previousOwnerSurrender?.invoke() | ||
| } catch (error: Throwable) { | ||
| runCatching { | ||
| synchronized(focusLock) { | ||
| if (currentOwner == owner) { | ||
| abandonFocusInternal() | ||
| } | ||
| } | ||
| }.exceptionOrNull()?.let(error::addSuppressed) | ||
| throw error | ||
| } finally { | ||
| if (result.previousOwnerSurrender != null) { | ||
| completeCallback() | ||
| } | ||
| } | ||
| return result.acquired | ||
| } | ||
|
|
||
| private fun acquireLocked( | ||
| owner: Owner, | ||
| audioAttributes: AudioAttributes, | ||
| focusGain: Int, | ||
| onFocusLoss: () -> Unit, | ||
| onOwnerSurrender: () -> Unit, | ||
| ): AcquisitionResult { | ||
| if (activeCallbacks > 0) { | ||
| return AcquisitionResult(acquired = false) | ||
| } | ||
| val am = audioManager | ||
| if (am == null) { | ||
| val previousSurrender = onSurrender.takeIf { currentOwner != null && currentOwner != owner } | ||
| if (previousSurrender != null) { | ||
| activeCallbacks += 1 | ||
| } | ||
| currentOwner = owner | ||
| onLoss = onFocusLoss | ||
| onSurrender = onOwnerSurrender | ||
| return AcquisitionResult(acquired = true, previousOwnerSurrender = previousSurrender) | ||
| } | ||
| if (focusRequest != null && currentOwner == owner) { | ||
| onLoss = onFocusLoss | ||
| onSurrender = onOwnerSurrender | ||
| return AcquisitionResult(acquired = true) | ||
| } | ||
| val generation = ++nextGeneration | ||
| val listener = | ||
| AudioManager.OnAudioFocusChangeListener { change -> | ||
| val lossCallback = | ||
| synchronized(focusLock) { | ||
| if (generation != currentGeneration || currentOwner != owner) { | ||
| null | ||
| } else { | ||
| when (change) { | ||
| AudioManager.AUDIOFOCUS_LOSS, | ||
| AudioManager.AUDIOFOCUS_LOSS_TRANSIENT, | ||
| AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK, | ||
| -> onLoss | ||
| else -> null | ||
| }?.also { activeCallbacks += 1 } | ||
| } | ||
| } | ||
| try { | ||
| lossCallback?.invoke() | ||
| } finally { | ||
| if (lossCallback != null) { | ||
| completeCallback() | ||
| } | ||
| } | ||
| } | ||
| val req = | ||
| AudioFocusRequest | ||
| .Builder(focusGain) | ||
| .setAudioAttributes(audioAttributes) | ||
| .setOnAudioFocusChangeListener(listener) | ||
| .build() | ||
| val granted = am.requestAudioFocus(req) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED | ||
| if (!granted) { | ||
| return AcquisitionResult(acquired = false) | ||
| } | ||
|
|
||
| val previousRequest = focusRequest | ||
| val previousSurrender = onSurrender.takeIf { currentOwner != null && currentOwner != owner } | ||
| if (previousSurrender != null) { | ||
| activeCallbacks += 1 | ||
| } | ||
| focusRequest = req | ||
| currentOwner = owner | ||
| onLoss = onFocusLoss | ||
| onSurrender = onOwnerSurrender | ||
| focusListener = listener | ||
| currentGeneration = generation | ||
| previousRequest?.let(am::abandonAudioFocusRequest) | ||
| return AcquisitionResult(acquired = true, previousOwnerSurrender = previousSurrender) | ||
| } | ||
|
|
||
| private data class AcquisitionResult( | ||
| val acquired: Boolean, | ||
| val previousOwnerSurrender: (() -> Unit)? = null, | ||
| ) | ||
|
|
||
| private fun completeCallback() { | ||
| synchronized(focusLock) { | ||
| check(activeCallbacks > 0) | ||
| activeCallbacks -= 1 | ||
| } | ||
| } | ||
|
|
||
| fun acquireForTts( | ||
| onFocusLoss: () -> Unit, | ||
| onOwnerSurrender: () -> Unit, | ||
| ): Boolean = | ||
| acquire( | ||
| owner = Owner.Tts, | ||
| audioAttributes = ttsSpeechAttributes, | ||
| focusGain = AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, | ||
| onFocusLoss = onFocusLoss, | ||
| onOwnerSurrender = onOwnerSurrender, | ||
| ) | ||
|
|
||
| fun release(owner: Owner) { | ||
| synchronized(focusLock) { | ||
| if (currentOwner != owner) return | ||
| abandonFocusInternal() | ||
| } | ||
| } | ||
|
|
||
| fun releaseTts() { | ||
| release(Owner.Tts) | ||
| } | ||
|
|
||
| private fun abandonFocusInternal() { | ||
| val request = focusRequest | ||
| audioManager?.let { am -> | ||
| request?.let { am.abandonAudioFocusRequest(it) } | ||
| } | ||
| focusRequest = null | ||
| currentOwner = null | ||
| onLoss = null | ||
| onSurrender = null | ||
| focusListener = null | ||
| currentGeneration = 0L | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
app/src/main/java/dev/ipf/whitenoise/android/audio/tts/TtsEngineCatalog.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package dev.ipf.whitenoise.android.audio.tts | ||
|
|
||
| import android.speech.tts.TextToSpeech | ||
|
|
||
| /** | ||
| * Thin platform seam for [TextToSpeech] engine enumeration so unit tests can | ||
| * supply deterministic engine lists without constructing real instances. | ||
| */ | ||
| interface TtsEngineCatalog { | ||
| fun installedEngines(tts: TextToSpeech): List<TextToSpeech.EngineInfo> | ||
|
|
||
| fun defaultEnginePackage(tts: TextToSpeech): String? | ||
|
|
||
| /** | ||
| * Package Android actually connected for this [TextToSpeech] instance, when | ||
| * the public API can report it. Null means the active package cannot be | ||
| * verified (for example after a silent fallback bind). | ||
| */ | ||
| fun connectedEnginePackage( | ||
| tts: TextToSpeech, | ||
| requestedPackage: String?, | ||
| ): String? | ||
| } | ||
|
|
||
| object AndroidTtsEngineCatalog : TtsEngineCatalog { | ||
| override fun installedEngines(tts: TextToSpeech): List<TextToSpeech.EngineInfo> = tts.engines | ||
|
|
||
| override fun defaultEnginePackage(tts: TextToSpeech): String? = tts.defaultEngine | ||
|
|
||
| override fun connectedEnginePackage( | ||
| tts: TextToSpeech, | ||
| requestedPackage: String?, | ||
| ): String? = null | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.