-
Notifications
You must be signed in to change notification settings - Fork 646
AudioHandler callback to modify AudioTrack and AudioRecord #7496
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
VinayGuthal
wants to merge
8
commits into
main
Choose a base branch
from
add_device_support
base: main
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.
+242
−24
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
92dc3ac
add the user handler
VinayGuthal e51cadd
update config
VinayGuthal ec1c56c
update api text file
VinayGuthal 8d4a98d
fix kotlin docs
VinayGuthal f2afcfd
Rename audioconversation config and address some comments
VinayGuthal 723077a
update
VinayGuthal 6d4d67c
use builder configuration instead
VinayGuthal d01a970
update
VinayGuthal 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
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
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
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
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
130 changes: 130 additions & 0 deletions
130
firebase-ai/src/main/kotlin/com/google/firebase/ai/type/LiveAudioConversationConfig.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,130 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.firebase.ai.type | ||
|
|
||
| import android.media.AudioRecord | ||
| import android.media.AudioTrack | ||
|
|
||
| /** | ||
| * Configuration parameters to use for conversation config. | ||
| * | ||
| * @property functionCallHandler A callback that is invoked whenever the model receives a function | ||
| * call. The [FunctionResponsePart] that the callback function returns will be automatically sent to | ||
| * the model. | ||
| * | ||
| * @property transcriptHandler A callback that is invoked whenever the model receives a transcript. | ||
| * The first [Transcription] object is the input transcription, and the second is the output | ||
| * transcription. | ||
| * | ||
| * @property audioHandler A callback that is invoked immediately following the successful | ||
| * initialization of the associated [AudioRecord.Builder] and [AudioTrack.Builder] objects. This | ||
| * offers a final opportunity to configure these objects, which will remain valid and effective for | ||
| * the duration of the current audio session. | ||
| * | ||
| * @property enableInterruptions If enabled, allows the user to speak over or interrupt the model's | ||
| * ongoing reply. | ||
| * | ||
| * **WARNING**: The user interruption feature relies on device-specific support, and may not be | ||
| * consistently available. | ||
| */ | ||
| @PublicPreviewAPI | ||
| public class LiveAudioConversationConfig | ||
| private constructor( | ||
| internal val functionCallHandler: ((FunctionCallPart) -> FunctionResponsePart)?, | ||
| internal val audioHandler: ((AudioRecord.Builder, AudioTrack.Builder) -> Unit)?, | ||
| internal val transcriptHandler: ((Transcription?, Transcription?) -> Unit)?, | ||
| internal val enableInterruptions: Boolean | ||
| ) { | ||
|
|
||
| /** | ||
| * Builder for creating a [LiveAudioConversationConfig]. | ||
| * | ||
| * Mainly intended for Java interop. Kotlin consumers should use [liveAudioConversationConfig] for | ||
| * a more idiomatic experience. | ||
| * | ||
| * @property functionCallHandler See [LiveAudioConversationConfig.functionCallHandler]. | ||
| * | ||
| * @property audioHandler See [LiveAudioConversationConfig.audioHandler]. | ||
| * | ||
| * @property transcriptHandler See [LiveAudioConversationConfig.transcriptHandler]. | ||
| * | ||
| * @property enableInterruptions See [LiveAudioConversationConfig.enableInterruptions]. | ||
| */ | ||
| public class Builder { | ||
| @JvmField public var functionCallHandler: ((FunctionCallPart) -> FunctionResponsePart)? = null | ||
| @JvmField public var audioHandler: ((AudioRecord.Builder, AudioTrack.Builder) -> Unit)? = null | ||
| @JvmField public var transcriptHandler: ((Transcription?, Transcription?) -> Unit)? = null | ||
| @JvmField public var enableInterruptions: Boolean = false | ||
|
|
||
| public fun setFunctionCallHandler( | ||
| functionCallHandler: ((FunctionCallPart) -> FunctionResponsePart)? | ||
| ): Builder = apply { this.functionCallHandler = functionCallHandler } | ||
|
|
||
| public fun setAudioHandler( | ||
| audioHandler: ((AudioRecord.Builder, AudioTrack.Builder) -> Unit)? | ||
| ): Builder = apply { this.audioHandler = audioHandler } | ||
|
|
||
| public fun setTranscriptHandler( | ||
| transcriptHandler: ((Transcription?, Transcription?) -> Unit)? | ||
| ): Builder = apply { this.transcriptHandler = transcriptHandler } | ||
|
|
||
| public fun setEnableInterruptions(enableInterruptions: Boolean): Builder = apply { | ||
| this.enableInterruptions = enableInterruptions | ||
| } | ||
rlazo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** Create a new [LiveAudioConversationConfig] with the attached arguments. */ | ||
| public fun build(): LiveAudioConversationConfig = | ||
| LiveAudioConversationConfig( | ||
| functionCallHandler = functionCallHandler, | ||
| audioHandler = audioHandler, | ||
| transcriptHandler = transcriptHandler, | ||
| enableInterruptions = enableInterruptions | ||
| ) | ||
| } | ||
|
|
||
| public companion object { | ||
|
|
||
| /** | ||
| * Alternative casing for [LiveAudioConversationConfig.Builder]: | ||
| * ``` | ||
| * val config = LiveAudioConversationConfig.builder() | ||
| * ``` | ||
| */ | ||
| public fun builder(): Builder = Builder() | ||
VinayGuthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Helper method to construct a [LiveAudioConversationConfig] in a DSL-like manner. | ||
| * | ||
| * Example Usage: | ||
| * ``` | ||
| * liveAudioConversationConfig { | ||
| * functionCallHandler = ... | ||
| * audioHandler = ... | ||
| * ... | ||
| * } | ||
| * ``` | ||
| */ | ||
| @OptIn(PublicPreviewAPI::class) | ||
| public fun liveAudioConversationConfig( | ||
| init: LiveAudioConversationConfig.Builder.() -> Unit | ||
| ): LiveAudioConversationConfig { | ||
| val builder = LiveAudioConversationConfig.builder() | ||
| builder.init() | ||
| return builder.build() | ||
| } | ||
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
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.