Skip to content

Commit

Permalink
✨ feat: Export additional types and functions for speech recognition …
Browse files Browse the repository at this point in the history
…and synthesis

This commit introduces new features to the codebase. Exported types and
functions related to speech recognition and synthesis have been added. The
`useOpenaiSTT` function now accepts additional options and callbacks for
handling audio recording and text changes. Similar changes have been made to
the `useOpenaiSTTWithPSR` and `useOpenaiSTTWithSR` functions. Additionally, the
`usePersistedSpeechRecognition` and `useSpeechRecognition` functions have been
updated to accept the same options and callbacks.

Changes:
- Export additional types and functions for speech recognition and synthesis
- Update `useOpenaiSTT` function to accept additional options and callbacks
- Update `useOpenaiSTTWithPSR` and `useOpenaiSTTWithSR` functions with similar changes
- Update `usePersistedSpeechRecognition` and `useSpeechRecognition` functions to accept the same options and callbacks
  • Loading branch information
canisminor1990 committed Nov 12, 2023
1 parent 35e96bf commit 0015bac
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 28 deletions.
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ export { useAudioRecorder } from './useAudioRecorder';
export { useAzureSpeech } from './useAzureSpeech';
export { useEdgeSpeech } from './useEdgeSpeech';
export { useMicrosoftSpeech } from './useMicrosoftSpeech';
export { useOpenaiSTT, useOpenaiSTTWithPSR, useOpenaiSTTWithSR } from './useOpenaiSTT';
export {
type OpenaiSpeechRecognitionOptions,
useOpenaiSTT,
useOpenaiSTTWithPSR,
useOpenaiSTTWithSR,
} from './useOpenaiSTT';
export { useOpenaiTTS } from './useOpenaiTTS';
export { usePersistedSpeechRecognition } from './useSpeechRecognition/usePersistedSpeechRecognition';
export { useSpeechRecognition } from './useSpeechRecognition/useSpeechRecognition';
export {
type SpeechRecognitionOptions,
useSpeechRecognition,
} from './useSpeechRecognition/useSpeechRecognition';
export { useSpeechSynthes } from './useSpeechSynthes';
export {
genLevaOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/useOpenaiSTT/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { useOpenaiSTT } from './useOpenaiSTT';
export { type OpenaiSpeechRecognitionOptions, useOpenaiSTT } from './useOpenaiSTT';
export { useOpenaiSTTWithPSR } from './useOpenaiSTTWithPSR';
export { useOpenaiSTTWithSR } from './useOpenaiSTTWithSR';
42 changes: 33 additions & 9 deletions src/useOpenaiSTT/useOpenaiSTT.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
import { useState } from 'react';
import { useCallback, useState } from 'react';
import useSWR from 'swr';

import { OpenaiSttOptions, fetchOpenaiSTT } from '@/services/fetchOpenaiSTT';
import { useAudioRecorder } from '@/useAudioRecorder';
import { SpeechRecognitionOptions } from '@/useSpeechRecognition/useSpeechRecognition';

export const useOpenaiSTT = (options: OpenaiSttOptions) => {
export type OpenaiSpeechRecognitionOptions = SpeechRecognitionOptions & OpenaiSttOptions;

export const useOpenaiSTT = ({
onBolbAvailable,
onTextChange,
...options
}: OpenaiSpeechRecognitionOptions) => {
const [isGlobalLoading, setIsGlobalLoading] = useState<boolean>(false);
const [shouldFetch, setShouldFetch] = useState<boolean>(false);
const { start, stop, blob, url, isRecording, time, formattedTime } = useAudioRecorder(() => {
setShouldFetch(true);
});
const [text, setText] = useState<string>();
const { start, stop, blob, url, isRecording, time, formattedTime } = useAudioRecorder(
(blobData) => {
setShouldFetch(true);
onBolbAvailable?.(blobData);
},
);

const key = new Date().getDate().toString();

const { isLoading, data } = useSWR(
const { isLoading } = useSWR(
shouldFetch && blob ? key : null,
async () => await fetchOpenaiSTT(blob as any, options),
{
onSuccess: (value) => {
onTextChange?.(value);
setIsGlobalLoading(false);
},
},
);

const handleStart = useCallback(() => {
setIsGlobalLoading(true);
start();
setText('');
}, [start]);

return {
blob,
formattedTime,
isLoading: isLoading || isRecording,
isLoading: isGlobalLoading || isLoading || isRecording,
isRecording,
start,
start: handleStart,
stop,
text: data,
text,
time,
url,
};
Expand Down
25 changes: 20 additions & 5 deletions src/useOpenaiSTT/useOpenaiSTTWithPSR.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { useCallback, useState } from 'react';
import useSWR from 'swr';

import { OpenaiSttOptions, fetchOpenaiSTT } from '@/services/fetchOpenaiSTT';
import { fetchOpenaiSTT } from '@/services/fetchOpenaiSTT';
import { usePersistedSpeechRecognition } from '@/useSpeechRecognition';

export const useOpenaiSTTWithPSR = (locale: string, options: OpenaiSttOptions) => {
import { OpenaiSpeechRecognitionOptions } from './useOpenaiSTT';

export const useOpenaiSTTWithPSR = (
locale: string,
{ onBolbAvailable, onTextChange, ...options }: OpenaiSpeechRecognitionOptions,
) => {
const [isGlobalLoading, setIsGlobalLoading] = useState<boolean>(false);
const [shouldFetch, setShouldFetch] = useState<boolean>(false);
const [text, setText] = useState<string>();
const {
Expand All @@ -16,8 +22,14 @@ export const useOpenaiSTTWithPSR = (locale: string, options: OpenaiSttOptions) =
time,
formattedTime,
} = usePersistedSpeechRecognition(locale, {
onBolbAvailable: () => setShouldFetch(true),
onTextChange: setText,
onBolbAvailable: (blobData) => {
setShouldFetch(true);
onBolbAvailable?.(blobData);
},
onTextChange: (data) => {
setText(data);
onTextChange?.(data);
},
});

const key = new Date().getDate().toString();
Expand All @@ -29,19 +41,22 @@ export const useOpenaiSTTWithPSR = (locale: string, options: OpenaiSttOptions) =
onSuccess: (data) => {
setShouldFetch(false);
setText(data);
onTextChange?.(data);
setIsGlobalLoading(false);
},
},
);

const handleStart = useCallback(() => {
setIsGlobalLoading(true);
start();
setText('');
}, [start]);

return {
blob,
formattedTime,
isLoading: isLoading || isRecording,
isLoading: isGlobalLoading || isLoading || isRecording,
isRecording,
start: handleStart,
stop,
Expand Down
25 changes: 20 additions & 5 deletions src/useOpenaiSTT/useOpenaiSTTWithSR.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { useCallback, useState } from 'react';
import useSWR from 'swr';

import { OpenaiSttOptions, fetchOpenaiSTT } from '@/services/fetchOpenaiSTT';
import { fetchOpenaiSTT } from '@/services/fetchOpenaiSTT';
import { useSpeechRecognition } from '@/useSpeechRecognition';

export const useOpenaiSTTWithSR = (locale: string, options: OpenaiSttOptions) => {
import { OpenaiSpeechRecognitionOptions } from './useOpenaiSTT';

export const useOpenaiSTTWithSR = (
locale: string,
{ onBolbAvailable, onTextChange, ...options }: OpenaiSpeechRecognitionOptions,
) => {
const [isGlobalLoading, setIsGlobalLoading] = useState<boolean>(false);
const [shouldFetch, setShouldFetch] = useState<boolean>(false);
const [text, setText] = useState<string>();
const {
Expand All @@ -16,8 +22,14 @@ export const useOpenaiSTTWithSR = (locale: string, options: OpenaiSttOptions) =>
time,
formattedTime,
} = useSpeechRecognition(locale, {
onBolbAvailable: () => setShouldFetch(true),
onTextChange: setText,
onBolbAvailable: (blobData) => {
setShouldFetch(true);
onBolbAvailable?.(blobData);
},
onTextChange: (data) => {
setText(data);
onTextChange?.(data);
},
});

const key = new Date().getDate().toString();
Expand All @@ -29,19 +41,22 @@ export const useOpenaiSTTWithSR = (locale: string, options: OpenaiSttOptions) =>
onSuccess: (data) => {
setShouldFetch(false);
setText(data);
onTextChange?.(data);
setIsGlobalLoading(false);
},
},
);

const handleStart = useCallback(() => {
setIsGlobalLoading(true);
start();
setText('');
}, [start]);

return {
blob,
formattedTime,
isLoading: isLoading || isRecording,
isLoading: isGlobalLoading || isLoading || isRecording,
isRecording,
start: handleStart,
stop,
Expand Down
6 changes: 4 additions & 2 deletions src/useSpeechRecognition/usePersistedSpeechRecognition.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useCallback, useMemo, useState } from 'react';

import { useAudioRecorder } from '@/useAudioRecorder';
import { useRecognition } from '@/useSpeechRecognition/useRecognition';

import { useRecognition } from './useRecognition';
import { SpeechRecognitionOptions } from './useSpeechRecognition';

export const usePersistedSpeechRecognition = (
locale: string,
options?: { onBolbAvailable?: (blob: Blob) => void; onTextChange?: (value: string) => void },
options?: SpeechRecognitionOptions,
) => {
const [texts, setTexts] = useState<string[]>([]);
const [isGLobalLoading, setIsGlobalLoading] = useState<boolean>(false);
Expand Down
10 changes: 6 additions & 4 deletions src/useSpeechRecognition/useSpeechRecognition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { useCallback } from 'react';
import { useAudioRecorder } from '@/useAudioRecorder';
import { useRecognition } from '@/useSpeechRecognition/useRecognition';

export const useSpeechRecognition = (
locale: string,
options?: { onBolbAvailable?: (blob: Blob) => void; onTextChange?: (value: string) => void },
) => {
export interface SpeechRecognitionOptions {
onBolbAvailable?: (blob: Blob) => void;
onTextChange?: (value: string) => void;
}

export const useSpeechRecognition = (locale: string, options?: SpeechRecognitionOptions) => {
const {
time,
formattedTime,
Expand Down

0 comments on commit 0015bac

Please sign in to comment.