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.
106 lines
3.7 KiB
Dart
106 lines
3.7 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';
|
||
|
|
|
||
|
|
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);
|
||
|
|
|
||
|
|
LanguageEnum langEnum = globalConfigurationsModel.voiceLanguageEnum;
|
||
|
|
String preVoice = globalConfigurationsModel.ticketNoText;
|
||
|
|
String postVoice = globalConfigurationsModel.postVoiceText;
|
||
|
|
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
|
||
|
|
if (postVoice.isNotEmpty) {
|
||
|
|
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.1);
|
||
|
|
if (isNeedToBreakVoiceForArabic) {
|
||
|
|
await textToSpeechInstance.speak("$preVoice .. ");
|
||
|
|
await textToSpeechInstance.setLanguage("en");
|
||
|
|
await textToSpeechInstance.speak("$clinicName .. $patientAlpha .. $patientNumeric .. ");
|
||
|
|
await textToSpeechInstance.setLanguage(langEnum.enumToString());
|
||
|
|
await textToSpeechInstance.speak(postVoice);
|
||
|
|
} else {
|
||
|
|
log("here");
|
||
|
|
await textToSpeechInstance.speak("$preVoice .. .. $clinicName .. $patientAlpha .. .. $patientNumeric .. .. $postVoice");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
void listenToTextToSpeechEvents({required Function() onVoiceCompleted}) {
|
||
|
|
textToSpeechInstance.setCompletionHandler(onVoiceCompleted);
|
||
|
|
}
|
||
|
|
}
|