Skip to content

Commit

Permalink
🐛 fix: Fix options type export
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Nov 16, 2023
1 parent 76269d9 commit 4402715
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
run: bun run release
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
15 changes: 13 additions & 2 deletions src/core/const/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ const getSpeechSynthesis = () => {
return (
(globalThis as any)?.speechSynthesis ||
(window as any)?.speechSynthesis ||
(window as any)?.speechSynthesis
(window as any)?.webkitSpeechSynthesis
);
} catch {}
};

const getSpeechSynthesisUtterance = () => {
try {
return (
(globalThis as any)?.SpeechSynthesisUtterance ||
(window as any)?.SpeechSynthesisUtterance ||
(window as any)?.webkitSpeechSynthesisUtterance
);
} catch {}
};

export const SpeechRecognition = getSpeechRecognition();
export const speechSynthesis = getSpeechSynthesis();
export const SpeechSynthesis = getSpeechSynthesis();
export const SpeechSynthesisUtterance = getSpeechSynthesisUtterance();
2 changes: 1 addition & 1 deletion src/react/hooks/useAudioPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { RefObject, useCallback, useEffect, useRef, useState } from 'react';
import useSWR from 'swr';

import { AudioProps } from '@/react/AudioPlayer';
import { arrayBufferConvert } from '@/core/utils/arrayBufferConvert';
import { audioBufferToBlob } from '@/core/utils/audioBufferToBlob';
import { AudioProps } from '@/react/AudioPlayer';

export interface AudioPlayerHook extends AudioProps {
isLoading?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/react/hooks/useStreamAudioPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RefObject, useCallback, useEffect, useRef, useState } from 'react';

import { AudioProps } from '@/react/AudioPlayer';
import { audioBufferToBlob, audioBuffersToBlob } from '@/core/utils/audioBufferToBlob';
import { AudioProps } from '@/react/AudioPlayer';

export interface StreamAudioPlayerHook extends AudioProps {
download: () => void;
Expand Down
6 changes: 3 additions & 3 deletions src/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export { useAudioVisualizer } from './hooks/useAudioVisualizer';
export { useBlobUrl } from './hooks/useBlobUrl';
export { useStreamAudioPlayer } from './hooks/useStreamAudioPlayer';
export { useAudioRecorder } from './useAudioRecorder';
export { useEdgeSpeech } from './useEdgeSpeech';
export { useMicrosoftSpeech } from './useMicrosoftSpeech';
export { type EdgeSpeechOptions,useEdgeSpeech } from './useEdgeSpeech';
export { type MicrosoftSpeechOptions,useMicrosoftSpeech } from './useMicrosoftSpeech';
export {
type OpenaiSpeechRecognitionOptions,
type OpenAISTTConfig,
Expand All @@ -21,4 +21,4 @@ export {
type SpeechRecognitionOptions,
useSpeechRecognition,
} from './useSpeechRecognition/useSpeechRecognition';
export { useSpeechSynthes } from './useSpeechSynthes';
export { type SpeechSynthesOptions,useSpeechSynthes } from './useSpeechSynthes';
2 changes: 1 addition & 1 deletion src/react/useMicrosoftSpeech/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState } from 'react';
import { type MicrosoftSpeechPayload, MicrosoftSpeechTTS } from '@/core/MicrosoftSpeechTTS';
import { TTSConfig, useTTS } from '@/react/useTTS';

interface MicrosoftSpeechOptions extends Pick<MicrosoftSpeechPayload, 'options'>, TTSConfig {
export interface MicrosoftSpeechOptions extends Pick<MicrosoftSpeechPayload, 'options'>, TTSConfig {
api?: {
url?: string;
};
Expand Down
40 changes: 27 additions & 13 deletions src/react/useSpeechSynthes/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
import { useCallback, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';

import { speechSynthesis } from '@/core/const/polyfill';
import { SpeechSynthesis, SpeechSynthesisUtterance } from '@/core/const/polyfill';
import { SsmlOptions } from '@/core/utils/genSSML';

export const useSpeechSynthes = (defaultText: string, { voice, rate, pitch }: SsmlOptions) => {
const [voiceList, setVoiceList] = useState(speechSynthesis.getVoices());
export interface SpeechSynthesOptions extends Pick<SsmlOptions, 'voice' | 'rate' | 'pitch'> {
onStart?: () => void;
onStop?: () => void;
}
export const useSpeechSynthes = (
defaultText: string,
{ voice, rate, pitch, ...options }: SpeechSynthesOptions,
) => {
const [voiceList, setVoiceList] = useState(SpeechSynthesis?.getVoices());
const [text, setText] = useState<string>(defaultText);
const [isLoading, setIsLoading] = useState<boolean>(false);

const speechSynthesisUtterance = useMemo(() => {
if (!SpeechSynthesisUtterance) return;
const utterance = new SpeechSynthesisUtterance(text);
utterance.voice = voiceList.find((item: any) => item.name === voice) as any;
if (pitch) utterance.pitch = pitch * 10;
if (rate) utterance.rate = rate * 10;
return utterance;
}, [text, voiceList, rate, pitch, voice]);

speechSynthesis.onvoiceschanged = () => {
setVoiceList(speechSynthesis.getVoices());
};
speechSynthesisUtterance.onstart = () => setIsLoading(true);
speechSynthesisUtterance.onend = () => setIsLoading(false);
useEffect(() => {
if (!SpeechSynthesis) return;

SpeechSynthesis.onvoiceschanged = () => {
setVoiceList(SpeechSynthesis?.getVoices());
};
SpeechSynthesis.onstart = () => setIsLoading(true);
SpeechSynthesis.onend = () => setIsLoading(false);
}, []);

const handleStart = useCallback(() => {
speechSynthesis.speak(speechSynthesisUtterance);
}, [speechSynthesis, speechSynthesisUtterance]);
options?.onStart?.();
SpeechSynthesis?.speak(speechSynthesisUtterance);
}, [speechSynthesisUtterance]);

const handleStop = useCallback(() => {
speechSynthesis.cancel();
options?.onStop?.();
speechSynthesis?.cancel();
setIsLoading(false);
}, [speechSynthesis]);
}, []);

return {
isLoading,
Expand Down
2 changes: 1 addition & 1 deletion src/react/useTTS/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useCallback, useEffect, useState } from 'react';
import useSWR, { type SWRConfiguration } from 'swr';

import { splitTextIntoSegments } from '@/core/utils/splitTextIntoSegments';
import { AudioProps } from '@/react/AudioPlayer';
import { useStreamAudioPlayer } from '@/react/hooks/useStreamAudioPlayer';
import { splitTextIntoSegments } from '@/core/utils/splitTextIntoSegments';

export interface TTSHook extends SWRConfiguration {
audio: AudioProps;
Expand Down

0 comments on commit 4402715

Please sign in to comment.