Voice.svelte 2.2 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
<script lang="ts">
	import { createEventDispatcher, onMount } from 'svelte';
	const dispatch = createEventDispatcher();

	export let saveSettings: Function;

	// Voice
Timothy J. Baek's avatar
Timothy J. Baek committed
8
9
10
11
12
	let engines = ['', 'openai'];
	let selectedEngine = '';

	let voices = [];
	let speaker = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
13
14
15
16

	onMount(async () => {
		let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');

Timothy J. Baek's avatar
Timothy J. Baek committed
17
		speaker = settings.speaker ?? '';
Timothy J. Baek's avatar
Timothy J. Baek committed
18
19

		const getVoicesLoop = setInterval(async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
20
			voices = await speechSynthesis.getVoices();
Timothy J. Baek's avatar
Timothy J. Baek committed
21
22

			// do your loop
Timothy J. Baek's avatar
Timothy J. Baek committed
23
			if (voices.length > 0) {
Timothy J. Baek's avatar
Timothy J. Baek committed
24
25
26
27
28
29
30
31
32
33
				clearInterval(getVoicesLoop);
			}
		}, 100);
	});
</script>

<form
	class="flex flex-col h-full justify-between space-y-3 text-sm"
	on:submit|preventDefault={() => {
		saveSettings({
Timothy J. Baek's avatar
Timothy J. Baek committed
34
			speaker: speaker !== '' ? speaker : undefined
Timothy J. Baek's avatar
Timothy J. Baek committed
35
36
37
38
39
		});
		dispatch('save');
	}}
>
	<div class=" space-y-3">
Timothy J. Baek's avatar
Timothy J. Baek committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
		<div class=" py-0.5 flex w-full justify-between">
			<div class=" self-center text-sm font-medium">Speech Engine</div>
			<div class="flex items-center relative">
				<select
					class="w-fit pr-8 rounded py-2 px-2 text-xs bg-transparent outline-none text-right"
					bind:value={selectedEngine}
					placeholder="Select a mode"
					on:change={(e) => {
						console.log(e);
					}}
				>
					<option value="">Default (Web API)</option>
					<option value="openai">Open AI</option>
				</select>
			</div>
		</div>

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

		{#if selectedEngine === ''}
Timothy J. Baek's avatar
Timothy J. Baek committed
60
			<div>
Timothy J. Baek's avatar
Timothy J. Baek committed
61
				<div class=" mb-2.5 text-sm font-medium">Set Voice</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
62
63
64
65
				<div class="flex w-full">
					<div class="flex-1">
						<select
							class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
Timothy J. Baek's avatar
Timothy J. Baek committed
66
							bind:value={speaker}
Timothy J. Baek's avatar
Timothy J. Baek committed
67
68
69
							placeholder="Select a voice"
						>
							<option value="" selected>Default</option>
Timothy J. Baek's avatar
Timothy J. Baek committed
70
							{#each voices.filter((v) => v.localService === true) as voice}
Timothy J. Baek's avatar
Timothy J. Baek committed
71
72
73
74
75
76
77
								<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
78
		{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
79
80
81
82
83
84
85
86
87
88
89
	</div>

	<div class="flex justify-end pt-3 text-sm font-medium">
		<button
			class=" px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-gray-100 transition rounded"
			type="submit"
		>
			Save
		</button>
	</div>
</form>