"docs/en_US/EvolutionTuner.md" did not exist on "e1fe5f5775711f748cd8fb6f220e78c68b911b15"
Voice.svelte 4.76 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

	let speechAutoSend = false;
	let responseAutoPlayback = false;

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

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

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

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

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

Timothy J. Baek's avatar
Timothy J. Baek committed
40
41
42
43
44
45
46
47
48
49
	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
50
51
52
	onMount(async () => {
		let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');

Timothy J. Baek's avatar
Timothy J. Baek committed
53
54
55
		speechAutoSend = settings.speechAutoSend ?? false;
		responseAutoPlayback = settings.responseAutoPlayback ?? false;

Timothy J. Baek's avatar
Timothy J. Baek committed
56
57
58
59
60
61
62
63
		engine = settings?.speech?.engine ?? '';
		speaker = settings?.speech?.speaker ?? '';

		if (engine === 'openai') {
			getOpenAIVoices();
		} else {
			getWebAPIVoices();
		}
Timothy J. Baek's avatar
Timothy J. Baek committed
64
65
66
67
68
69
70
	});
</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
71
72
73
74
			speech: {
				engine: engine !== '' ? engine : undefined,
				speaker: speaker !== '' ? speaker : undefined
			}
Timothy J. Baek's avatar
Timothy J. Baek committed
75
76
77
78
79
		});
		dispatch('save');
	}}
>
	<div class=" space-y-3">
Timothy J. Baek's avatar
Timothy J. Baek committed
80
81
82
83
84
85
86
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
		<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">
				<div class=" self-center text-xs font-medium">Voice Input Auto-Send</div>

				<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">
				<div class=" self-center text-xs font-medium">TTS Automatic Playback</div>

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					on:click={() => {
						toggleResponseAutoPlayback();
Timothy J. Baek's avatar
Timothy J. Baek committed
131
					}}
Timothy J. Baek's avatar
Timothy J. Baek committed
132
					type="button"
Timothy J. Baek's avatar
Timothy J. Baek committed
133
				>
Timothy J. Baek's avatar
Timothy J. Baek committed
134
135
136
137
138
139
					{#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
140
141
142
143
144
			</div>
		</div>

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

Timothy J. Baek's avatar
Timothy J. Baek committed
145
		{#if engine === ''}
Timothy J. Baek's avatar
Timothy J. Baek committed
146
			<div>
Timothy J. Baek's avatar
Timothy J. Baek committed
147
				<div class=" mb-2.5 text-sm font-medium">Set Voice</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
148
149
150
151
				<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
152
							bind:value={speaker}
Timothy J. Baek's avatar
Timothy J. Baek committed
153
154
155
							placeholder="Select a voice"
						>
							<option value="" selected>Default</option>
Timothy J. Baek's avatar
Timothy J. Baek committed
156
							{#each voices.filter((v) => v.localService === true) as voice}
Timothy J. Baek's avatar
Timothy J. Baek committed
157
158
159
160
161
162
163
								<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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
		{: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
182
		{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
183
184
185
186
187
188
189
190
191
192
193
	</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>