Skip to content

Commit 6d4d67c

Browse files
committed
use builder configuration instead
1 parent 723077a commit 6d4d67c

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

firebase-ai/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- [changed] Added `LiveAudioConversationConfig` to control different aspects of the conversation
4+
while using the `startAudioConversation` function.
35
- [changed] Added better scheduling and louder output for Live API.
46
- [changed] Added support for input and output transcription. (#7482)
57
- [feature] Added support for sending realtime audio and video in a `LiveSession`.

firebase-ai/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
version=17.5.0
15+
version=99.9.9
1616
latestReleasedVersion=17.4.0

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/AudioHelper.kt

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package com.google.firebase.ai.type
1919
import android.Manifest
2020
import android.media.AudioAttributes
2121
import android.media.AudioFormat
22-
import android.media.AudioManager
2322
import android.media.AudioRecord
2423
import android.media.AudioTrack
2524
import android.media.MediaRecorder
@@ -157,28 +156,39 @@ internal class AudioHelper(
157156
*
158157
* It also makes it easier to read, since the long initialization is separate from the
159158
* constructor.
159+
*
160+
* @param audioHandler A callback that is invoked immediately following the successful
161+
* initialization of the associated [AudioRecord] and [AudioTrack] objects. This offers a final
162+
* opportunity to configure these objects, which will remain valid and effective for the
163+
* duration of the current audio session.
160164
*/
161165
@RequiresPermission(Manifest.permission.RECORD_AUDIO)
162-
fun build(audioHandler: ((AudioRecord, AudioTrack) -> Unit)? = null): AudioHelper {
163-
val playbackTrack =
164-
AudioTrack(
165-
AudioAttributes.Builder()
166-
.setUsage(AudioAttributes.USAGE_MEDIA)
167-
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
168-
.build(),
166+
fun build(
167+
audioHandler: ((AudioRecord.Builder, AudioTrack.Builder) -> Unit)? = null
168+
): AudioHelper {
169+
val playTrackBuilder = AudioTrack.Builder()
170+
playTrackBuilder
171+
.setAudioFormat(
169172
AudioFormat.Builder()
170173
.setSampleRate(24000)
171174
.setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
172175
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
173-
.build(),
176+
.build()
177+
)
178+
.setAudioAttributes(
179+
AudioAttributes.Builder()
180+
.setUsage(AudioAttributes.USAGE_MEDIA)
181+
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
182+
.build()
183+
)
184+
.setBufferSizeInBytes(
174185
AudioTrack.getMinBufferSize(
175186
24000,
176187
AudioFormat.CHANNEL_OUT_MONO,
177188
AudioFormat.ENCODING_PCM_16BIT
178189
),
179-
AudioTrack.MODE_STREAM,
180-
AudioManager.AUDIO_SESSION_ID_GENERATE
181190
)
191+
.setTransferMode(AudioTrack.MODE_STREAM)
182192

183193
val bufferSize =
184194
AudioRecord.getMinBufferSize(
@@ -191,15 +201,22 @@ internal class AudioHelper(
191201
throw AudioRecordInitializationFailedException(
192202
"Audio Record buffer size is invalid ($bufferSize)"
193203
)
194-
195-
val recorder =
196-
AudioRecord(
197-
MediaRecorder.AudioSource.VOICE_COMMUNICATION,
198-
16000,
199-
AudioFormat.CHANNEL_IN_MONO,
200-
AudioFormat.ENCODING_PCM_16BIT,
201-
bufferSize
202-
)
204+
val recorderBuilder =
205+
AudioRecord.Builder()
206+
.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION)
207+
.setAudioFormat(
208+
AudioFormat.Builder()
209+
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
210+
.setSampleRate(16000)
211+
.setChannelMask(AudioFormat.CHANNEL_IN_MONO)
212+
.build()
213+
)
214+
.setBufferSizeInBytes(bufferSize)
215+
if (audioHandler != null) {
216+
audioHandler(recorderBuilder, playTrackBuilder)
217+
}
218+
val recorder = recorderBuilder.build()
219+
val playbackTrack = playTrackBuilder.build()
203220
if (recorder.state != AudioRecord.STATE_INITIALIZED)
204221
throw AudioRecordInitializationFailedException(
205222
"Audio Record initialization has failed. State: ${recorder.state}"
@@ -209,9 +226,6 @@ internal class AudioHelper(
209226
AcousticEchoCanceler.create(recorder.audioSessionId)?.enabled = true
210227
}
211228

212-
if (audioHandler != null) {
213-
audioHandler(recorder, playbackTrack)
214-
}
215229
return AudioHelper(recorder, playbackTrack)
216230
}
217231
}

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/LiveAudioConversationConfig.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ import android.media.AudioTrack
3131
* transcription.
3232
*
3333
* @property audioHandler A callback that is invoked immediately following the successful
34-
* initialization of the associated [AudioRecord] and [AudioTrack] objects. This offers a final
35-
* opportunity to configure these objects, which will remain valid and effective for the duration of
36-
* the current audio session.
34+
* initialization of the associated [AudioRecord.Builder] and [AudioTrack.Builder] objects. This
35+
* offers a final opportunity to configure these objects, which will remain valid and effective for
36+
* the duration of the current audio session.
3737
*
3838
* @property enableInterruptions If enabled, allows the user to speak over or interrupt the model's
3939
* ongoing reply.
@@ -45,7 +45,7 @@ import android.media.AudioTrack
4545
public class LiveAudioConversationConfig
4646
private constructor(
4747
internal val functionCallHandler: ((FunctionCallPart) -> FunctionResponsePart)?,
48-
internal val audioHandler: ((AudioRecord, AudioTrack) -> Unit)?,
48+
internal val audioHandler: ((AudioRecord.Builder, AudioTrack.Builder) -> Unit)?,
4949
internal val transcriptHandler: ((Transcription?, Transcription?) -> Unit)?,
5050
internal val enableInterruptions: Boolean
5151
) {

0 commit comments

Comments
 (0)