Skip to content

Commit

Permalink
feat: init vad
Browse files Browse the repository at this point in the history
  • Loading branch information
PrinceBaghel258025 committed Sep 22, 2024
1 parent 9fab122 commit 65dd5b6
Show file tree
Hide file tree
Showing 13 changed files with 566 additions and 238 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@radix-ui/react-toggle": "1.0.3",
"@radix-ui/react-tooltip": "1.0.6",
"@react-hook/media-query": "1.1.1",
"@ricky0123/vad-react": "^0.0.24",
"@sentry/nextjs": "7.61.0",
"@t3-oss/env-core": "0.3.1",
"@t3-oss/env-nextjs": "0.3.1",
Expand Down
82 changes: 82 additions & 0 deletions public/vad/ort-training-wasm-simd-threaded.mjs

Large diffs are not rendered by default.

Binary file added public/vad/ort-training-wasm-simd-threaded.wasm
Binary file not shown.
127 changes: 127 additions & 0 deletions public/vad/ort-wasm-simd-threaded.jsep.mjs

Large diffs are not rendered by default.

Binary file added public/vad/ort-wasm-simd-threaded.jsep.wasm
Binary file not shown.
80 changes: 80 additions & 0 deletions public/vad/ort-wasm-simd-threaded.mjs

Large diffs are not rendered by default.

Binary file added public/vad/ort-wasm-simd-threaded.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions public/vad/polyfills.js

Large diffs are not rendered by default.

Binary file added public/vad/silero_vad.onnx
Binary file not shown.
1 change: 1 addition & 0 deletions public/vad/vad.worklet.bundle.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions src/components/VadAudio.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"use client";

import { useState, useRef, useCallback } from "react";
import { useMicVAD, utils } from "@ricky0123/vad-react";
import { Microphone, StopCircle } from "@phosphor-icons/react";
import { Button } from "@/components/button";

interface VadAudioProps {
onAudioCapture: (audioFile: File) => void;
onStartListening: () => void;
onStopListening: () => void;
}

export default function VadAudio({
onAudioCapture,
onStartListening,
onStopListening,
}: VadAudioProps) {
const [isListening, setIsListening] = useState(false);
const audioChunks = useRef<Blob[]>([]);

const vad = useMicVAD({
onSpeechEnd: (audio: Float32Array) => {
const wavBuffer = utils.encodeWAV(audio);
const audioBlob = new Blob([wavBuffer], { type: "audio/wav" });
const audioFile = new File([audioBlob], "audio.wav", {
type: "audio/wav",
});
console.log("audioFile", audioFile);

onAudioCapture(audioFile);
},
workletURL: "/vad/vad.worklet.bundle.min.js",
modelURL: "/vad/silero_vad.onnx",
ortConfig: (ort) => {
ort.env.wasm.wasmPaths = "/vad/";
},
startOnLoad: false,
});

const handleStartListening = useCallback(() => {
vad.start();
onStartListening();
setIsListening(true);
audioChunks.current = [];
}, [vad]);
// console.log("vad.start()", vad.errored, vad.loading, vad.userSpeaking, vad.listening);

const handleStopListening = useCallback(() => {
setIsListening(false);
onStopListening();
vad.pause();
}, [vad]);

return (
<div className="flex items-center gap-2">
<Button
onClick={vad.listening ? handleStopListening : handleStartListening}
size="icon"
variant="secondary"
type="button"
className="disabled:text-muted"
>
{vad.listening ? (
<StopCircle
className="h-4 w-4 fill-current"
color="#618a9e"
weight="bold"
/>
) : (
<Microphone
className="h-4 w-4 fill-current"
color="#618a9e"
weight="bold"
/>
)}
</Button>
</div>
);
}
Loading

0 comments on commit 65dd5b6

Please sign in to comment.