"tools/vscode:/vscode.git/clone" did not exist on "22c0e300670672e4e0a8604bd6ab89bc28c970a6"
Voice.svelte 5.45 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

Timothy J. Baek's avatar
Timothy J. Baek committed
9
	let conversationMode = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
10
11
12
	let speechAutoSend = false;
	let responseAutoPlayback = false;

Timothy J. Baek's avatar
Timothy J. Baek committed
13
	let engines = ['', 'openai'];
Timothy J. Baek's avatar
Timothy J. Baek committed
14
	let engine = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
15
16
17

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

Timothy J. Baek's avatar
Timothy J. Baek committed
19
20
21
22
23
24
25
26
27
28
	const getOpenAIVoices = () => {
		voices = [
			{ name: 'alloy' },
			{ name: 'echo' },
			{ name: 'fable' },
			{ name: 'onyx' },
			{ name: 'nova' },
			{ name: 'shimmer' }
		];
	};
Timothy J. Baek's avatar
Timothy J. Baek committed
29

Timothy J. Baek's avatar
Timothy J. Baek committed
30
	const getWebAPIVoices = () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
31
		const getVoicesLoop = setInterval(async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
32
			voices = await speechSynthesis.getVoices();
Timothy J. Baek's avatar
Timothy J. Baek committed
33
34

			// do your loop
Timothy J. Baek's avatar
Timothy J. Baek committed
35
			if (voices.length > 0) {
Timothy J. Baek's avatar
Timothy J. Baek committed
36
37
38
				clearInterval(getVoicesLoop);
			}
		}, 100);
Timothy J. Baek's avatar
Timothy J. Baek committed
39
40
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
41
42
43
44
45
	const toggleConversationMode = async () => {
		conversationMode = !conversationMode;
		saveSettings({ conversationMode: conversationMode });
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
46
47
48
49
50
51
52
53
54
55
	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
56
57
58
	onMount(async () => {
		let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');

Timothy J. Baek's avatar
Timothy J. Baek committed
59
		conversationMode = settings.conversationMode ?? false;
Timothy J. Baek's avatar
Timothy J. Baek committed
60
61
62
		speechAutoSend = settings.speechAutoSend ?? false;
		responseAutoPlayback = settings.responseAutoPlayback ?? false;

Timothy J. Baek's avatar
Timothy J. Baek committed
63
64
65
66
67
68
69
70
		engine = settings?.speech?.engine ?? '';
		speaker = settings?.speech?.speaker ?? '';

		if (engine === 'openai') {
			getOpenAIVoices();
		} else {
			getWebAPIVoices();
		}
Timothy J. Baek's avatar
Timothy J. Baek committed
71
72
73
74
75
76
77
	});
</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
78
79
80
81
			speech: {
				engine: engine !== '' ? engine : undefined,
				speaker: speaker !== '' ? speaker : undefined
			}
Timothy J. Baek's avatar
Timothy J. Baek committed
82
83
84
85
86
		});
		dispatch('save');
	}}
>
	<div class=" space-y-3">
Timothy J. Baek's avatar
Timothy J. Baek committed
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
		<div>
			<div class=" mb-1 text-sm font-medium">TTS Settings</div>

			<div class=" py-0.5 flex w-full justify-between">
				<div class=" self-center text-xs font-medium">Speech Engine</div>
				<div class="flex items-center relative">
					<select
						class="w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
						bind:value={engine}
						placeholder="Select a mode"
						on:change={(e) => {
							if (e.target.value === 'openai') {
								getOpenAIVoices();
								speaker = 'alloy';
							} else {
								getWebAPIVoices();
								speaker = '';
							}
						}}
					>
						<option value="">Default (Web API)</option>
						<option value="openai">Open AI</option>
					</select>
				</div>
			</div>

			<div class=" py-0.5 flex w-full justify-between">
Timothy J. Baek's avatar
Timothy J. Baek committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
				<div class=" self-center text-xs font-medium">Conversation Mode</div>

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					on:click={() => {
						toggleConversationMode();
					}}
					type="button"
				>
					{#if conversationMode === true}
						<span class="ml-2 self-center">On</span>
					{:else}
						<span class="ml-2 self-center">Off</span>
					{/if}
				</button>
			</div>

			<div class=" py-0.5 flex w-full justify-between">
				<div class=" self-center text-xs font-medium">Auto-send input after 3 sec.</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					on:click={() => {
						toggleSpeechAutoSend();
					}}
					type="button"
				>
					{#if speechAutoSend === true}
						<span class="ml-2 self-center">On</span>
					{:else}
						<span class="ml-2 self-center">Off</span>
					{/if}
				</button>
			</div>

			<div class=" py-0.5 flex w-full justify-between">
Timothy J. Baek's avatar
Timothy J. Baek committed
150
				<div class=" self-center text-xs font-medium">Auto-playback response</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
151
152
153
154
155

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					on:click={() => {
						toggleResponseAutoPlayback();
Timothy J. Baek's avatar
Timothy J. Baek committed
156
					}}
Timothy J. Baek's avatar
Timothy J. Baek committed
157
					type="button"
Timothy J. Baek's avatar
Timothy J. Baek committed
158
				>
Timothy J. Baek's avatar
Timothy J. Baek committed
159
160
161
162
163
164
					{#if responseAutoPlayback === true}
						<span class="ml-2 self-center">On</span>
					{:else}
						<span class="ml-2 self-center">Off</span>
					{/if}
				</button>
Timothy J. Baek's avatar
Timothy J. Baek committed
165
166
167
168
169
			</div>
		</div>

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

Timothy J. Baek's avatar
Timothy J. Baek committed
170
		{#if engine === ''}
Timothy J. Baek's avatar
Timothy J. Baek committed
171
			<div>
Timothy J. Baek's avatar
Timothy J. Baek committed
172
				<div class=" mb-2.5 text-sm font-medium">Set Voice</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
173
174
175
176
				<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
177
							bind:value={speaker}
Timothy J. Baek's avatar
Timothy J. Baek committed
178
179
180
							placeholder="Select a voice"
						>
							<option value="" selected>Default</option>
Timothy J. Baek's avatar
Timothy J. Baek committed
181
							{#each voices.filter((v) => v.localService === true) as voice}
Timothy J. Baek's avatar
Timothy J. Baek committed
182
183
184
185
186
187
188
								<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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
		{:else if engine === 'openai'}
			<div>
				<div class=" mb-2.5 text-sm font-medium">Set Voice</div>
				<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"
							bind:value={speaker}
							placeholder="Select a voice"
						>
							{#each voices as voice}
								<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
207
		{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
208
209
210
211
212
213
214
215
216
217
218
	</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>