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
199 lines (175 loc) · 5.71 KB
/
Copy pathsockets.dart
File metadata and controls
199 lines (175 loc) · 5.71 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import 'dart:async';
import 'package:omi/backend/schema/bt_device/bt_device.dart';
import 'package:omi/backend/schema/geolocation.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,
String? clientConversationId,
CustomSttConfig? customSttConfig,
Geolocation? geolocation,
});
/// [customSttConfig] switches the session to on-device transcription
/// (see TranscriptSocketServiceFactory.createSpeechProfileOnDevice); null
/// uses the backend's streaming STT.
Future<TranscriptSegmentSocketService?> speechProfile({
required BleAudioCodec codec,
required int sampleRate,
required String language,
bool force = false,
String? source,
bool speechProfileRedo = false,
CustomSttConfig? customSttConfig,
});
}
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,
String? clientConversationId,
CustomSttConfig? customSttConfig,
Geolocation? geolocation,
}) 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 &&
_socket?.geolocation?.time == geolocation?.time &&
_socket?.clientConversationId == clientConversationId) {
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,
geolocation: geolocation,
clientConversationId: clientConversationId,
);
} else {
_socket = TranscriptSocketServiceFactory.createDefault(
sampleRate,
codec,
language,
source: source,
sttConfigId: sttConfigId,
geolocation: geolocation,
clientConversationId: clientConversationId,
);
}
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,
String? clientConversationId,
CustomSttConfig? customSttConfig,
Geolocation? geolocation,
}) 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,
clientConversationId: clientConversationId,
customSttConfig: customSttConfig,
geolocation: geolocation,
);
}
@override
Future<TranscriptSegmentSocketService?> speechProfile({
required BleAudioCodec codec,
required int sampleRate,
required String language,
bool force = false,
String? source,
bool speechProfileRedo = false,
CustomSttConfig? customSttConfig,
}) async {
Logger.debug(
"socket speech profile > $codec $sampleRate $force source: $source localStt: ${customSttConfig?.provider.name}",
);
await _mutex.acquire();
try {
// Use separate socket for speech profile to avoid conflicts with conversation socket
await _speechProfileSocket?.stop();
_speechProfileSocket = customSttConfig != null
? TranscriptSocketServiceFactory.createSpeechProfileOnDevice(
sampleRate,
codec,
language,
customSttConfig,
source: source,
speechProfileRedo: speechProfileRedo,
)
: SpeechProfileTranscriptSegmentSocketService.create(
sampleRate,
codec,
language,
source: source,
onboardingMode: true,
speechProfileRedo: speechProfileRedo,
);
await _speechProfileSocket?.start();
if (_speechProfileSocket?.state != SocketServiceState.connected) {
return null;
}
return _speechProfileSocket;
} finally {
_mutex.release();
}
}
}