Skip to content

Commit

Permalink
✨ feat: Add tts content clean
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Dec 1, 2023
1 parent 447eeb8 commit 6879553
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@
"openai": "^4.17.3",
"query-string": "^8",
"react-error-boundary": "^4",
"remark-gfm": "^3",
"remark-parse": "^10",
"swr": "^2",
"unified": "^11",
"url-join": "^5",
"uuid": "^9"
},
Expand Down Expand Up @@ -122,6 +125,7 @@
"semantic-release": "^21",
"tsx": "^4.1.2",
"typescript": "^5",
"unist-util-visit": "^5",
"vercel": "^28"
},
"peerDependencies": {
Expand Down
23 changes: 23 additions & 0 deletions src/core/utils/cleanContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import remarkGfm from 'remark-gfm';
import remarkParse from 'remark-parse';
import { unified } from 'unified';
import { visit } from 'unist-util-visit';

// @ts-ignore
const convertMarkdownToMdast = async (md: string) => {
// @ts-ignore
return unified().use(remarkParse).use(remarkGfm).parse(md.trim());
};

export const cleanContent = async (content: string) => {
try {
const mdast = await convertMarkdownToMdast(content.trim());
const newContent: string[] = [];
visit(mdast, 'text', (node: any) => {
if (node?.value) newContent.push(node.value.trim());
});
return newContent.join('');
} catch {
return content.trim();
}
};
30 changes: 17 additions & 13 deletions src/react/hooks/useAudioPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,23 @@ export const useAudioPlayer = ({
const [isPlaying, setIsPlaying] = useState(false);
const [isGlobalLoading, setIsGlobalLoading] = useState(true);

const { isLoading } = useSWR(src || null, async () => {
if (!src) return;
setIsGlobalLoading(true);
const data = await fetch(src);
const arrayBuffer = await data.arrayBuffer();
setArrayBuffers([arrayBuffer]);
const newBlob = new Blob([arrayBuffer], { type: type });
audioRef.current.pause();
audioRef.current.currentTime = 0;
if (audioRef.current.src) URL.revokeObjectURL(audioRef.current.src);
audioRef.current.src = URL.createObjectURL(newBlob);
audioRef.current.load();
});
const { isLoading } = useSWR(
src || null,
async () => {
if (!src) return;
setIsGlobalLoading(true);
const data = await fetch(src);
const arrayBuffer = await data.arrayBuffer();
setArrayBuffers([arrayBuffer]);
const newBlob = new Blob([arrayBuffer], { type: type });
audioRef.current.pause();
audioRef.current.currentTime = 0;
if (audioRef.current.src) URL.revokeObjectURL(audioRef.current.src);
audioRef.current.src = URL.createObjectURL(newBlob);
audioRef.current.load();
},
{ revalidateOnFocus: false },
);

useEffect(() => {
if (!audioRef.current) return;
Expand Down
1 change: 1 addition & 0 deletions src/react/hooks/useBlobUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const useBlobUrl = (src: string) => {
setAudio(newAudio.audio);
setIsGlobalLoading(false);
},
revalidateOnFocus: false,
},
);

Expand Down
2 changes: 1 addition & 1 deletion src/react/useOpenAISTT/useOpenAISTTCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export const useOpenAISTTCore = (init: OpenAISTTCoreOptions) => {
const instance = new OpenaiSTT(api);
return instance.create({ options, speech });
},
swrConfig,
{ revalidateOnFocus: false, ...swrConfig },
);
};
8 changes: 6 additions & 2 deletions src/react/useTTS/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback, useEffect, useState } from 'react';
import useSWR, { type SWRConfiguration, type SWRResponse } from 'swr';

import { cleanContent } from '@/core/utils/cleanContent';
import { splitTextIntoSegments } from '@/core/utils/splitTextIntoSegments';
import { type AudioProps } from '@/react/AudioPlayer';
import { useStreamAudioPlayer } from '@/react/hooks/useStreamAudioPlayer';
Expand Down Expand Up @@ -65,6 +66,7 @@ export const useTTS = (
setIsGlobalLoading(false);
}
},
revalidateOnFocus: false,
...restSWRConfig,
},
);
Expand All @@ -78,8 +80,10 @@ export const useTTS = (
}, [text, isLoading]);

useEffect(() => {
const texts = splitTextIntoSegments(text);
handleReset(texts);
cleanContent(text).then((content) => {
const texts = splitTextIntoSegments(content);
handleReset(texts);
});
return () => {
handleReset();
};
Expand Down

0 comments on commit 6879553

Please sign in to comment.