Audio.svelte 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <script lang="ts">
  2. import { getAudioConfig, updateAudioConfig } from '$lib/apis/audio';
  3. import { user, settings } 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. conversationMode = $settings.conversationMode ?? false;
  83. speechAutoSend = $settings.speechAutoSend ?? false;
  84. responseAutoPlayback = $settings.responseAutoPlayback ?? false;
  85. STTEngine = $settings?.audio?.STTEngine ?? '';
  86. TTSEngine = $settings?.audio?.TTSEngine ?? '';
  87. speaker = $settings?.audio?.speaker ?? '';
  88. model = $settings?.audio?.model ?? '';
  89. if (TTSEngine === 'openai') {
  90. getOpenAIVoices();
  91. getOpenAIVoicesModel();
  92. } else {
  93. getWebAPIVoices();
  94. }
  95. if ($user.role === 'admin') {
  96. const res = await getAudioConfig(localStorage.token);
  97. if (res) {
  98. OpenAIUrl = res.OPENAI_API_BASE_URL;
  99. OpenAIKey = res.OPENAI_API_KEY;
  100. model = res.OPENAI_API_MODEL;
  101. speaker = res.OPENAI_API_VOICE;
  102. }
  103. }
  104. });
  105. </script>
  106. <form
  107. class="flex flex-col h-full justify-between space-y-3 text-sm"
  108. on:submit|preventDefault={async () => {
  109. if ($user.role === 'admin') {
  110. await updateConfigHandler();
  111. }
  112. saveSettings({
  113. audio: {
  114. STTEngine: STTEngine !== '' ? STTEngine : undefined,
  115. TTSEngine: TTSEngine !== '' ? TTSEngine : undefined,
  116. speaker: speaker !== '' ? speaker : undefined,
  117. model: model !== '' ? model : undefined
  118. }
  119. });
  120. dispatch('save');
  121. }}
  122. >
  123. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[25rem]">
  124. <div>
  125. <div class=" mb-1 text-sm font-medium">{$i18n.t('STT Settings')}</div>
  126. <div class=" py-0.5 flex w-full justify-between">
  127. <div class=" self-center text-xs font-medium">{$i18n.t('Speech-to-Text Engine')}</div>
  128. <div class="flex items-center relative">
  129. <select
  130. class="dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  131. bind:value={STTEngine}
  132. placeholder="Select a mode"
  133. on:change={(e) => {
  134. if (e.target.value !== '') {
  135. navigator.mediaDevices.getUserMedia({ audio: true }).catch(function (err) {
  136. toast.error(
  137. $i18n.t(`Permission denied when accessing microphone: {{error}}`, {
  138. error: err
  139. })
  140. );
  141. STTEngine = '';
  142. });
  143. }
  144. }}
  145. >
  146. <option value="">{$i18n.t('Default (Web API)')}</option>
  147. <option value="whisper-local">{$i18n.t('Whisper (Local)')}</option>
  148. </select>
  149. </div>
  150. </div>
  151. <div class=" py-0.5 flex w-full justify-between">
  152. <div class=" self-center text-xs font-medium">{$i18n.t('Conversation Mode')}</div>
  153. <button
  154. class="p-1 px-3 text-xs flex rounded transition"
  155. on:click={() => {
  156. toggleConversationMode();
  157. }}
  158. type="button"
  159. >
  160. {#if conversationMode === true}
  161. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  162. {:else}
  163. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  164. {/if}
  165. </button>
  166. </div>
  167. <div class=" py-0.5 flex w-full justify-between">
  168. <div class=" self-center text-xs font-medium">
  169. {$i18n.t('Auto-send input after 3 sec.')}
  170. </div>
  171. <button
  172. class="p-1 px-3 text-xs flex rounded transition"
  173. on:click={() => {
  174. toggleSpeechAutoSend();
  175. }}
  176. type="button"
  177. >
  178. {#if speechAutoSend === true}
  179. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  180. {:else}
  181. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  182. {/if}
  183. </button>
  184. </div>
  185. </div>
  186. <div>
  187. <div class=" mb-1 text-sm font-medium">{$i18n.t('TTS Settings')}</div>
  188. <div class=" py-0.5 flex w-full justify-between">
  189. <div class=" self-center text-xs font-medium">{$i18n.t('Text-to-Speech Engine')}</div>
  190. <div class="flex items-center relative">
  191. <select
  192. class=" dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  193. bind:value={TTSEngine}
  194. placeholder="Select a mode"
  195. on:change={(e) => {
  196. if (e.target.value === 'openai') {
  197. getOpenAIVoices();
  198. speaker = 'alloy';
  199. model = 'tts-1';
  200. } else {
  201. getWebAPIVoices();
  202. speaker = '';
  203. }
  204. }}
  205. >
  206. <option value="">{$i18n.t('Default (Web API)')}</option>
  207. <option value="openai">{$i18n.t('Open AI')}</option>
  208. </select>
  209. </div>
  210. </div>
  211. {#if $user.role === 'admin'}
  212. {#if TTSEngine === 'openai'}
  213. <div class="mt-1 flex gap-2 mb-1">
  214. <input
  215. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  216. placeholder={$i18n.t('API Base URL')}
  217. bind:value={OpenAIUrl}
  218. required
  219. />
  220. <input
  221. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  222. placeholder={$i18n.t('API Key')}
  223. bind:value={OpenAIKey}
  224. required
  225. />
  226. </div>
  227. {/if}
  228. {/if}
  229. <div class=" py-0.5 flex w-full justify-between">
  230. <div class=" self-center text-xs font-medium">{$i18n.t('Auto-playback response')}</div>
  231. <button
  232. class="p-1 px-3 text-xs flex rounded transition"
  233. on:click={() => {
  234. toggleResponseAutoPlayback();
  235. }}
  236. type="button"
  237. >
  238. {#if responseAutoPlayback === true}
  239. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  240. {:else}
  241. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  242. {/if}
  243. </button>
  244. </div>
  245. </div>
  246. <hr class=" dark:border-gray-700" />
  247. {#if TTSEngine === ''}
  248. <div>
  249. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Voice')}</div>
  250. <div class="flex w-full">
  251. <div class="flex-1">
  252. <select
  253. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  254. bind:value={speaker}
  255. placeholder="Select a voice"
  256. >
  257. <option value="" selected>{$i18n.t('Default')}</option>
  258. {#each voices.filter((v) => v.localService === true) as voice}
  259. <option value={voice.name} class="bg-gray-100 dark:bg-gray-700">{voice.name}</option
  260. >
  261. {/each}
  262. </select>
  263. </div>
  264. </div>
  265. </div>
  266. {:else if TTSEngine === 'openai'}
  267. <div>
  268. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Voice')}</div>
  269. <div class="flex w-full">
  270. <div class="flex-1">
  271. <input
  272. list="voice-list"
  273. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  274. bind:value={speaker}
  275. placeholder="Select a voice"
  276. />
  277. <datalist id="voice-list">
  278. {#each voices as voice}
  279. <option value={voice.name} />
  280. {/each}
  281. </datalist>
  282. </div>
  283. </div>
  284. </div>
  285. <div>
  286. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Model')}</div>
  287. <div class="flex w-full">
  288. <div class="flex-1">
  289. <input
  290. list="model-list"
  291. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  292. bind:value={model}
  293. placeholder="Select a model"
  294. />
  295. <datalist id="model-list">
  296. {#each models as model}
  297. <option value={model.name} />
  298. {/each}
  299. </datalist>
  300. </div>
  301. </div>
  302. </div>
  303. {/if}
  304. </div>
  305. <div class="flex justify-end text-sm font-medium">
  306. <button
  307. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  308. type="submit"
  309. >
  310. {$i18n.t('Save')}
  311. </button>
  312. </div>
  313. </form>