Fix voice order

This commit is contained in:
2025-02-06 10:45:58 +02:00
parent 7a9f975ebd
commit 4444f9bdaa

View File

@@ -38,7 +38,7 @@ class _AiChatState extends State<AiChat> {
// bool _ttsOn = false;
String? textStream;
List<Map> _voices = [];
Map? _currentVoice;
List<String> _voicesString = [];
List<types.Message> _messages = [];
late types.User _user;
late types.User _mihAI;
@@ -405,6 +405,50 @@ class _AiChatState extends State<AiChat> {
],
),
const SizedBox(height: 15),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(
width: 230,
child: MIHDropdownField(
controller: _ttsVoiceController,
hintText: "AI Voice",
dropdownOptions: _voicesString,
required: true,
editable: true,
enableSearch: false,
),
),
const SizedBox(width: 10),
Padding(
padding: const EdgeInsets.all(5.0),
child: Container(
//color: MzanziInnovationHub.of(context)!.theme.successColor(),
decoration: BoxDecoration(
color: MzanziInnovationHub.of(context)!
.theme
.successColor(),
borderRadius: const BorderRadius.all(
Radius.circular(100),
),
),
child: IconButton(
color: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
onPressed: () {
print("Start TTS now");
_speakText(
"This is the sample of the Mzansi A.I Voice.");
},
icon: const Icon(Icons.volume_up),
),
),
),
],
),
const SizedBox(height: 15),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
@@ -446,52 +490,6 @@ class _AiChatState extends State<AiChat> {
],
),
const SizedBox(height: 15),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(
width: 230,
child: MIHDropdownField(
controller: _ttsVoiceController,
hintText: "AI Voice",
dropdownOptions: _voices
.map((_voice) => _voice["name"] as String)
.toList(),
required: true,
editable: true,
enableSearch: false,
),
),
const SizedBox(width: 10),
Padding(
padding: const EdgeInsets.all(5.0),
child: Container(
//color: MzanziInnovationHub.of(context)!.theme.successColor(),
decoration: BoxDecoration(
color: MzanziInnovationHub.of(context)!
.theme
.successColor(),
borderRadius: const BorderRadius.all(
Radius.circular(100),
),
),
child: IconButton(
color: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
onPressed: () {
print("Start TTS now");
_speakText(
"This is the sample of the Mzansi A.I Voice.");
},
icon: const Icon(Icons.volume_up),
),
),
),
],
),
const SizedBox(height: 15),
],
),
),
@@ -503,18 +501,6 @@ class _AiChatState extends State<AiChat> {
);
}
void setTtsVoice(String voiceName) {
_flutterTts.setVoice(
{
"name": voiceName,
"locale": _voices
.where((_voice) => _voice["name"].contains(voiceName))
.first["locale"]
},
);
_ttsVoiceController.text = _currentVoice!["name"];
}
void _speakText(String text) async {
try {
await _flutterTts.stop(); // Stop any ongoing speech
@@ -524,11 +510,23 @@ class _AiChatState extends State<AiChat> {
}
}
void setTtsVoice(String voiceName) {
_flutterTts.setVoice(
{
"name": voiceName,
"locale": _voices
.where((_voice) => _voice["name"].contains(voiceName))
.first["locale"]
},
);
_ttsVoiceController.text = voiceName;
}
void voiceSelected() {
if (_ttsVoiceController.text.isNotEmpty) {
_ttsVoiceName.value = _ttsVoiceController.text;
print(
"======================================== Voice Set ========================================");
// print(
// "======================================== Voice Set ========================================");
setTtsVoice(_ttsVoiceController.text);
} else {
_ttsVoiceName.value = "";
@@ -549,18 +547,25 @@ class _AiChatState extends State<AiChat> {
void initTTS() {
_flutterTts.setVolume(0.7);
// _flutterTts.setSpeechRate(0.6);
// _flutterTts.setPitch(1.0);
_flutterTts.getVoices.then(
(data) {
try {
_voices = List<Map>.from(data);
print("=================== Voices ===================\n$_voices");
setState(() {
_voices = _voices
.where((_voice) => _voice["name"].contains("en-us"))
.where(
(_voice) => _voice["name"].toLowerCase().contains("en-us"))
.toList();
_currentVoice = _voices.first;
setTtsVoice(_currentVoice!["name"]);
_voicesString =
_voices.map((_voice) => _voice["name"] as String).toList();
_voicesString.sort();
// print(
// "=================== Voices ===================\n$_voicesString");
setTtsVoice(_voicesString.first);
});
} catch (e) {
print(e);
@@ -582,7 +587,7 @@ class _AiChatState extends State<AiChat> {
);
_modelController.text = 'gemma2:2b';
_fontSizeController.text = _chatFrontSize.ceil().toString();
_ttsVoiceController.addListener(voiceSelected);
_chatHistory.add(
ollama.Message(
role: ollama.MessageRole.system,
@@ -591,6 +596,7 @@ class _AiChatState extends State<AiChat> {
);
_loadMessages();
initTTS();
_ttsVoiceController.addListener(voiceSelected);
}
@override