Audio.svelte 5.77 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
64
65
		STTEngine = $settings?.audio?.stt?.engine ?? '';
		voice = $settings?.audio?.tts?.voice ?? $config.audio.tts.voice ?? '';
		nonLocalVoices = $settings.audio?.tts?.nonLocalVoices ?? false;
Timothy J. Baek's avatar
Timothy J. Baek committed
66

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
67
		await getVoices();
Timothy J. Baek's avatar
Timothy J. Baek committed
68
69
70
71
72
	});
</script>

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

Timothy J. Baek's avatar
Timothy J. Baek committed
92
93
94
95
96
97
98
99
100
101
102
103
104
			{#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
105
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
106
			{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
107

Timothy J. Baek's avatar
Timothy J. Baek committed
108
			<div class=" py-0.5 flex w-full justify-between">
109
				<div class=" self-center text-xs font-medium">
110
					{$i18n.t('Instant Auto-Send After Voice Transcription')}
111
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
112
113
114
115
116
117
118
119
120

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					on:click={() => {
						toggleSpeechAutoSend();
					}}
					type="button"
				>
					{#if speechAutoSend === true}
121
						<span class="ml-2 self-center">{$i18n.t('On')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
122
					{:else}
123
						<span class="ml-2 self-center">{$i18n.t('Off')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
124
125
126
					{/if}
				</button>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
127
128
129
		</div>

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

Timothy J. Baek's avatar
Timothy J. Baek committed
132
			<div class=" py-0.5 flex w-full justify-between">
133
				<div class=" self-center text-xs font-medium">{$i18n.t('Auto-playback response')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
134
135
136
137
138

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

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

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

						<datalist id="voice-list">
Timothy J. Baek's avatar
Timothy J. Baek committed
196
							{#each voices as voice}
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
197
								<option value={voice.id}>{voice.name}</option>
Timothy J. Baek's avatar
Timothy J. Baek committed
198
							{/each}
199
						</datalist>
Timothy J. Baek's avatar
Timothy J. Baek committed
200
201
202
					</div>
				</div>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
203
		{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
204
205
	</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
206
	<div class="flex justify-end text-sm font-medium">
Timothy J. Baek's avatar
Timothy J. Baek committed
207
		<button
Timothy J. Baek's avatar
Timothy J. Baek committed
208
			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
209
210
			type="submit"
		>
Jannik Streidl's avatar
Jannik Streidl committed
211
			{$i18n.t('Save')}
Timothy J. Baek's avatar
Timothy J. Baek committed
212
213
214
		</button>
	</div>
</form>