-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigstore.cpp
More file actions
325 lines (283 loc) · 13.9 KB
/
configstore.cpp
File metadata and controls
325 lines (283 loc) · 13.9 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "configstore.h"
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QJsonObject>
#include <QJsonParseError>
#include <QSaveFile>
#include <QStandardPaths>
#include <QtGlobal>
namespace {
/// Soniox max endpoint delay bounds (ms) from API documentation.
constexpr int SONIOX_MIN_ENDPOINT_DELAY_MS = 500;
constexpr int SONIOX_MAX_ENDPOINT_DELAY_MS = 3000;
/// Soniox default max endpoint delay (ms) from API documentation.
constexpr int SONIOX_DEFAULT_ENDPOINT_DELAY_MS = 2000;
/// Yandex max pause hint bounds (ms) from API documentation.
constexpr int YANDEX_MIN_PAUSE_HINT_MS = 0;
constexpr int YANDEX_MAX_PAUSE_HINT_MS = 5000;
[[nodiscard]] int clampSonioxEndpointDelay(const int value) {
return qBound(SONIOX_MIN_ENDPOINT_DELAY_MS, value, SONIOX_MAX_ENDPOINT_DELAY_MS);
}
[[nodiscard]] int clampYandexPauseHint(const int value) {
return qBound(YANDEX_MIN_PAUSE_HINT_MS, value, YANDEX_MAX_PAUSE_HINT_MS);
}
QJsonObject settingsToJson(const AppSettings &settings) {
return QJsonObject{
{"provider", settings.providerId},
{"hotkey", settings.hotkey},
{"finalization_hotkey", settings.finalizationHotkey},
{"input_device", settings.inputDeviceId}
};
}
QJsonObject sonioxToJson(const SonioxSettings &settings) {
return QJsonObject{
{"proxy", settings.proxy},
{"endpoint", settings.endpoint},
{"api_key", settings.apiKey},
{"model", settings.model},
{"language_hints", settings.languageHints},
{"language_hints_strict", settings.languageHintsStrict},
{"context", settings.context},
{"endpoint_detection", settings.endpointDetection},
{"use_max_endpoint_delay", settings.useMaxEndpointDelay},
{"max_endpoint_delay_ms", settings.maxEndpointDelayMs},
{"keep_connection", settings.keepConnection}
};
}
QJsonObject azureToJson(const AzureSettings &settings) {
return QJsonObject{
{"proxy", settings.proxy},
{"endpoint", settings.endpoint},
{"api_key", settings.apiKey},
{"endpoint_id", settings.endpointId},
{"language", settings.language},
{"language_hints", settings.languageHints},
{"language_hints_strict", settings.languageHintsStrict},
{"phrase_hints", settings.phraseHints},
{"keep_connection", settings.keepConnection}
};
}
QJsonObject deepgramToJson(const DeepgramSettings &settings) {
return QJsonObject{
{"proxy", settings.proxy},
{"endpoint", settings.endpoint},
{"api_key", settings.apiKey},
{"model", settings.model},
{"language", settings.language},
{"punctuate", settings.punctuate},
{"interim_results", settings.interimResults},
{"smart_format", settings.smartFormat},
{"keep_connection", settings.keepConnection}
};
}
QJsonObject yandexToJson(const YandexSettings &settings) {
return QJsonObject{
{"endpoint", settings.endpoint},
{"api_key", settings.apiKey},
{"folder_id", settings.folderId},
{"language", settings.language},
{"model", settings.model},
{"use_api_key", settings.useApiKey},
{"text_normalization", settings.textNormalization},
{"literature_text", settings.literatureText},
{"profanity_filter", settings.profanityFilter},
{"use_max_pause_between_words_hint", settings.useMaxPauseBetweenWordsHint},
{"max_pause_between_words_hint_ms", settings.maxPauseBetweenWordsHintMs}
};
}
QJsonObject visualStyleToJson(const VisualStyleSettings &settings) {
return QJsonObject{
{"border_color", settings.borderColor},
{"glow_color", settings.glowColor},
{"pulse_speed", settings.pulseSpeedHz},
{"glow_size", settings.glowSizePx},
{"show_rec", settings.showRecBadge},
{"blink_rec_dot", settings.blinkRecDot}
};
}
} // namespace
QString ConfigStore::configFilePath() {
const QString configDir = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation)
+ QDir::separator()
+ QCoreApplication::applicationName();
QDir().mkpath(configDir);
return configDir + QDir::separator() + "config.json";
}
AppConfig ConfigStore::defaultConfig() {
AppConfig config;
config.settings.providerId = "soniox";
config.settings.hotkey = "Ctrl+Space";
config.settings.finalizationHotkey = "F12";
config.settings.inputDeviceId = "";
config.soniox.proxy = "";
config.soniox.endpoint = "wss://stt-rt.soniox.com/transcribe-websocket";
config.soniox.apiKey = "";
config.soniox.model = "stt-rt-v3";
config.soniox.languageHints = "";
config.soniox.languageHintsStrict = false;
config.soniox.context = "";
config.soniox.endpointDetection = true;
config.soniox.useMaxEndpointDelay = false;
config.soniox.maxEndpointDelayMs = SONIOX_DEFAULT_ENDPOINT_DELAY_MS;
config.soniox.keepConnection = false;
config.azure.proxy = "";
config.azure.endpoint = "wss://eastus.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1";
config.azure.apiKey = "";
config.azure.endpointId = "";
config.azure.language = "en-US";
config.azure.languageHints = "";
config.azure.languageHintsStrict = false;
config.azure.phraseHints = "";
config.azure.keepConnection = false;
config.deepgram.proxy = "";
config.deepgram.endpoint = "wss://api.deepgram.com/v1/listen";
config.deepgram.apiKey = "";
config.deepgram.model = "nova-3";
config.deepgram.language = "en-US";
config.deepgram.punctuate = true;
config.deepgram.interimResults = true;
config.deepgram.smartFormat = false;
config.deepgram.keepConnection = false;
config.yandex.endpoint = "stt.api.cloud.yandex.net:443";
config.yandex.apiKey = "";
config.yandex.folderId = "";
config.yandex.language = "en-US";
config.yandex.model = "general";
config.yandex.useApiKey = true;
config.yandex.textNormalization = true;
config.yandex.literatureText = true;
config.yandex.profanityFilter = false;
config.yandex.useMaxPauseBetweenWordsHint = false;
config.yandex.maxPauseBetweenWordsHintMs = YANDEX_MIN_PAUSE_HINT_MS;
config.visualStyle.borderColor = "#EC46FF";
config.visualStyle.glowColor = "#EC46FF";
config.visualStyle.pulseSpeedHz = 1.4;
config.visualStyle.glowSizePx = 20;
config.visualStyle.showRecBadge = true;
config.visualStyle.blinkRecDot = true;
return config;
}
AppConfig ConfigStore::fromJson(const QJsonDocument &doc, bool *ok) {
if (ok)
*ok = false;
AppConfig config = defaultConfig();
if (doc.isNull() || !doc.isObject())
return config;
const QJsonObject root = doc.object();
const QJsonObject settings = root.value("settings").toObject();
const QJsonObject soniox = root.value("soniox").toObject();
const QJsonObject azure = root.value("azure").toObject();
const QJsonObject deepgram = root.value("deepgram").toObject();
const QJsonObject yandex = root.value("yandex").toObject();
const QJsonObject visualStyle = root.value("visual_style").toObject();
const bool hasFinalizationHotkey = settings.contains("finalization_hotkey");
config.settings.providerId = settings.value("provider").toString(config.settings.providerId);
config.settings.hotkey = settings.value("hotkey").toString(config.settings.hotkey);
config.settings.finalizationHotkey = settings.value("finalization_hotkey")
.toString(config.settings.finalizationHotkey);
config.settings.inputDeviceId = settings.value("input_device").toString(config.settings.inputDeviceId);
config.soniox.proxy = soniox.value("proxy").toString();
config.soniox.endpoint = soniox.value("endpoint").toString(config.soniox.endpoint);
config.soniox.apiKey = soniox.value("api_key").toString();
config.soniox.model = soniox.value("model").toString(config.soniox.model);
config.soniox.languageHints = soniox.value("language_hints").toString();
config.soniox.languageHintsStrict = soniox.value("language_hints_strict")
.toBool(config.soniox.languageHintsStrict);
config.soniox.context = soniox.value("context").toString();
config.soniox.endpointDetection = soniox.value("endpoint_detection").toBool(config.soniox.endpointDetection);
config.soniox.useMaxEndpointDelay = soniox.value("use_max_endpoint_delay")
.toBool(config.soniox.useMaxEndpointDelay);
config.soniox.maxEndpointDelayMs = clampSonioxEndpointDelay(
soniox.value("max_endpoint_delay_ms").toInt(config.soniox.maxEndpointDelayMs));
config.soniox.keepConnection = soniox.value("keep_connection").toBool(config.soniox.keepConnection);
config.azure.proxy = azure.value("proxy").toString();
config.azure.endpoint = azure.value("endpoint").toString(config.azure.endpoint);
config.azure.apiKey = azure.value("api_key").toString();
config.azure.endpointId = azure.value("endpoint_id").toString();
config.azure.language = azure.value("language").toString(config.azure.language);
config.azure.languageHints = azure.value("language_hints").toString();
config.azure.languageHintsStrict = azure.value("language_hints_strict")
.toBool(config.azure.languageHintsStrict);
config.azure.phraseHints = azure.value("phrase_hints").toString();
config.azure.keepConnection = azure.value("keep_connection").toBool(config.azure.keepConnection);
config.deepgram.proxy = deepgram.value("proxy").toString();
config.deepgram.endpoint = deepgram.value("endpoint").toString(config.deepgram.endpoint);
config.deepgram.apiKey = deepgram.value("api_key").toString();
config.deepgram.model = deepgram.value("model").toString(config.deepgram.model);
config.deepgram.language = deepgram.value("language").toString(config.deepgram.language);
config.deepgram.punctuate = deepgram.value("punctuate").toBool(config.deepgram.punctuate);
config.deepgram.interimResults = deepgram.value("interim_results").toBool(config.deepgram.interimResults);
config.deepgram.smartFormat = deepgram.value("smart_format").toBool(config.deepgram.smartFormat);
config.deepgram.keepConnection = deepgram.value("keep_connection").toBool(config.deepgram.keepConnection);
config.yandex.endpoint = yandex.value("endpoint").toString(config.yandex.endpoint);
config.yandex.apiKey = yandex.value("api_key").toString();
config.yandex.folderId = yandex.value("folder_id").toString();
config.yandex.language = yandex.value("language").toString(config.yandex.language);
config.yandex.model = yandex.value("model").toString(config.yandex.model);
config.yandex.useApiKey = yandex.value("use_api_key").toBool(config.yandex.useApiKey);
config.yandex.textNormalization = yandex.value("text_normalization").toBool(config.yandex.textNormalization);
config.yandex.literatureText = yandex.value("literature_text").toBool(config.yandex.literatureText);
config.yandex.profanityFilter = yandex.value("profanity_filter").toBool(config.yandex.profanityFilter);
config.yandex.useMaxPauseBetweenWordsHint = yandex.value("use_max_pause_between_words_hint")
.toBool(config.yandex.useMaxPauseBetweenWordsHint);
config.yandex.maxPauseBetweenWordsHintMs = clampYandexPauseHint(
yandex.value("max_pause_between_words_hint_ms").toInt(config.yandex.maxPauseBetweenWordsHintMs));
if (!hasFinalizationHotkey) {
const QString legacyEouHotkey = yandex.value("eou_hotkey").toString();
if (!legacyEouHotkey.trimmed().isEmpty())
config.settings.finalizationHotkey = legacyEouHotkey;
}
config.visualStyle.borderColor = visualStyle.value("border_color").toString(config.visualStyle.borderColor);
config.visualStyle.glowColor = visualStyle.value("glow_color").toString(config.visualStyle.glowColor);
config.visualStyle.pulseSpeedHz = visualStyle.value("pulse_speed").toDouble(config.visualStyle.pulseSpeedHz);
config.visualStyle.glowSizePx = visualStyle.value("glow_size").toInt(config.visualStyle.glowSizePx);
config.visualStyle.showRecBadge = visualStyle.value("show_rec").toBool(config.visualStyle.showRecBadge);
config.visualStyle.blinkRecDot = visualStyle.value("blink_rec_dot").toBool(config.visualStyle.blinkRecDot);
if (config.soniox.proxy.isEmpty())
config.soniox.proxy = settings.value("proxy").toString();
if (ok)
*ok = true;
return config;
}
QJsonDocument ConfigStore::toJson(const AppConfig &config) {
QJsonObject root{
{"settings", settingsToJson(config.settings)},
{"soniox", sonioxToJson(config.soniox)},
{"azure", azureToJson(config.azure)},
{"deepgram", deepgramToJson(config.deepgram)},
{"yandex", yandexToJson(config.yandex)},
{"visual_style", visualStyleToJson(config.visualStyle)}
};
return QJsonDocument(root);
}
bool ConfigStore::loadFromFile(const QString &path, AppConfig *config) {
if (!config || path.trimmed().isEmpty())
return false;
QFile file(path);
if (!file.open(QIODevice::ReadOnly))
return false;
const QByteArray payload = file.readAll();
file.close();
QJsonParseError parseError;
const QJsonDocument doc = QJsonDocument::fromJson(payload, &parseError);
if (parseError.error != QJsonParseError::NoError)
return false;
bool ok = false;
const AppConfig parsed = fromJson(doc, &ok);
if (!ok)
return false;
*config = parsed;
return true;
}
bool ConfigStore::saveToFile(const QString &path, const AppConfig &config) {
if (path.trimmed().isEmpty())
return false;
QSaveFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
return false;
const QJsonDocument doc = toJson(config);
if (file.write(doc.toJson()) < 0)
return false;
return file.commit();
}