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

102 lines
3.5 KiB
Dart

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';
bool isNeedToBreakVoiceForArabic = false;
abstract class TextToSpeechService {
Future<void> speechText({
required TicketDetailsModel ticket,
required GlobalConfigurationsModel globalConfigurationsModel,
});
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;
@override
Future<void> speechText({
required TicketDetailsModel ticket,
required GlobalConfigurationsModel globalConfigurationsModel,
}) async {
textToSpeechInstance.setVolume(1.0);
textToSpeechInstance.setSpeechRate(0.45);
textToSpeechInstance.setPitch(0.9);
// String postVoice = globalConfigurationsModel.postVoiceText;
String postVoice = '';
LanguageEnum langEnum = globalConfigurationsModel.voiceLanguageEnum;
String preVoice = globalConfigurationsModel.ticketNoText;
String ticketNo = ticket.ticketModel!.queueNo!.trim().toString();
bool isClinicNameAdded = false;
log("lang: ${await textToSpeechInstance.areLanguagesInstalled(["en", "ar"])}");
log("lang: $langEnum");
log("preVoice: $preVoice");
log("postVoice: $postVoice");
log("ticketNo: $ticketNo");
log("isClinicNameAdded: $isClinicNameAdded");
String clinicName = "";
String patientAlpha = "";
String patientNumeric = "";
if (isClinicNameAdded) {
var clinic = ticketNo.split(" ");
clinicName = clinic[0];
var queueNoArray = clinic[1].split("-");
if (queueNoArray.length > 2) {
patientAlpha = "${queueNoArray[0]} .. ${queueNoArray[1]}";
patientNumeric = queueNoArray[2];
} else {
patientAlpha = queueNoArray[0];
patientNumeric = queueNoArray[1];
}
} else {
var queueNoArray = ticketNo.split("-");
if (queueNoArray.length > 2) {
patientAlpha = "${queueNoArray[0]} .. ${queueNoArray[1]}";
patientNumeric = queueNoArray[2];
} else {
patientAlpha = queueNoArray[0];
patientNumeric = queueNoArray[1];
}
}
log("Speech: $preVoice .. $clinicName .. $patientAlpha .. $patientNumeric .. $postVoice");
// Create Pre Voice Players
log('lang $langEnum');
if (langEnum != LanguageEnum.arabic) {
await textToSpeechInstance.setLanguage(langEnum.enumToString());
await textToSpeechInstance.speak("$preVoice .. $clinicName .. $patientAlpha .. $patientNumeric .. $postVoice");
return;
}
await textToSpeechInstance.setLanguage(langEnum.enumToString());
textToSpeechInstance.setPitch(1.3);
if (isNeedToBreakVoiceForArabic) {
await textToSpeechInstance.speak("$preVoice .. ");
await textToSpeechInstance.setLanguage("en");
await textToSpeechInstance.speak("$clinicName .. $patientAlpha .. $patientNumeric .. ");
} else {
await textToSpeechInstance.speak("$preVoice .. .. $clinicName .. $patientAlpha .. .. $patientNumeric .. .. $postVoice");
}
}
@override
void listenToTextToSpeechEvents({required Function() onVoiceCompleted}) {
textToSpeechInstance.setCompletionHandler(onVoiceCompleted);
}
}