Audio.svelte 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <script lang="ts">
  2. import { getAudioConfig, updateAudioConfig } from '$lib/apis/audio';
  3. import { user, settings, config } 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. import { getBackendConfig } from '$lib/apis';
  8. const dispatch = createEventDispatcher();
  9. const i18n = getContext('i18n');
  10. export let saveHandler: Function;
  11. // Audio
  12. let TTS_OPENAI_API_BASE_URL = '';
  13. let TTS_OPENAI_API_KEY = '';
  14. let TTS_ENGINE = '';
  15. let TTS_MODEL = '';
  16. let TTS_VOICE = '';
  17. let STT_OPENAI_API_BASE_URL = '';
  18. let STT_OPENAI_API_KEY = '';
  19. let STT_ENGINE = '';
  20. let STT_MODEL = '';
  21. let voices = [];
  22. let models = [];
  23. let nonLocalVoices = false;
  24. const getOpenAIVoices = () => {
  25. voices = [
  26. { name: 'alloy' },
  27. { name: 'echo' },
  28. { name: 'fable' },
  29. { name: 'onyx' },
  30. { name: 'nova' },
  31. { name: 'shimmer' }
  32. ];
  33. };
  34. const getOpenAIModels = () => {
  35. models = [{ name: 'tts-1' }, { name: 'tts-1-hd' }];
  36. };
  37. const getWebAPIVoices = () => {
  38. const getVoicesLoop = setInterval(async () => {
  39. voices = await speechSynthesis.getVoices();
  40. // do your loop
  41. if (voices.length > 0) {
  42. clearInterval(getVoicesLoop);
  43. }
  44. }, 100);
  45. };
  46. const updateConfigHandler = async () => {
  47. const res = await updateAudioConfig(localStorage.token, {
  48. tts: {
  49. OPENAI_API_BASE_URL: TTS_OPENAI_API_BASE_URL,
  50. OPENAI_API_KEY: TTS_OPENAI_API_KEY,
  51. ENGINE: TTS_ENGINE,
  52. MODEL: TTS_MODEL,
  53. VOICE: TTS_VOICE
  54. },
  55. stt: {
  56. OPENAI_API_BASE_URL: STT_OPENAI_API_BASE_URL,
  57. OPENAI_API_KEY: STT_OPENAI_API_KEY,
  58. ENGINE: STT_ENGINE,
  59. MODEL: STT_MODEL
  60. }
  61. });
  62. if (res) {
  63. toast.success($i18n.t('Audio settings updated successfully'));
  64. config.set(await getBackendConfig());
  65. }
  66. };
  67. onMount(async () => {
  68. const res = await getAudioConfig(localStorage.token);
  69. if (res) {
  70. console.log(res);
  71. TTS_OPENAI_API_BASE_URL = res.tts.OPENAI_API_BASE_URL;
  72. TTS_OPENAI_API_KEY = res.tts.OPENAI_API_KEY;
  73. TTS_ENGINE = res.tts.ENGINE;
  74. TTS_MODEL = res.tts.MODEL;
  75. TTS_VOICE = res.tts.VOICE;
  76. STT_OPENAI_API_BASE_URL = res.stt.OPENAI_API_BASE_URL;
  77. STT_OPENAI_API_KEY = res.stt.OPENAI_API_KEY;
  78. STT_ENGINE = res.stt.ENGINE;
  79. STT_MODEL = res.stt.MODEL;
  80. }
  81. if (TTS_ENGINE === 'openai') {
  82. getOpenAIVoices();
  83. getOpenAIModels();
  84. } else {
  85. getWebAPIVoices();
  86. }
  87. });
  88. </script>
  89. <form
  90. class="flex flex-col h-full justify-between space-y-3 text-sm"
  91. on:submit|preventDefault={async () => {
  92. await updateConfigHandler();
  93. dispatch('save');
  94. }}
  95. >
  96. <div class=" space-y-3 overflow-y-scroll scrollbar-hidden h-full">
  97. <div class="flex flex-col gap-3">
  98. <div>
  99. <div class=" mb-1 text-sm font-medium">{$i18n.t('STT Settings')}</div>
  100. <div class=" py-0.5 flex w-full justify-between">
  101. <div class=" self-center text-xs font-medium">{$i18n.t('Speech-to-Text Engine')}</div>
  102. <div class="flex items-center relative">
  103. <select
  104. class="dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  105. bind:value={STT_ENGINE}
  106. placeholder="Select an engine"
  107. >
  108. <option value="">{$i18n.t('Whisper (Local)')}</option>
  109. <option value="openai">OpenAI</option>
  110. <option value="web">{$i18n.t('Web API')}</option>
  111. </select>
  112. </div>
  113. </div>
  114. {#if STT_ENGINE === 'openai'}
  115. <div>
  116. <div class="mt-1 flex gap-2 mb-1">
  117. <input
  118. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  119. placeholder={$i18n.t('API Base URL')}
  120. bind:value={STT_OPENAI_API_BASE_URL}
  121. required
  122. />
  123. <input
  124. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  125. placeholder={$i18n.t('API Key')}
  126. bind:value={STT_OPENAI_API_KEY}
  127. required
  128. />
  129. </div>
  130. </div>
  131. <hr class=" dark:border-gray-850 my-2" />
  132. <div>
  133. <div class=" mb-1.5 text-sm font-medium">{$i18n.t('STT Model')}</div>
  134. <div class="flex w-full">
  135. <div class="flex-1">
  136. <input
  137. list="model-list"
  138. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  139. bind:value={STT_MODEL}
  140. placeholder="Select a model"
  141. />
  142. <datalist id="model-list">
  143. <option value="whisper-1" />
  144. </datalist>
  145. </div>
  146. </div>
  147. </div>
  148. {/if}
  149. </div>
  150. <hr class=" dark:border-gray-800" />
  151. <div>
  152. <div class=" mb-1 text-sm font-medium">{$i18n.t('TTS Settings')}</div>
  153. <div class=" py-0.5 flex w-full justify-between">
  154. <div class=" self-center text-xs font-medium">{$i18n.t('Text-to-Speech Engine')}</div>
  155. <div class="flex items-center relative">
  156. <select
  157. class=" dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  158. bind:value={TTS_ENGINE}
  159. placeholder="Select a mode"
  160. on:change={(e) => {
  161. if (e.target.value === 'openai') {
  162. getOpenAIVoices();
  163. TTS_VOICE = 'alloy';
  164. TTS_MODEL = 'tts-1';
  165. } else {
  166. getWebAPIVoices();
  167. TTS_VOICE = '';
  168. }
  169. }}
  170. >
  171. <option value="">{$i18n.t('Web API')}</option>
  172. <option value="openai">{$i18n.t('Open AI')}</option>
  173. </select>
  174. </div>
  175. </div>
  176. {#if TTS_ENGINE === 'openai'}
  177. <div>
  178. <div class="mt-1 flex gap-2 mb-1">
  179. <input
  180. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  181. placeholder={$i18n.t('API Base URL')}
  182. bind:value={TTS_OPENAI_API_BASE_URL}
  183. required
  184. />
  185. <input
  186. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  187. placeholder={$i18n.t('API Key')}
  188. bind:value={TTS_OPENAI_API_KEY}
  189. required
  190. />
  191. </div>
  192. </div>
  193. {/if}
  194. <hr class=" dark:border-gray-850 my-2" />
  195. {#if TTS_ENGINE === ''}
  196. <div>
  197. <div class=" mb-1.5 text-sm font-medium">{$i18n.t('TTS Voice')}</div>
  198. <div class="flex w-full">
  199. <div class="flex-1">
  200. <select
  201. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  202. bind:value={TTS_VOICE}
  203. >
  204. <option value="" selected={TTS_VOICE !== ''}>{$i18n.t('Default')}</option>
  205. {#each voices as voice}
  206. <option
  207. value={voice.voiceURI}
  208. class="bg-gray-100 dark:bg-gray-700"
  209. selected={TTS_VOICE === voice.voiceURI}>{voice.name}</option
  210. >
  211. {/each}
  212. </select>
  213. </div>
  214. </div>
  215. </div>
  216. {:else if TTS_ENGINE === 'openai'}
  217. <div class=" flex gap-2">
  218. <div class="w-full">
  219. <div class=" mb-1.5 text-sm font-medium">{$i18n.t('TTS Voice')}</div>
  220. <div class="flex w-full">
  221. <div class="flex-1">
  222. <input
  223. list="voice-list"
  224. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  225. bind:value={TTS_VOICE}
  226. placeholder="Select a voice"
  227. />
  228. <datalist id="voice-list">
  229. {#each voices as voice}
  230. <option value={voice.name} />
  231. {/each}
  232. </datalist>
  233. </div>
  234. </div>
  235. </div>
  236. <div class="w-full">
  237. <div class=" mb-1.5 text-sm font-medium">{$i18n.t('TTS Model')}</div>
  238. <div class="flex w-full">
  239. <div class="flex-1">
  240. <input
  241. list="model-list"
  242. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  243. bind:value={TTS_MODEL}
  244. placeholder="Select a model"
  245. />
  246. <datalist id="model-list">
  247. {#each models as model}
  248. <option value={model.name} />
  249. {/each}
  250. </datalist>
  251. </div>
  252. </div>
  253. </div>
  254. </div>
  255. {/if}
  256. </div>
  257. </div>
  258. </div>
  259. <div class="flex justify-end text-sm font-medium">
  260. <button
  261. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  262. type="submit"
  263. >
  264. {$i18n.t('Save')}
  265. </button>
  266. </div>
  267. </form>