Skip to content

Commit

Permalink
chore: vad chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
PrinceBaghel258025 committed Nov 7, 2024
1 parent 21fc686 commit 9a8a590
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
6 changes: 4 additions & 2 deletions src/components/VadAudio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ interface VadAudioProps {
onStartListening: () => void;
onStopListening: () => void;
isHome?: boolean;
isTranscribing?: boolean;
}

export default function VadAudio({
onAudioCapture,
onStartListening,
onStopListening,
isTranscribing = false,
isHome = false,
}: VadAudioProps) {
const [isListening, setIsListening] = useState(false);
Expand Down Expand Up @@ -70,6 +72,7 @@ export default function VadAudio({
setIsListening(false);
onStopListening();
vad.pause();
console.log("pausing vad", vad.listening);
resetDuration();
clearTimer();
if (isSupported) {
Expand All @@ -82,7 +85,6 @@ export default function VadAudio({
timerRef.current = setInterval(() => {
if (startTimeRef.current) {
const elapsed = Date.now() - startTimeRef.current;
console.log("elapsed", elapsed);
const minutes = Math.floor(elapsed / 60000);
const seconds = Math.floor((elapsed % 60000) / 1000);
setDuration(
Expand Down Expand Up @@ -127,7 +129,7 @@ export default function VadAudio({
variant={vad.listening ? "destructive" : "secondary"}
type="button"
className="disabled:text-muted"
disabled={vad.loading}
disabled={vad.loading || (isTranscribing && !vad.listening)}
>
{vad.listening ? (
<StopCircle
Expand Down
26 changes: 13 additions & 13 deletions src/components/inputBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SetStateAction,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import { ChatRequestOptions, CreateMessage, Message, nanoid } from "ai";
Expand Down Expand Up @@ -473,6 +474,10 @@ const InputBar = (props: InputBarProps) => {
[key: number]: string;
}>({});

const [isBlinking, setIsBlinking] = useState(false); // Control blinking state
const [displayNumber, setDisplayNumber] = useState(1);
const isLastChunk = useRef(false);

const handleAudioChunk = async (audioChunk: File) => {
const newAudioId = audioId + 1;
setAudioId(newAudioId);
Expand All @@ -490,6 +495,11 @@ const InputBar = (props: InputBarProps) => {
...prev,
[newAudioId]: data.text,
}));
if (isLastChunk.current) {
setTimeout(() => {
setTranscriptHashTable({});
}, 1000);
}
// props?.setInput?.((prev) => prev + data.text);
setIsTranscribing(false);
} catch (err) {
Expand Down Expand Up @@ -595,9 +605,6 @@ const InputBar = (props: InputBarProps) => {
// setDisableInputs(true)
};

const [isBlinking, setIsBlinking] = useState(false); // Control blinking state
const [displayNumber, setDisplayNumber] = useState(1);

useEffect(() => {
let interval: any;
if (isBlinking) {
Expand All @@ -609,16 +616,6 @@ const InputBar = (props: InputBarProps) => {
return () => clearInterval(interval);
}, [isBlinking]);

useEffect(() => {
if (!isBlinking) {
const resetTimer = setTimeout(() => {
setTranscriptHashTable({});
}, 5000); // Reset after 5 seconds

return () => clearTimeout(resetTimer);
}
}, [isBlinking]);

return (
<form
onSubmit={handleSubmit}
Expand Down Expand Up @@ -748,7 +745,9 @@ const InputBar = (props: InputBarProps) => {
/>
</Button> */}
<VadAudio
isTranscribing={isTranscribing}
onStartListening={() => {
isLastChunk.current = false;
setIsBlinking(true);
setIsAudioWaveVisible(true);
const newAudioId = audioId + 1;
Expand All @@ -758,6 +757,7 @@ const InputBar = (props: InputBarProps) => {
}));
}}
onStopListening={() => {
isLastChunk.current = true;
setIsBlinking(false);
// setTranscriptHashTable({});
setIsAudioWaveVisible(false);
Expand Down
21 changes: 17 additions & 4 deletions src/components/inputBar2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SetStateAction,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import { ChatRequestOptions, CreateMessage, Message, nanoid } from "ai";
Expand Down Expand Up @@ -342,6 +343,9 @@ const InputBar = (props: InputBarProps) => {
const [transcriptHashTable, setTranscriptHashTable] = useState<{
[key: number]: string;
}>({});
const [isBlinking, setIsBlinking] = useState(false); // Control blinking state
const [displayNumber, setDisplayNumber] = useState(1);
const isLastChunk = useRef(false);

const handleAudioChunk = async (audioChunk: File) => {
const newAudioId = audioId + 1;
Expand All @@ -360,6 +364,12 @@ const InputBar = (props: InputBarProps) => {
...prev,
[newAudioId]: data.text,
}));
if (isLastChunk.current) {
console.log("resetting transcript hash table", isLastChunk.current);
setTimeout(() => {
setTranscriptHashTable({});
}, 1000);
}
setIsTranscribing(false);
} catch (err) {
console.error("got in error", err);
Expand Down Expand Up @@ -391,9 +401,6 @@ const InputBar = (props: InputBarProps) => {
}
};

const [isBlinking, setIsBlinking] = useState(false); // Control blinking state
const [displayNumber, setDisplayNumber] = useState(1);

useEffect(() => {
let interval: any;
if (isBlinking) {
Expand Down Expand Up @@ -509,7 +516,9 @@ const InputBar = (props: InputBarProps) => {
<div>
<div className="flex gap-2">
<VadAudio
isTranscribing={isTranscribing}
onStartListening={() => {
isLastChunk.current = false;
setIsBlinking(true);
setIsAudioWaveVisible(true);
const newAudioId = audioId + 1;
Expand All @@ -521,13 +530,17 @@ const InputBar = (props: InputBarProps) => {
setIsAudioWaveVisible(true);
}}
onStopListening={() => {
isLastChunk.current = true;
setIsBlinking(false);
setTranscriptHashTable({});
setIsAudioWaveVisible(false);
}}
// disabled={isRecording || isTranscribing || disableInputs}
onAudioCapture={(file: File) => {
// trigger a call to the backend to transcribe the audio
console.log(
"checking isBlinking onAudioCapture",
isLastChunk.current,
);
handleAudioChunk(file);
}}
isHome={props.isHome}
Expand Down

0 comments on commit 9a8a590

Please sign in to comment.