You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.8 KiB
108 lines
2.8 KiB
import 'package:get/get.dart';
|
|
import 'asr_service.dart';
|
|
import 'tts_service.dart';
|
|
import 'speech_impl/azure_asr_service.dart';
|
|
import 'speech_impl/azure_tts_service.dart';
|
|
import 'speech_impl/flutter_asr_service.dart';
|
|
import 'speech_impl/flutter_tts_service.dart';
|
|
|
|
import '../../core/utils/logger.dart';
|
|
|
|
/// 语音服务类型
|
|
enum SpeechServiceType {
|
|
/// Azure 语音服务
|
|
azure,
|
|
|
|
/// Flutter 本地语音服务
|
|
flutter,
|
|
}
|
|
|
|
/// 语音服务工厂类
|
|
///
|
|
/// 用于根据配置信息获取不同的TTS和ASR实现
|
|
class SpeechFactory extends GetxService {
|
|
static SpeechFactory get to => Get.find<SpeechFactory>();
|
|
|
|
/// 当前使用的语音服务类型
|
|
SpeechServiceType _currentServiceType = SpeechServiceType.azure;
|
|
|
|
/// 获取当前使用的语音服务类型
|
|
SpeechServiceType get currentServiceType => _currentServiceType;
|
|
|
|
/// 设置语音服务类型
|
|
void setServiceType(SpeechServiceType type) {
|
|
if (_currentServiceType != type) {
|
|
_currentServiceType = type;
|
|
Logger.info('语音服务类型已切换为: ${type.toString().split('.').last}');
|
|
|
|
// 重新注册服务
|
|
_registerServices();
|
|
}
|
|
}
|
|
|
|
/// 注册语音服务
|
|
void _registerServices() {
|
|
// 注销现有服务
|
|
if (Get.isRegistered<AsrService>()) {
|
|
Get.delete<AsrService>(force: true);
|
|
}
|
|
|
|
if (Get.isRegistered<TtsService>()) {
|
|
Get.delete<TtsService>(force: true);
|
|
}
|
|
|
|
// 根据类型注册新服务
|
|
switch (_currentServiceType) {
|
|
case SpeechServiceType.azure:
|
|
Get.lazyPut<AsrService>(() => AzureAsrService());
|
|
Get.lazyPut<TtsService>(() => AzureTtsService());
|
|
break;
|
|
case SpeechServiceType.flutter:
|
|
Get.lazyPut<AsrService>(() => FlutterAsrService());
|
|
Get.lazyPut<TtsService>(() => FlutterTtsService());
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// 初始化语音服务
|
|
Future<void> initialize({SpeechServiceType? initialType}) async {
|
|
try {
|
|
// 如果指定了初始类型,则设置
|
|
if (initialType != null) {
|
|
_currentServiceType = initialType;
|
|
}
|
|
|
|
// 注册服务
|
|
_registerServices();
|
|
|
|
// // 初始化服务
|
|
// final asrService = Get.find<AsrService>();
|
|
// final ttsService = Get.find<TtsService>();
|
|
|
|
// await asrService.initialize();
|
|
// await ttsService.initialize();
|
|
|
|
Logger.info(
|
|
'语音服务初始化完成,当前类型: ${_currentServiceType.toString().split('.').last}');
|
|
} catch (e) {
|
|
Logger.error('语音服务初始化失败: $e');
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
/// 获取ASR服务
|
|
AsrService getAsrService() {
|
|
if (!Get.isRegistered<AsrService>()) {
|
|
_registerServices();
|
|
}
|
|
return Get.find<AsrService>();
|
|
}
|
|
|
|
/// 获取TTS服务
|
|
TtsService getTtsService() {
|
|
if (!Get.isRegistered<TtsService>()) {
|
|
_registerServices();
|
|
}
|
|
return Get.find<TtsService>();
|
|
}
|
|
}
|
|
|