Audio.svelte 8.34 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script lang="ts">
2
	import { getAudioConfig, updateAudioConfig } from '$lib/apis/audio';
Timothy J. Baek's avatar
Timothy J. Baek committed
3
	import { user } from '$lib/stores';
4
	import { createEventDispatcher, onMount, getContext } from 'svelte';
Jannik Streidl's avatar
Jannik Streidl committed
5
	import { toast } from 'svelte-sonner';
Timothy J. Baek's avatar
Timothy J. Baek committed
6
7
	const dispatch = createEventDispatcher();

8
9
	const i18n = getContext('i18n');

Timothy J. Baek's avatar
Timothy J. Baek committed
10
11
	export let saveSettings: Function;

Timothy J. Baek's avatar
Timothy J. Baek committed
12
	// Audio
Timothy J. Baek's avatar
Timothy J. Baek committed
13

14
15
16
	let OpenAIUrl = '';
	let OpenAIKey = '';

Timothy J. Baek's avatar
Timothy J. Baek committed
17
18
19
	let STTEngines = ['', 'openai'];
	let STTEngine = '';

Timothy J. Baek's avatar
Timothy J. Baek committed
20
	let conversationMode = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
21
22
23
	let speechAutoSend = false;
	let responseAutoPlayback = false;

Timothy J. Baek's avatar
Timothy J. Baek committed
24
25
	let TTSEngines = ['', 'openai'];
	let TTSEngine = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
26
27
28

	let voices = [];
	let speaker = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
29

Timothy J. Baek's avatar
Timothy J. Baek committed
30
31
32
33
34
35
36
37
38
39
	const getOpenAIVoices = () => {
		voices = [
			{ name: 'alloy' },
			{ name: 'echo' },
			{ name: 'fable' },
			{ name: 'onyx' },
			{ name: 'nova' },
			{ name: 'shimmer' }
		];
	};
Timothy J. Baek's avatar
Timothy J. Baek committed
40

Timothy J. Baek's avatar
Timothy J. Baek committed
41
	const getWebAPIVoices = () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
42
		const getVoicesLoop = setInterval(async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
43
			voices = await speechSynthesis.getVoices();
Timothy J. Baek's avatar
Timothy J. Baek committed
44
45

			// do your loop
Timothy J. Baek's avatar
Timothy J. Baek committed
46
			if (voices.length > 0) {
Timothy J. Baek's avatar
Timothy J. Baek committed
47
48
49
				clearInterval(getVoicesLoop);
			}
		}, 100);
Timothy J. Baek's avatar
Timothy J. Baek committed
50
51
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
52
53
	const toggleConversationMode = async () => {
		conversationMode = !conversationMode;
Timothy J. Baek's avatar
Timothy J. Baek committed
54
55
56
57
58
59
60
61
62
63
64

		if (conversationMode) {
			responseAutoPlayback = true;
			speechAutoSend = true;
		}

		saveSettings({
			conversationMode: conversationMode,
			responseAutoPlayback: responseAutoPlayback,
			speechAutoSend: speechAutoSend
		});
Timothy J. Baek's avatar
Timothy J. Baek committed
65
66
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
67
68
69
70
71
72
73
74
75
76
	const toggleResponseAutoPlayback = async () => {
		responseAutoPlayback = !responseAutoPlayback;
		saveSettings({ responseAutoPlayback: responseAutoPlayback });
	};

	const toggleSpeechAutoSend = async () => {
		speechAutoSend = !speechAutoSend;
		saveSettings({ speechAutoSend: speechAutoSend });
	};

77
78
79
80
81
82
83
84
85
86
87
88
	const updateConfigHandler = async () => {
		const res = await updateAudioConfig(localStorage.token, {
			url: OpenAIUrl,
			key: OpenAIKey
		});

		if (res) {
			OpenAIUrl = res.OPENAI_API_BASE_URL;
			OpenAIKey = res.OPENAI_API_KEY;
		}
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
89
90
91
	onMount(async () => {
		let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');

Timothy J. Baek's avatar
Timothy J. Baek committed
92
		conversationMode = settings.conversationMode ?? false;
Timothy J. Baek's avatar
Timothy J. Baek committed
93
94
95
		speechAutoSend = settings.speechAutoSend ?? false;
		responseAutoPlayback = settings.responseAutoPlayback ?? false;

Timothy J. Baek's avatar
Timothy J. Baek committed
96
97
98
		STTEngine = settings?.audio?.STTEngine ?? '';
		TTSEngine = settings?.audio?.TTSEngine ?? '';
		speaker = settings?.audio?.speaker ?? '';
Timothy J. Baek's avatar
Timothy J. Baek committed
99

Timothy J. Baek's avatar
Timothy J. Baek committed
100
		if (TTSEngine === 'openai') {
Timothy J. Baek's avatar
Timothy J. Baek committed
101
102
103
104
			getOpenAIVoices();
		} else {
			getWebAPIVoices();
		}
105

Timothy J. Baek's avatar
Timothy J. Baek committed
106
107
		if ($user.role === 'admin') {
			const res = await getAudioConfig(localStorage.token);
108

Timothy J. Baek's avatar
Timothy J. Baek committed
109
110
111
112
			if (res) {
				OpenAIUrl = res.OPENAI_API_BASE_URL;
				OpenAIKey = res.OPENAI_API_KEY;
			}
113
		}
Timothy J. Baek's avatar
Timothy J. Baek committed
114
115
116
117
118
	});
</script>

<form
	class="flex flex-col h-full justify-between space-y-3 text-sm"
119
	on:submit|preventDefault={async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
120
121
122
		if ($user.role === 'admin') {
			await updateConfigHandler();
		}
Timothy J. Baek's avatar
Timothy J. Baek committed
123
		saveSettings({
Timothy J. Baek's avatar
Timothy J. Baek committed
124
			audio: {
Timothy J. Baek's avatar
Timothy J. Baek committed
125
126
				STTEngine: STTEngine !== '' ? STTEngine : undefined,
				TTSEngine: TTSEngine !== '' ? TTSEngine : undefined,
Timothy J. Baek's avatar
Timothy J. Baek committed
127
128
				speaker: speaker !== '' ? speaker : undefined
			}
Timothy J. Baek's avatar
Timothy J. Baek committed
129
130
131
132
		});
		dispatch('save');
	}}
>
133
	<div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[22rem]">
Timothy J. Baek's avatar
Timothy J. Baek committed
134
		<div>
135
			<div class=" mb-1 text-sm font-medium">{$i18n.t('STT Settings')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
136
137

			<div class=" py-0.5 flex w-full justify-between">
138
				<div class=" self-center text-xs font-medium">{$i18n.t('Speech-to-Text Engine')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
139
140
				<div class="flex items-center relative">
					<select
Jannik Streidl's avatar
Jannik Streidl committed
141
						class="dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
Timothy J. Baek's avatar
Timothy J. Baek committed
142
						bind:value={STTEngine}
Timothy J. Baek's avatar
Timothy J. Baek committed
143
144
						placeholder="Select a mode"
						on:change={(e) => {
Timothy J. Baek's avatar
Timothy J. Baek committed
145
146
							if (e.target.value !== '') {
								navigator.mediaDevices.getUserMedia({ audio: true }).catch(function (err) {
Ased Mammad's avatar
Ased Mammad committed
147
148
149
150
151
									toast.error(
										$i18n.t(`Permission denied when accessing microphone: {{error}}`, {
											error: err
										})
									);
Timothy J. Baek's avatar
Timothy J. Baek committed
152
153
									STTEngine = '';
								});
Timothy J. Baek's avatar
Timothy J. Baek committed
154
155
156
							}
						}}
					>
157
158
						<option value="">{$i18n.t('Default (Web API)')}</option>
						<option value="whisper-local">{$i18n.t('Whisper (Local)')}</option>
Timothy J. Baek's avatar
Timothy J. Baek committed
159
160
161
162
163
					</select>
				</div>
			</div>

			<div class=" py-0.5 flex w-full justify-between">
164
				<div class=" self-center text-xs font-medium">{$i18n.t('Conversation Mode')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
165
166
167
168
169
170
171
172
173

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					on:click={() => {
						toggleConversationMode();
					}}
					type="button"
				>
					{#if conversationMode === true}
174
						<span class="ml-2 self-center">{$i18n.t('On')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
175
					{:else}
176
						<span class="ml-2 self-center">{$i18n.t('Off')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
177
178
179
180
181
					{/if}
				</button>
			</div>

			<div class=" py-0.5 flex w-full justify-between">
182
183
184
				<div class=" self-center text-xs font-medium">
					{$i18n.t('Auto-send input after 3 sec.')}
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
185
186
187
188
189
190
191
192
193

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					on:click={() => {
						toggleSpeechAutoSend();
					}}
					type="button"
				>
					{#if speechAutoSend === true}
194
						<span class="ml-2 self-center">{$i18n.t('On')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
195
					{:else}
196
						<span class="ml-2 self-center">{$i18n.t('Off')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
197
198
199
					{/if}
				</button>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
200
201
202
		</div>

		<div>
203
			<div class=" mb-1 text-sm font-medium">{$i18n.t('TTS Settings')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
204
205

			<div class=" py-0.5 flex w-full justify-between">
206
				<div class=" self-center text-xs font-medium">{$i18n.t('Text-to-Speech Engine')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
207
208
				<div class="flex items-center relative">
					<select
Jannik Streidl's avatar
Jannik Streidl committed
209
						class=" dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
Timothy J. Baek's avatar
Timothy J. Baek committed
210
211
212
213
214
215
216
217
218
219
220
221
						bind:value={TTSEngine}
						placeholder="Select a mode"
						on:change={(e) => {
							if (e.target.value === 'openai') {
								getOpenAIVoices();
								speaker = 'alloy';
							} else {
								getWebAPIVoices();
								speaker = '';
							}
						}}
					>
222
223
						<option value="">{$i18n.t('Default (Web API)')}</option>
						<option value="openai">{$i18n.t('Open AI')}</option>
Timothy J. Baek's avatar
Timothy J. Baek committed
224
225
226
					</select>
				</div>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
227

Timothy J. Baek's avatar
Timothy J. Baek committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
			{#if $user.role === 'admin'}
				{#if TTSEngine === 'openai'}
					<div class="mt-1 flex gap-2 mb-1">
						<input
							class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
							placeholder={$i18n.t('API Base URL')}
							bind:value={OpenAIUrl}
							required
						/>

						<input
							class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
							placeholder={$i18n.t('API Key')}
							bind:value={OpenAIKey}
							required
						/>
					</div>
				{/if}
246
247
			{/if}

Timothy J. Baek's avatar
Timothy J. Baek committed
248
			<div class=" py-0.5 flex w-full justify-between">
249
				<div class=" self-center text-xs font-medium">{$i18n.t('Auto-playback response')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
250
251
252
253
254

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					on:click={() => {
						toggleResponseAutoPlayback();
Timothy J. Baek's avatar
Timothy J. Baek committed
255
					}}
Timothy J. Baek's avatar
Timothy J. Baek committed
256
					type="button"
Timothy J. Baek's avatar
Timothy J. Baek committed
257
				>
Timothy J. Baek's avatar
Timothy J. Baek committed
258
					{#if responseAutoPlayback === true}
259
						<span class="ml-2 self-center">{$i18n.t('On')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
260
					{:else}
261
						<span class="ml-2 self-center">{$i18n.t('Off')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
262
263
					{/if}
				</button>
Timothy J. Baek's avatar
Timothy J. Baek committed
264
265
266
267
268
			</div>
		</div>

		<hr class=" dark:border-gray-700" />

Timothy J. Baek's avatar
Timothy J. Baek committed
269
		{#if TTSEngine === ''}
Timothy J. Baek's avatar
Timothy J. Baek committed
270
			<div>
271
				<div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Voice')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
272
273
274
				<div class="flex w-full">
					<div class="flex-1">
						<select
Timothy J. Baek's avatar
Timothy J. Baek committed
275
							class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
Timothy J. Baek's avatar
Timothy J. Baek committed
276
							bind:value={speaker}
Timothy J. Baek's avatar
Timothy J. Baek committed
277
278
							placeholder="Select a voice"
						>
Ased Mammad's avatar
Ased Mammad committed
279
							<option value="" selected>{$i18n.t('Default')}</option>
Timothy J. Baek's avatar
Timothy J. Baek committed
280
							{#each voices.filter((v) => v.localService === true) as voice}
Timothy J. Baek's avatar
Timothy J. Baek committed
281
282
283
284
285
286
287
								<option value={voice.name} class="bg-gray-100 dark:bg-gray-700">{voice.name}</option
								>
							{/each}
						</select>
					</div>
				</div>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
288
		{:else if TTSEngine === 'openai'}
Timothy J. Baek's avatar
Timothy J. Baek committed
289
			<div>
290
				<div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Voice')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
291
292
				<div class="flex w-full">
					<div class="flex-1">
293
294
295
						<input
							list="voice-list"
							class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
Timothy J. Baek's avatar
Timothy J. Baek committed
296
297
							bind:value={speaker}
							placeholder="Select a voice"
298
299
300
						/>

						<datalist id="voice-list">
Timothy J. Baek's avatar
Timothy J. Baek committed
301
							{#each voices as voice}
302
								<option value={voice.name} />
Timothy J. Baek's avatar
Timothy J. Baek committed
303
							{/each}
304
						</datalist>
Timothy J. Baek's avatar
Timothy J. Baek committed
305
306
307
					</div>
				</div>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
308
		{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
309
310
311
312
	</div>

	<div class="flex justify-end pt-3 text-sm font-medium">
		<button
Timothy J. Baek's avatar
Timothy J. Baek committed
313
			class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
Timothy J. Baek's avatar
Timothy J. Baek committed
314
315
			type="submit"
		>
Jannik Streidl's avatar
Jannik Streidl committed
316
			{$i18n.t('Save')}
Timothy J. Baek's avatar
Timothy J. Baek committed
317
318
319
		</button>
	</div>
</form>