Audio.svelte 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <script lang="ts">
  2. import { getAudioConfig, updateAudioConfig } from '$lib/apis/audio';
  3. import { user } from '$lib/stores';
  4. import { createEventDispatcher, onMount, getContext } from 'svelte';
  5. import { toast } from 'svelte-sonner';
  6. const dispatch = createEventDispatcher();
  7. const i18n = getContext('i18n');
  8. export let saveSettings: Function;
  9. // Audio
  10. let OpenAIUrl = '';
  11. let OpenAIKey = '';
  12. let STTEngines = ['', 'openai'];
  13. let STTEngine = '';
  14. let conversationMode = false;
  15. let speechAutoSend = false;
  16. let responseAutoPlayback = false;
  17. let TTSEngines = ['', 'openai'];
  18. let TTSEngine = '';
  19. let voices = [];
  20. let speaker = '';
  21. let models = [];
  22. let model = '';
  23. const getOpenAIVoices = () => {
  24. voices = [
  25. { name: 'alloy' },
  26. { name: 'echo' },
  27. { name: 'fable' },
  28. { name: 'onyx' },
  29. { name: 'nova' },
  30. { name: 'shimmer' }
  31. ];
  32. };
  33. const getOpenAIVoicesModel = () => {
  34. models = [{ name: 'tts-1' }, { name: 'tts-1-hd' }];
  35. };
  36. const getWebAPIVoices = () => {
  37. const getVoicesLoop = setInterval(async () => {
  38. voices = await speechSynthesis.getVoices();
  39. // do your loop
  40. if (voices.length > 0) {
  41. clearInterval(getVoicesLoop);
  42. }
  43. }, 100);
  44. };
  45. const toggleConversationMode = async () => {
  46. conversationMode = !conversationMode;
  47. if (conversationMode) {
  48. responseAutoPlayback = true;
  49. speechAutoSend = true;
  50. }
  51. saveSettings({
  52. conversationMode: conversationMode,
  53. responseAutoPlayback: responseAutoPlayback,
  54. speechAutoSend: speechAutoSend
  55. });
  56. };
  57. const toggleResponseAutoPlayback = async () => {
  58. responseAutoPlayback = !responseAutoPlayback;
  59. saveSettings({ responseAutoPlayback: responseAutoPlayback });
  60. };
  61. const toggleSpeechAutoSend = async () => {
  62. speechAutoSend = !speechAutoSend;
  63. saveSettings({ speechAutoSend: speechAutoSend });
  64. };
  65. const updateConfigHandler = async () => {
  66. if (TTSEngine === 'openai') {
  67. const res = await updateAudioConfig(localStorage.token, {
  68. url: OpenAIUrl,
  69. key: OpenAIKey,
  70. model: model,
  71. speaker: speaker
  72. });
  73. if (res) {
  74. OpenAIUrl = res.OPENAI_API_BASE_URL;
  75. OpenAIKey = res.OPENAI_API_KEY;
  76. model = res.OPENAI_API_MODEL;
  77. speaker = res.OPENAI_API_VOICE;
  78. }
  79. }
  80. };
  81. onMount(async () => {
  82. let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');
  83. conversationMode = settings.conversationMode ?? false;
  84. speechAutoSend = settings.speechAutoSend ?? false;
  85. responseAutoPlayback = settings.responseAutoPlayback ?? false;
  86. STTEngine = settings?.audio?.STTEngine ?? '';
  87. TTSEngine = settings?.audio?.TTSEngine ?? '';
  88. speaker = settings?.audio?.speaker ?? '';
  89. model = settings?.audio?.model ?? '';
  90. if (TTSEngine === 'openai') {
  91. getOpenAIVoices();
  92. getOpenAIVoicesModel();
  93. } else {
  94. getWebAPIVoices();
  95. }
  96. if ($user.role === 'admin') {
  97. const res = await getAudioConfig(localStorage.token);
  98. if (res) {
  99. OpenAIUrl = res.OPENAI_API_BASE_URL;
  100. OpenAIKey = res.OPENAI_API_KEY;
  101. model = res.OPENAI_API_MODEL;
  102. speaker = res.OPENAI_API_VOICE;
  103. }
  104. }
  105. });
  106. </script>
  107. <form
  108. class="flex flex-col h-full justify-between space-y-3 text-sm"
  109. on:submit|preventDefault={async () => {
  110. if ($user.role === 'admin') {
  111. await updateConfigHandler();
  112. }
  113. saveSettings({
  114. audio: {
  115. STTEngine: STTEngine !== '' ? STTEngine : undefined,
  116. TTSEngine: TTSEngine !== '' ? TTSEngine : undefined,
  117. speaker: speaker !== '' ? speaker : undefined,
  118. model: model !== '' ? model : undefined
  119. }
  120. });
  121. dispatch('save');
  122. }}
  123. >
  124. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[25rem]">
  125. <div>
  126. <div class=" mb-1 text-sm font-medium">{$i18n.t('STT Settings')}</div>
  127. <div class=" py-0.5 flex w-full justify-between">
  128. <div class=" self-center text-xs font-medium">{$i18n.t('Speech-to-Text Engine')}</div>
  129. <div class="flex items-center relative">
  130. <select
  131. class="dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  132. bind:value={STTEngine}
  133. placeholder="Select a mode"
  134. on:change={(e) => {
  135. if (e.target.value !== '') {
  136. navigator.mediaDevices.getUserMedia({ audio: true }).catch(function (err) {
  137. toast.error(
  138. $i18n.t(`Permission denied when accessing microphone: {{error}}`, {
  139. error: err
  140. })
  141. );
  142. STTEngine = '';
  143. });
  144. }
  145. }}
  146. >
  147. <option value="">{$i18n.t('Default (Web API)')}</option>
  148. <option value="whisper-local">{$i18n.t('Whisper (Local)')}</option>
  149. </select>
  150. </div>
  151. </div>
  152. <div class=" py-0.5 flex w-full justify-between">
  153. <div class=" self-center text-xs font-medium">{$i18n.t('Conversation Mode')}</div>
  154. <button
  155. class="p-1 px-3 text-xs flex rounded transition"
  156. on:click={() => {
  157. toggleConversationMode();
  158. }}
  159. type="button"
  160. >
  161. {#if conversationMode === true}
  162. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  163. {:else}
  164. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  165. {/if}
  166. </button>
  167. </div>
  168. <div class=" py-0.5 flex w-full justify-between">
  169. <div class=" self-center text-xs font-medium">
  170. {$i18n.t('Auto-send input after 3 sec.')}
  171. </div>
  172. <button
  173. class="p-1 px-3 text-xs flex rounded transition"
  174. on:click={() => {
  175. toggleSpeechAutoSend();
  176. }}
  177. type="button"
  178. >
  179. {#if speechAutoSend === true}
  180. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  181. {:else}
  182. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  183. {/if}
  184. </button>
  185. </div>
  186. </div>
  187. <div>
  188. <div class=" mb-1 text-sm font-medium">{$i18n.t('TTS Settings')}</div>
  189. <div class=" py-0.5 flex w-full justify-between">
  190. <div class=" self-center text-xs font-medium">{$i18n.t('Text-to-Speech Engine')}</div>
  191. <div class="flex items-center relative">
  192. <select
  193. class=" dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  194. bind:value={TTSEngine}
  195. placeholder="Select a mode"
  196. on:change={(e) => {
  197. if (e.target.value === 'openai') {
  198. getOpenAIVoices();
  199. speaker = 'alloy';
  200. model = 'tts-1';
  201. } else {
  202. getWebAPIVoices();
  203. speaker = '';
  204. }
  205. }}
  206. >
  207. <option value="">{$i18n.t('Default (Web API)')}</option>
  208. <option value="openai">{$i18n.t('Open AI')}</option>
  209. </select>
  210. </div>
  211. </div>
  212. {#if $user.role === 'admin'}
  213. {#if TTSEngine === 'openai'}
  214. <div class="mt-1 flex gap-2 mb-1">
  215. <input
  216. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  217. placeholder={$i18n.t('API Base URL')}
  218. bind:value={OpenAIUrl}
  219. required
  220. />
  221. <input
  222. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  223. placeholder={$i18n.t('API Key')}
  224. bind:value={OpenAIKey}
  225. required
  226. />
  227. </div>
  228. {/if}
  229. {/if}
  230. <div class=" py-0.5 flex w-full justify-between">
  231. <div class=" self-center text-xs font-medium">{$i18n.t('Auto-playback response')}</div>
  232. <button
  233. class="p-1 px-3 text-xs flex rounded transition"
  234. on:click={() => {
  235. toggleResponseAutoPlayback();
  236. }}
  237. type="button"
  238. >
  239. {#if responseAutoPlayback === true}
  240. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  241. {:else}
  242. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  243. {/if}
  244. </button>
  245. </div>
  246. </div>
  247. <hr class=" dark:border-gray-700" />
  248. {#if TTSEngine === ''}
  249. <div>
  250. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Voice')}</div>
  251. <div class="flex w-full">
  252. <div class="flex-1">
  253. <select
  254. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  255. bind:value={speaker}
  256. placeholder="Select a voice"
  257. >
  258. <option value="" selected>{$i18n.t('Default')}</option>
  259. {#each voices.filter((v) => v.localService === true) as voice}
  260. <option value={voice.name} class="bg-gray-100 dark:bg-gray-700">{voice.name}</option
  261. >
  262. {/each}
  263. </select>
  264. </div>
  265. </div>
  266. </div>
  267. {:else if TTSEngine === 'openai'}
  268. <div>
  269. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Voice')}</div>
  270. <div class="flex w-full">
  271. <div class="flex-1">
  272. <input
  273. list="voice-list"
  274. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  275. bind:value={speaker}
  276. placeholder="Select a voice"
  277. />
  278. <datalist id="voice-list">
  279. {#each voices as voice}
  280. <option value={voice.name} />
  281. {/each}
  282. </datalist>
  283. </div>
  284. </div>
  285. </div>
  286. <div>
  287. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Model')}</div>
  288. <div class="flex w-full">
  289. <div class="flex-1">
  290. <input
  291. list="model-list"
  292. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  293. bind:value={model}
  294. placeholder="Select a model"
  295. />
  296. <datalist id="model-list">
  297. {#each models as model}
  298. <option value={model.name} />
  299. {/each}
  300. </datalist>
  301. </div>
  302. </div>
  303. </div>
  304. {/if}
  305. </div>
  306. <div class="flex justify-end text-sm font-medium">
  307. <button
  308. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  309. type="submit"
  310. >
  311. {$i18n.t('Save')}
  312. </button>
  313. </div>
  314. </form>