-
Notifications
You must be signed in to change notification settings - Fork 4.1k
feat(vertexai): Add flutter_soloud for sound output in Live API audio streaming example. #17305
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3434e46
add soloud for sound output, but record is failing on iOS
khanhnwin 958d97b
restarting my simulator fixed it... but incorporating my AudioInput c…
khanhnwin 6188430
add JS scripts for flutter_soloud on web
khanhnwin 3ba57a2
add gitignore to example
cynthiajoan e471203
Merge branch 'main' into khanh-flutter-soloud
cynthiajoan 340db1f
Clean up before review
cynthiajoan d9a2ebe
Merge branch 'main' into khanh-flutter-soloud
cynthiajoan 72043e8
Apply suggestions from code review
cynthiajoan c1355d9
review comments
cynthiajoan b35502a
remove linux example folder
cynthiajoan e63ef37
some update for the example running generated files
cynthiajoan 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
Clean up before review
- Loading branch information
commit 340db1f92d4faef76eda9e827f46fd7f33e555ca
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
95 changes: 95 additions & 0 deletions
95
packages/firebase_ai/firebase_ai/example/lib/utils/audio_input.dart
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,95 @@ | ||
| // 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. | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:record/record.dart'; | ||
| import 'dart:typed_data'; | ||
|
|
||
| class AudioInput extends ChangeNotifier { | ||
| final _recorder = AudioRecorder(); | ||
| final AudioEncoder _encoder = AudioEncoder.pcm16bits; | ||
| bool isRecording = false; | ||
| bool isPaused = false; | ||
| Stream<Uint8List>? audioStream; | ||
|
|
||
| Future<void> init() async { | ||
| await checkPermission(); | ||
| } | ||
|
|
||
| @override | ||
| void dispose() { | ||
| _recorder.dispose(); | ||
| super.dispose(); | ||
| } | ||
|
|
||
| Future<void> checkPermission() async { | ||
|
cynthiajoan marked this conversation as resolved.
Outdated
|
||
| final hasPermission = await _recorder.hasPermission(); | ||
| if (!hasPermission) { | ||
| throw MicrophonePermissionDeniedException( | ||
| 'App does not have mic permissions', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| Future<Stream<Uint8List>?> startRecordingStream() async { | ||
| var recordConfig = RecordConfig( | ||
| encoder: _encoder, | ||
| sampleRate: 24000, | ||
| numChannels: 1, | ||
| echoCancel: true, | ||
| noiseSuppress: true, | ||
| androidConfig: const AndroidRecordConfig( | ||
| audioSource: AndroidAudioSource.voiceCommunication, | ||
| ), | ||
| iosConfig: const IosRecordConfig(categoryOptions: []), | ||
| ); | ||
| await _recorder.listInputDevices(); | ||
| audioStream = await _recorder.startStream(recordConfig); | ||
| isRecording = true; | ||
| notifyListeners(); | ||
| return audioStream; | ||
| } | ||
|
|
||
| Future<void> stopRecording() async { | ||
| await _recorder.stop(); | ||
| isRecording = false; | ||
| notifyListeners(); | ||
| } | ||
|
|
||
| Future<void> togglePause() async { | ||
| if (isPaused) { | ||
| await _recorder.resume(); | ||
| isPaused = false; | ||
| } else { | ||
| await _recorder.pause(); | ||
| isPaused = true; | ||
| } | ||
| notifyListeners(); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| /// An exception thrown when microphone permission is denied or not granted. | ||
| class MicrophonePermissionDeniedException implements Exception { | ||
| /// The optional message associated with the permission denial. | ||
| final String? message; | ||
|
|
||
| /// Creates a new [MicrophonePermissionDeniedException] with an optional [message]. | ||
| MicrophonePermissionDeniedException([this.message]); | ||
|
|
||
| @override | ||
| String toString() { | ||
| return 'MicrophonePermissionDeniedException: $message'; | ||
| } | ||
| } | ||
66 changes: 66 additions & 0 deletions
66
packages/firebase_ai/firebase_ai/example/lib/utils/audio_output.dart
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,66 @@ | ||
| // 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. | ||
|
|
||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:flutter_soloud/flutter_soloud.dart'; | ||
|
|
||
| class AudioOutput { | ||
| AudioSource? stream; // Start playback | ||
|
cynthiajoan marked this conversation as resolved.
Outdated
|
||
| SoundHandle? handle; | ||
|
|
||
| Future<void> init() async { | ||
| /// Initialize the player. | ||
|
cynthiajoan marked this conversation as resolved.
Outdated
|
||
| await SoLoud.instance.init(sampleRate: 24000, channels: Channels.mono); | ||
| await setupNewStream(); | ||
| } | ||
|
|
||
| Future<void> setupNewStream() async { | ||
| if (SoLoud.instance.isInitialized) { | ||
| // Stop and clear any previous playback handle if it's still valid | ||
| await stopStream(); // Ensure previous sound is stopped | ||
|
|
||
| stream = SoLoud.instance.setBufferStream( | ||
| maxBufferSizeBytes: | ||
| 1024 * 1024 * 10, // 10MB of max buffer (not allocated) | ||
| bufferingType: BufferingType.released, | ||
| bufferingTimeNeeds: 0, | ||
| onBuffering: (isBuffering, handle, time) {}, | ||
| ); | ||
| // Reset handle to null until the stream is played again | ||
| handle = null; | ||
| } | ||
| } | ||
|
|
||
| Future<AudioSource?> playStream() async { | ||
| handle = await SoLoud.instance.play(stream!); | ||
| return stream; | ||
| } | ||
|
|
||
| Future<void> stopStream() async { | ||
| if (stream != null && | ||
| handle != null && | ||
| SoLoud.instance.getIsValidVoiceHandle(handle!)) { | ||
| SoLoud.instance.setDataIsEnded(stream!); | ||
| await SoLoud.instance.stop(handle!); | ||
|
|
||
| // Clear old stream, set up new session for next time. | ||
| await setupNewStream(); | ||
| } | ||
| } | ||
|
|
||
| void addAudioStream(Uint8List audioChunk) { | ||
| SoLoud.instance.addAudioDataStream(stream!, audioChunk); | ||
| } | ||
| } | ||
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.