You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HMG_QLine/lib/services/text_to_speech_service.dart

121 lines
4.2 KiB
Dart

10 months ago
import 'dart:developer';
import 'package:flutter_tts/flutter_tts.dart';
import 'package:hmg_qline/models/global_config_model.dart';
import 'package:hmg_qline/models/ticket_model.dart';
import 'package:hmg_qline/utilities/enums.dart';
import 'package:hmg_qline/utilities/extensions.dart';
10 months ago
bool isNeedToBreakVoiceForArabic = true;
10 months ago
abstract class TextToSpeechService {
Future<void> speechText({
required TicketDetailsModel ticket,
required GlobalConfigurationsModel globalConfigurationsModel,
});
10 months ago
Future<void> speechTextTest(String test);
10 months ago
void listenToTextToSpeechEvents({required Function() onVoiceCompleted});
}
class TextToSpeechServiceImp implements TextToSpeechService {
FlutterTts textToSpeechInstance;
TextToSpeechServiceImp({required this.textToSpeechInstance});
double volume = 1.0;
double pitch = 0.6;
double rate = 0.2;
10 months ago
@override
Future<void> speechTextTest(String test) async {
await textToSpeechInstance.setLanguage(LanguageEnum.english.enumToString());
textToSpeechInstance.setSpeechRate(0.37);
textToSpeechInstance.setPitch(0.8);
await textToSpeechInstance.speak(test);
}
10 months ago
@override
Future<void> speechText({
required TicketDetailsModel ticket,
required GlobalConfigurationsModel globalConfigurationsModel,
}) async {
10 months ago
const ttsGoogleEngine = 'com.google.android.tts';
// const ttsFlyTecEngine = 'com.iflytek.speechcloud';
10 months ago
LanguageEnum langEnum = ticket.ticketModel!.voiceLanguageEnum;
10 months ago
List engines = await textToSpeechInstance.getEngines;
if (engines.contains(ttsGoogleEngine)) {
await textToSpeechInstance.setEngine(ttsGoogleEngine);
}
10 months ago
textToSpeechInstance.setVolume(1.0);
10 months ago
if (langEnum == LanguageEnum.arabic) {
await textToSpeechInstance.setLanguage(LanguageEnum.arabic.enumToString());
textToSpeechInstance.setSpeechRate(0.45);
textToSpeechInstance.setPitch(0.8);
} else if (langEnum == LanguageEnum.english) {
await textToSpeechInstance.setLanguage(LanguageEnum.english.enumToString());
textToSpeechInstance.setSpeechRate(0.37);
textToSpeechInstance.setPitch(0.85);
}
10 months ago
// String postVoice = globalConfigurationsModel.postVoiceText;
10 months ago
// String preVoice = ticket.ticketModel!.ticketNoText;
10 months ago
10 months ago
String postVoice = ticket.ticketModel!.postVoiceText;
10 months ago
String preVoice = '';
String roomNo = '';
if (ticket.ticketModel!.roomNo != null && ticket.ticketModel!.roomNo!.isNotEmpty) {
roomNo = ticket.ticketModel!.roomNo.toString();
}
if (preVoice.isNotEmpty) {
preVoice = '$preVoice..';
}
10 months ago
String ticketNo = ticket.ticketModel!.queueNo!.trim().toString();
log("lang: ${await textToSpeechInstance.areLanguagesInstalled(["en", "ar"])}");
10 months ago
log("getDefaultEngine: ${await textToSpeechInstance.getDefaultEngine}");
log("getEngines: ${await textToSpeechInstance.getEngines}");
10 months ago
log("lang: $langEnum");
log("preVoice: $preVoice");
log("postVoice: $postVoice");
log("ticketNo: $ticketNo");
String patientAlpha = "";
String patientNumeric = "";
10 months ago
var queueNoArray = ticketNo.split("-");
if (queueNoArray.length > 2) {
patientAlpha = "${queueNoArray[0]} .. ${queueNoArray[1]}";
patientNumeric = queueNoArray[2];
10 months ago
} else {
10 months ago
patientAlpha = queueNoArray[0];
patientNumeric = queueNoArray[1];
10 months ago
}
10 months ago
if (langEnum == LanguageEnum.english) {
await textToSpeechInstance.speak("$preVoice $patientAlpha .. $patientNumeric .. $postVoice $roomNo");
10 months ago
return;
}
if (isNeedToBreakVoiceForArabic) {
10 months ago
if (preVoice.isNotEmpty) {
await textToSpeechInstance.speak("$preVoice ");
}
textToSpeechInstance.setLanguage(LanguageEnum.english.enumToString());
await textToSpeechInstance.speak(" $patientAlpha .. $patientNumeric .. ");
await textToSpeechInstance.setLanguage(langEnum.enumToString());
10 months ago
await textToSpeechInstance.speak(" $postVoice $roomNo");
10 months ago
} else {
10 months ago
await textToSpeechInstance.speak("$preVoice $patientAlpha .. .. $patientNumeric .. .. $postVoice $roomNo");
10 months ago
}
}
@override
void listenToTextToSpeechEvents({required Function() onVoiceCompleted}) {
textToSpeechInstance.setCompletionHandler(onVoiceCompleted);
}
}