MCPcopy Create free account
hub / github.com/cactus-compute/cactus-react-native / StreamSTTScreen

Function StreamSTTScreen

example/src/StreamSTTScreen.tsx:17–168  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

15const CHUNK_SIZE = 16000 * 2 * 3;
16
17const StreamSTTScreen = () => {
18 const cactusSTT = useCactusSTT({ model: 'whisper-small' });
19 const [audioFile, setAudioFile] = useState<string | null>(null);
20 const [audioFileName, setAudioFileName] = useState<string>('');
21
22 useEffect(() => {
23 if (!cactusSTT.isDownloaded) {
24 cactusSTT.download();
25 }
26 // eslint-disable-next-line react-hooks/exhaustive-deps
27 }, [cactusSTT.isDownloaded]);
28
29 const handleSelectAudio = async () => {
30 try {
31 const res = await DocumentPicker.pick({
32 type: [DocumentPicker.types.audio],
33 });
34 if (res && res.length > 0) {
35 const fileName = `audio_${Date.now()}.wav`;
36 const destPath = `${RNFS.CachesDirectoryPath}/${fileName}`;
37 await RNFS.copyFile(res[0].uri, destPath);
38 setAudioFile(destPath);
39 setAudioFileName(res[0].name || 'Unknown');
40 }
41 } catch (err) {
42 console.error(err);
43 }
44 };
45
46 const readAudioFile = async (filePath: string): Promise<Uint8Array> => {
47 const base64Audio = await RNFS.readFile(filePath, 'base64');
48 const binaryString = atob(base64Audio);
49 const bytes = new Uint8Array(binaryString.length);
50 for (let i = 0; i < binaryString.length; i++) {
51 bytes[i] = binaryString.charCodeAt(i);
52 }
53 // Skip WAV header (44 bytes)
54 return bytes.slice(44);
55 };
56
57 const handleStreamTranscribe = async () => {
58 if (!audioFile) return;
59 try {
60 await cactusSTT.streamTranscribeStart({ confirmationThreshold: 0.99 });
61
62 const pcmData = await readAudioFile(audioFile);
63
64 for (let i = 0; i < pcmData.length; i += CHUNK_SIZE) {
65 const chunk = pcmData.slice(i, i + CHUNK_SIZE);
66 await cactusSTT.streamTranscribeProcess({
67 audio: Array.from(chunk),
68 });
69 }
70
71 await cactusSTT.streamTranscribeStop();
72 } catch (err) {
73 console.error('Stream error:', err);
74 }

Callers

nothing calls this directly

Calls 2

useCactusSTTFunction · 0.85
downloadMethod · 0.45

Tested by

no test coverage detected