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; // bool _ttsOn = false;
String? textStream; String? textStream;
List<Map> _voices = []; List<Map> _voices = [];
Map? _currentVoice; List<String> _voicesString = [];
List<types.Message> _messages = []; List<types.Message> _messages = [];
late types.User _user; late types.User _user;
late types.User _mihAI; late types.User _mihAI;
@@ -405,6 +405,50 @@ class _AiChatState extends State<AiChat> {
], ],
), ),
const SizedBox(height: 15), 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( Row(
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
children: [ children: [
@@ -446,52 +490,6 @@ class _AiChatState extends State<AiChat> {
], ],
), ),
const SizedBox(height: 15), 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 { void _speakText(String text) async {
try { try {
await _flutterTts.stop(); // Stop any ongoing speech 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() { void voiceSelected() {
if (_ttsVoiceController.text.isNotEmpty) { if (_ttsVoiceController.text.isNotEmpty) {
_ttsVoiceName.value = _ttsVoiceController.text; _ttsVoiceName.value = _ttsVoiceController.text;
print( // print(
"======================================== Voice Set ========================================"); // "======================================== Voice Set ========================================");
setTtsVoice(_ttsVoiceController.text); setTtsVoice(_ttsVoiceController.text);
} else { } else {
_ttsVoiceName.value = ""; _ttsVoiceName.value = "";
@@ -549,18 +547,25 @@ class _AiChatState extends State<AiChat> {
void initTTS() { void initTTS() {
_flutterTts.setVolume(0.7); _flutterTts.setVolume(0.7);
// _flutterTts.setSpeechRate(0.6);
// _flutterTts.setPitch(1.0);
_flutterTts.getVoices.then( _flutterTts.getVoices.then(
(data) { (data) {
try { try {
_voices = List<Map>.from(data); _voices = List<Map>.from(data);
print("=================== Voices ===================\n$_voices");
setState(() { setState(() {
_voices = _voices _voices = _voices
.where((_voice) => _voice["name"].contains("en-us")) .where(
(_voice) => _voice["name"].toLowerCase().contains("en-us"))
.toList(); .toList();
_currentVoice = _voices.first; _voicesString =
setTtsVoice(_currentVoice!["name"]); _voices.map((_voice) => _voice["name"] as String).toList();
_voicesString.sort();
// print(
// "=================== Voices ===================\n$_voicesString");
setTtsVoice(_voicesString.first);
}); });
} catch (e) { } catch (e) {
print(e); print(e);
@@ -582,7 +587,7 @@ class _AiChatState extends State<AiChat> {
); );
_modelController.text = 'gemma2:2b'; _modelController.text = 'gemma2:2b';
_fontSizeController.text = _chatFrontSize.ceil().toString(); _fontSizeController.text = _chatFrontSize.ceil().toString();
_ttsVoiceController.addListener(voiceSelected);
_chatHistory.add( _chatHistory.add(
ollama.Message( ollama.Message(
role: ollama.MessageRole.system, role: ollama.MessageRole.system,
@@ -591,6 +596,7 @@ class _AiChatState extends State<AiChat> {
); );
_loadMessages(); _loadMessages();
initTTS(); initTTS();
_ttsVoiceController.addListener(voiceSelected);
} }
@override @override