Audio.svelte 5.95 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script lang="ts">
Jannik Streidl's avatar
Jannik Streidl committed
2
	import { toast } from 'svelte-sonner';
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
3
4
5
6
7
	import { createEventDispatcher, onMount, getContext } from 'svelte';

	import { user, settings, config } from '$lib/stores';
	import { getVoices as _getVoices } from '$lib/apis/audio';

8
	import Switch from '$lib/components/common/Switch.svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
9
10
	const dispatch = createEventDispatcher();

11
12
	const i18n = getContext('i18n');

Timothy J. Baek's avatar
Timothy J. Baek committed
13
14
	export let saveSettings: Function;

Timothy J. Baek's avatar
Timothy J. Baek committed
15
	// Audio
Timothy J. Baek's avatar
Timothy J. Baek committed
16
	let conversationMode = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
17
18
	let speechAutoSend = false;
	let responseAutoPlayback = false;
19
	let nonLocalVoices = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
20

Timothy J. Baek's avatar
Timothy J. Baek committed
21
	let STTEngine = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
22
23

	let voices = [];
Timothy J. Baek's avatar
Timothy J. Baek committed
24
	let voice = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
25

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
26
27
28
29
	const getVoices = async () => {
		if ($config.audio.tts.engine === '') {
			const getVoicesLoop = setInterval(async () => {
				voices = await speechSynthesis.getVoices();
Timothy J. Baek's avatar
Timothy J. Baek committed
30

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
31
32
33
34
35
36
37
38
39
				// do your loop
				if (voices.length > 0) {
					clearInterval(getVoicesLoop);
				}
			}, 100);
		} else {
			const res = await _getVoices(localStorage.token).catch((e) => {
				toast.error(e);
			});
Timothy J. Baek's avatar
Timothy J. Baek committed
40

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
41
42
43
			if (res) {
				console.log(res);
				voices = res.voices;
Timothy J. Baek's avatar
Timothy J. Baek committed
44
			}
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
45
		}
Timothy J. Baek's avatar
Timothy J. Baek committed
46
47
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
48
49
50
51
52
53
54
55
56
57
	const toggleResponseAutoPlayback = async () => {
		responseAutoPlayback = !responseAutoPlayback;
		saveSettings({ responseAutoPlayback: responseAutoPlayback });
	};

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

Timothy J. Baek's avatar
Timothy J. Baek committed
58
	onMount(async () => {
59
60
61
62
		conversationMode = $settings.conversationMode ?? false;
		speechAutoSend = $settings.speechAutoSend ?? false;
		responseAutoPlayback = $settings.responseAutoPlayback ?? false;

Timothy J. Baek's avatar
Timothy J. Baek committed
63
		STTEngine = $settings?.audio?.stt?.engine ?? '';
64
65
66
67
68
69
70

		if ($settings?.audio?.tts?.defaultVoice === $config.audio.tts.voice) {
			voice = $settings?.audio?.tts?.voice ?? $config.audio.tts.voice ?? '';
		} else {
			voice = $config.audio.tts.voice ?? '';
		}

Timothy J. Baek's avatar
Timothy J. Baek committed
71
		nonLocalVoices = $settings.audio?.tts?.nonLocalVoices ?? false;
Timothy J. Baek's avatar
Timothy J. Baek committed
72

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
73
		await getVoices();
Timothy J. Baek's avatar
Timothy J. Baek committed
74
75
76
77
78
	});
</script>

<form
	class="flex flex-col h-full justify-between space-y-3 text-sm"
79
	on:submit|preventDefault={async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
80
		saveSettings({
Timothy J. Baek's avatar
Timothy J. Baek committed
81
			audio: {
Timothy J. Baek's avatar
Timothy J. Baek committed
82
83
84
85
				stt: {
					engine: STTEngine !== '' ? STTEngine : undefined
				},
				tts: {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
86
					voice: voice !== '' ? voice : undefined,
87
					defaultVoice: $config?.audio?.tts?.voice ?? '',
Timothy J. Baek's avatar
Timothy J. Baek committed
88
89
					nonLocalVoices: $config.audio.tts.engine === '' ? nonLocalVoices : undefined
				}
Timothy J. Baek's avatar
Timothy J. Baek committed
90
			}
Timothy J. Baek's avatar
Timothy J. Baek committed
91
92
93
94
		});
		dispatch('save');
	}}
>
Timothy J. Baek's avatar
Timothy J. Baek committed
95
	<div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[25rem]">
Timothy J. Baek's avatar
Timothy J. Baek committed
96
		<div>
97
			<div class=" mb-1 text-sm font-medium">{$i18n.t('STT Settings')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
98

Timothy J. Baek's avatar
Timothy J. Baek committed
99
100
101
102
103
104
105
106
107
108
109
110
111
			{#if $config.audio.stt.engine !== 'web'}
				<div class=" py-0.5 flex w-full justify-between">
					<div class=" self-center text-xs font-medium">{$i18n.t('Speech-to-Text Engine')}</div>
					<div class="flex items-center relative">
						<select
							class="dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
							bind:value={STTEngine}
							placeholder="Select an engine"
						>
							<option value="">{$i18n.t('Default')}</option>
							<option value="web">{$i18n.t('Web API')}</option>
						</select>
					</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
112
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
113
			{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
114

Timothy J. Baek's avatar
Timothy J. Baek committed
115
			<div class=" py-0.5 flex w-full justify-between">
116
				<div class=" self-center text-xs font-medium">
117
					{$i18n.t('Instant Auto-Send After Voice Transcription')}
118
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
119
120
121
122
123
124
125
126
127

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					on:click={() => {
						toggleSpeechAutoSend();
					}}
					type="button"
				>
					{#if speechAutoSend === true}
128
						<span class="ml-2 self-center">{$i18n.t('On')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
129
					{:else}
130
						<span class="ml-2 self-center">{$i18n.t('Off')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
131
132
133
					{/if}
				</button>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
134
135
136
		</div>

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

Timothy J. Baek's avatar
Timothy J. Baek committed
139
			<div class=" py-0.5 flex w-full justify-between">
140
				<div class=" self-center text-xs font-medium">{$i18n.t('Auto-playback response')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
141
142
143
144
145

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					on:click={() => {
						toggleResponseAutoPlayback();
Timothy J. Baek's avatar
Timothy J. Baek committed
146
					}}
Timothy J. Baek's avatar
Timothy J. Baek committed
147
					type="button"
Timothy J. Baek's avatar
Timothy J. Baek committed
148
				>
Timothy J. Baek's avatar
Timothy J. Baek committed
149
					{#if responseAutoPlayback === true}
150
						<span class="ml-2 self-center">{$i18n.t('On')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
151
					{:else}
152
						<span class="ml-2 self-center">{$i18n.t('Off')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
153
154
					{/if}
				</button>
Timothy J. Baek's avatar
Timothy J. Baek committed
155
156
157
			</div>
		</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
158
		<hr class=" dark:border-gray-850" />
Timothy J. Baek's avatar
Timothy J. Baek committed
159

Timothy J. Baek's avatar
Timothy J. Baek committed
160
		{#if $config.audio.tts.engine === ''}
Timothy J. Baek's avatar
Timothy J. Baek committed
161
			<div>
162
				<div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Voice')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
163
164
165
				<div class="flex w-full">
					<div class="flex-1">
						<select
Timothy J. Baek's avatar
Timothy J. Baek committed
166
							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
167
							bind:value={voice}
Timothy J. Baek's avatar
Timothy J. Baek committed
168
						>
Timothy J. Baek's avatar
Timothy J. Baek committed
169
170
							<option value="" selected={voice !== ''}>{$i18n.t('Default')}</option>
							{#each voices.filter((v) => nonLocalVoices || v.localService === true) as _voice}
171
								<option
Timothy J. Baek's avatar
Timothy J. Baek committed
172
									value={_voice.name}
173
									class="bg-gray-100 dark:bg-gray-700"
Timothy J. Baek's avatar
Timothy J. Baek committed
174
									selected={voice === _voice.name}>{_voice.name}</option
Timothy J. Baek's avatar
Timothy J. Baek committed
175
176
177
178
179
								>
							{/each}
						</select>
					</div>
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
180
181
				<div class="flex items-center justify-between my-1.5">
					<div class="text-xs">
182
183
184
185
186
187
188
						{$i18n.t('Allow non-local voices')}
					</div>

					<div class="mt-1">
						<Switch bind:state={nonLocalVoices} />
					</div>
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
189
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
190
		{:else if $config.audio.tts.engine !== ''}
Timothy J. Baek's avatar
Timothy J. Baek committed
191
			<div>
192
				<div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Voice')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
193
194
				<div class="flex w-full">
					<div class="flex-1">
195
196
197
						<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
198
							bind:value={voice}
Timothy J. Baek's avatar
Timothy J. Baek committed
199
							placeholder="Select a voice"
200
201
202
						/>

						<datalist id="voice-list">
Timothy J. Baek's avatar
Timothy J. Baek committed
203
							{#each voices as voice}
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
204
								<option value={voice.id}>{voice.name}</option>
Timothy J. Baek's avatar
Timothy J. Baek committed
205
							{/each}
206
						</datalist>
Timothy J. Baek's avatar
Timothy J. Baek committed
207
208
209
					</div>
				</div>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
210
		{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
211
212
	</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
213
	<div class="flex justify-end text-sm font-medium">
Timothy J. Baek's avatar
Timothy J. Baek committed
214
		<button
Timothy J. Baek's avatar
Timothy J. Baek committed
215
			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
216
217
			type="submit"
		>
Jannik Streidl's avatar
Jannik Streidl committed
218
			{$i18n.t('Save')}
Timothy J. Baek's avatar
Timothy J. Baek committed
219
220
221
		</button>
	</div>
</form>