forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsockets.dart
More file actions
165 lines (141 loc) · 4.32 KB
/
Copy pathsockets.dart
File metadata and controls
165 lines (141 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import 'dart:async';
import 'package:omi/backend/schema/bt_device/bt_device.dart';
import 'package:omi/models/custom_stt_config.dart';
import 'package:omi/services/sockets/transcription_service.dart';
import 'package:omi/utils/logger.dart';
import 'package:omi/utils/mutex.dart';
export 'package:omi/services/freemium_transcription_service.dart';
abstract class ISocketService {
void start();
void stop();
Future<TranscriptSegmentSocketService?> conversation({
required BleAudioCodec codec,
required int sampleRate,
required String language,
bool force = false,
String? source,
CustomSttConfig? customSttConfig,
});
Future<TranscriptSegmentSocketService?> speechProfile({
required BleAudioCodec codec,
required int sampleRate,
required String language,
bool force = false,
String? source,
});
}
abstract interface class ISocketServiceSubsciption {}
class SocketServicePool extends ISocketService {
TranscriptSegmentSocketService? _socket;
TranscriptSegmentSocketService? _speechProfileSocket;
@override
void start() {}
@override
void stop() async {
await _socket?.stop();
await _speechProfileSocket?.stop();
}
// Warn: Should use a better solution to prevent race conditions
final Mutex _mutex = Mutex();
Future<TranscriptSegmentSocketService?> socket({
required BleAudioCodec codec,
required int sampleRate,
required String language,
bool force = false,
String? source,
CustomSttConfig? customSttConfig,
}) async {
await _mutex.acquire();
try {
final sttConfigId = customSttConfig?.sttConfigId ?? 'omi:default';
// Check if we can reuse existing socket (same codec, sample rate, config, and connected)
if (!force &&
_socket?.codec == codec &&
_socket?.sampleRate == sampleRate &&
_socket?.state == SocketServiceState.connected &&
_socket?.sttConfigId == sttConfigId) {
Logger.debug("Reusing existing socket connection");
return _socket;
}
Logger.debug(
"_connect force=$force state=${_socket?.state} configChanged=${_socket?.sttConfigId != sttConfigId}",
);
// new socket
await _socket?.stop();
if (customSttConfig != null && customSttConfig.isEnabled) {
_socket = TranscriptSocketServiceFactory.createFromCustomConfig(
sampleRate,
codec,
language,
customSttConfig,
source: source,
);
} else {
_socket = TranscriptSocketServiceFactory.createDefault(
sampleRate,
codec,
language,
source: source,
sttConfigId: sttConfigId,
);
}
await _socket?.start();
if (_socket?.state != SocketServiceState.connected) {
return null;
}
return _socket;
} finally {
_mutex.release();
}
}
@override
Future<TranscriptSegmentSocketService?> conversation({
required BleAudioCodec codec,
required int sampleRate,
required String language,
bool force = false,
String? source,
CustomSttConfig? customSttConfig,
}) async {
Logger.debug(
"socket conversation > $codec $sampleRate $force source: $source customStt: ${customSttConfig?.provider}",
);
return await socket(
codec: codec,
sampleRate: sampleRate,
language: language,
force: force,
source: source,
customSttConfig: customSttConfig,
);
}
@override
Future<TranscriptSegmentSocketService?> speechProfile({
required BleAudioCodec codec,
required int sampleRate,
required String language,
bool force = false,
String? source,
}) async {
Logger.debug("socket speech profile > $codec $sampleRate $force source: $source");
await _mutex.acquire();
try {
// Use separate socket for speech profile to avoid conflicts with conversation socket
await _speechProfileSocket?.stop();
_speechProfileSocket = SpeechProfileTranscriptSegmentSocketService.create(
sampleRate,
codec,
language,
source: source,
onboardingMode: true,
);
await _speechProfileSocket?.start();
if (_speechProfileSocket?.state != SocketServiceState.connected) {
return null;
}
return _speechProfileSocket;
} finally {
_mutex.release();
}
}
}