Audio.svelte 9.5 KB

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