Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/firebase_ai/firebase_ai/lib/src/live_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,33 @@ class VoiceConfig {

/// Configures speech synthesis settings.
///
/// Allows specifying the desired voice for speech synthesis.
/// Allows specifying the desired voice and language for speech synthesis.
class SpeechConfig {
/// Creates a [SpeechConfig] instance.
///
/// [voiceName] See https://cloud.google.com/text-to-speech/docs/chirp3-hd
/// for names and sound demos.
SpeechConfig({String? voiceName})
///
/// [languageCode] The language code (BCP-47) for the speech synthesis,
/// e.g. "en-US", "fr-FR", "de-DE".
SpeechConfig({String? voiceName, this.languageCode})
: voiceConfig = voiceName != null
? VoiceConfig(
prebuiltVoiceConfig: PrebuiltVoiceConfig(voiceName: voiceName))
: null;

/// The voice config to use for speech synthesis.
final VoiceConfig? voiceConfig;

/// The language code (BCP-47) for speech synthesis,
/// e.g. "en-US", "fr-FR", "de-DE".
final String? languageCode;
// ignore: public_member_api_docs
Map<String, Object?> toJson() => {
if (voiceConfig case final voiceConfig?)
'voice_config': voiceConfig.toJson()
'voice_config': voiceConfig.toJson(),
if (languageCode case final languageCode?)
'language_code': languageCode,
};
}

Expand Down
16 changes: 16 additions & 0 deletions packages/firebase_ai/firebase_ai/test/live_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ void main() {
expect(speechConfigWithoutVoice.toJson(), {});
});

test('SpeechConfig with languageCode toJson() returns correct JSON', () {
final speechConfigWithLanguage =
SpeechConfig(voiceName: 'Aoede', languageCode: 'en-US');
expect(speechConfigWithLanguage.toJson(), {
'voice_config': {
'prebuilt_voice_config': {'voice_name': 'Aoede'}
},
'language_code': 'en-US',
});

final speechConfigLanguageOnly = SpeechConfig(languageCode: 'fr-FR');
expect(speechConfigLanguageOnly.toJson(), {
'language_code': 'fr-FR',
});
});

test('ResponseModalities enum toJson() returns correct value', () {
expect(ResponseModalities.text.toJson(), 'TEXT');
expect(ResponseModalities.image.toJson(), 'IMAGE');
Expand Down
Loading