Audio.svelte 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { createEventDispatcher, onMount, getContext } from 'svelte';
  4. import { user, settings, config } from '$lib/stores';
  5. import { getVoices as _getVoices } from '$lib/apis/audio';
  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 conversationMode = false;
  12. let speechAutoSend = false;
  13. let responseAutoPlayback = false;
  14. let nonLocalVoices = false;
  15. let STTEngine = '';
  16. let voices = [];
  17. let voice = '';
  18. // Audio speed control
  19. let playbackRate = 1;
  20. const speedOptions = [2, 1.75, 1.5, 1.25, 1, 0.75, 0.5];
  21. const getVoices = async () => {
  22. if ($config.audio.tts.engine === '') {
  23. const getVoicesLoop = setInterval(async () => {
  24. voices = await speechSynthesis.getVoices();
  25. // do your loop
  26. if (voices.length > 0) {
  27. clearInterval(getVoicesLoop);
  28. }
  29. }, 100);
  30. } else {
  31. const res = await _getVoices(localStorage.token).catch((e) => {
  32. toast.error(e);
  33. });
  34. if (res) {
  35. console.log(res);
  36. voices = res.voices;
  37. }
  38. }
  39. };
  40. const toggleResponseAutoPlayback = async () => {
  41. responseAutoPlayback = !responseAutoPlayback;
  42. saveSettings({ responseAutoPlayback: responseAutoPlayback });
  43. };
  44. const toggleSpeechAutoSend = async () => {
  45. speechAutoSend = !speechAutoSend;
  46. saveSettings({ speechAutoSend: speechAutoSend });
  47. };
  48. onMount(async () => {
  49. playbackRate = $settings.audio?.tts?.playbackRate ?? 1;
  50. conversationMode = $settings.conversationMode ?? false;
  51. speechAutoSend = $settings.speechAutoSend ?? false;
  52. responseAutoPlayback = $settings.responseAutoPlayback ?? false;
  53. STTEngine = $settings?.audio?.stt?.engine ?? '';
  54. if ($settings?.audio?.tts?.defaultVoice === $config.audio.tts.voice) {
  55. voice = $settings?.audio?.tts?.voice ?? $config.audio.tts.voice ?? '';
  56. } else {
  57. voice = $config.audio.tts.voice ?? '';
  58. }
  59. nonLocalVoices = $settings.audio?.tts?.nonLocalVoices ?? false;
  60. await getVoices();
  61. });
  62. </script>
  63. <form
  64. class="flex flex-col h-full justify-between space-y-3 text-sm"
  65. on:submit|preventDefault={async () => {
  66. saveSettings({
  67. audio: {
  68. stt: {
  69. engine: STTEngine !== '' ? STTEngine : undefined
  70. },
  71. tts: {
  72. playbackRate: playbackRate,
  73. voice: voice !== '' ? voice : undefined,
  74. defaultVoice: $config?.audio?.tts?.voice ?? '',
  75. nonLocalVoices: $config.audio.tts.engine === '' ? nonLocalVoices : undefined
  76. }
  77. }
  78. });
  79. dispatch('save');
  80. }}
  81. >
  82. <div class=" space-y-3 pr-1.5 overflow-y-scroll">
  83. <div>
  84. <div class=" mb-1 text-sm font-medium">{$i18n.t('STT Settings')}</div>
  85. {#if $config.audio.stt.engine !== 'web'}
  86. <div class=" py-0.5 flex w-full justify-between">
  87. <div class=" self-center text-xs font-medium">{$i18n.t('Speech-to-Text Engine')}</div>
  88. <div class="flex items-center relative">
  89. <select
  90. class="dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  91. bind:value={STTEngine}
  92. placeholder="Select an engine"
  93. >
  94. <option value="">{$i18n.t('Default')}</option>
  95. <option value="web">{$i18n.t('Web API')}</option>
  96. </select>
  97. </div>
  98. </div>
  99. {/if}
  100. <div class=" py-0.5 flex w-full justify-between">
  101. <div class=" self-center text-xs font-medium">
  102. {$i18n.t('Instant Auto-Send After Voice Transcription')}
  103. </div>
  104. <button
  105. class="p-1 px-3 text-xs flex rounded transition"
  106. on:click={() => {
  107. toggleSpeechAutoSend();
  108. }}
  109. type="button"
  110. >
  111. {#if speechAutoSend === true}
  112. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  113. {:else}
  114. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  115. {/if}
  116. </button>
  117. </div>
  118. </div>
  119. <div>
  120. <div class=" mb-1 text-sm font-medium">{$i18n.t('TTS Settings')}</div>
  121. <div class=" py-0.5 flex w-full justify-between">
  122. <div class=" self-center text-xs font-medium">{$i18n.t('Auto-playback response')}</div>
  123. <button
  124. class="p-1 px-3 text-xs flex rounded transition"
  125. on:click={() => {
  126. toggleResponseAutoPlayback();
  127. }}
  128. type="button"
  129. >
  130. {#if responseAutoPlayback === true}
  131. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  132. {:else}
  133. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  134. {/if}
  135. </button>
  136. </div>
  137. <div class=" py-0.5 flex w-full justify-between">
  138. <div class=" self-center text-xs font-medium">{$i18n.t('Speech Playback Speed')}</div>
  139. <div class="flex items-center relative">
  140. <select
  141. class="dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  142. bind:value={playbackRate}
  143. >
  144. {#each speedOptions as option}
  145. <option value={option} selected={playbackRate === option}>{option}x</option>
  146. {/each}
  147. </select>
  148. </div>
  149. </div>
  150. </div>
  151. <hr class=" dark:border-gray-850" />
  152. {#if $config.audio.tts.engine === ''}
  153. <div>
  154. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Voice')}</div>
  155. <div class="flex w-full">
  156. <div class="flex-1">
  157. <select
  158. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  159. bind:value={voice}
  160. >
  161. <option value="" selected={voice !== ''}>{$i18n.t('Default')}</option>
  162. {#each voices.filter((v) => nonLocalVoices || v.localService === true) as _voice}
  163. <option
  164. value={_voice.name}
  165. class="bg-gray-100 dark:bg-gray-700"
  166. selected={voice === _voice.name}>{_voice.name}</option
  167. >
  168. {/each}
  169. </select>
  170. </div>
  171. </div>
  172. <div class="flex items-center justify-between my-1.5">
  173. <div class="text-xs">
  174. {$i18n.t('Allow non-local voices')}
  175. </div>
  176. <div class="mt-1">
  177. <Switch bind:state={nonLocalVoices} />
  178. </div>
  179. </div>
  180. </div>
  181. {:else if $config.audio.tts.engine !== ''}
  182. <div>
  183. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Voice')}</div>
  184. <div class="flex w-full">
  185. <div class="flex-1">
  186. <input
  187. list="voice-list"
  188. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  189. bind:value={voice}
  190. placeholder="Select a voice"
  191. />
  192. <datalist id="voice-list">
  193. {#each voices as voice}
  194. <option value={voice.id}>{voice.name}</option>
  195. {/each}
  196. </datalist>
  197. </div>
  198. </div>
  199. </div>
  200. {/if}
  201. </div>
  202. <div class="flex justify-end text-sm font-medium">
  203. <button
  204. class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full"
  205. type="submit"
  206. >
  207. {$i18n.t('Save')}
  208. </button>
  209. </div>
  210. </form>