Audio.svelte 6.0 KB

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