Skip to content

Commit

Permalink
Merge pull request #166 from SamagraX-Stencil/feat/voicerecorder
Browse files Browse the repository at this point in the history
feat: voicerecorder implementation
  • Loading branch information
ankitmlesmagico committed Jul 31, 2024
2 parents cdfcd7d + e78ab1c commit a4e92c8
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/molecules/src/voice-recorder/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ interface VoiceRecorder {
setInputMsg: (msg: string) => void;
tapToSpeak: boolean;
includeDiv?: boolean;
showVoiceRecorder?: boolean;
delayBetweenDialogs?: number;
handleVoiceRecorder?: () => void;
styles?: object;
}

const VoiceRecorder: React.FC<VoiceRecorder> = ({
setInputMsg,
tapToSpeak,
includeDiv = false,
showVoiceRecorder = true,
delayBetweenDialogs,
handleVoiceRecorder,
styles: customStyles,
}) => {
const config = useUiConfig('component', 'voiceRecorder');

Expand All @@ -23,14 +31,15 @@ const VoiceRecorder: React.FC<VoiceRecorder> = ({
const [recorderStatus, setRecorderStatus] = useState('idle');

const voiceMinDecibels: number = config.voiceMinDecibels;
const delayBetweenDialogs: number = config.delayBetweenDialogs;
const actualDelayBetweenDialogs: number = delayBetweenDialogs || config.delayBetweenDialogs;
const dialogMaxLength: number = config.dialogMaxLength;
const [isRecording, setIsRecording] = useState(config.isRecording);

const startRecording = () => {
if (!isRecording) {
setIsRecording(true);
record();
if (handleVoiceRecorder) handleVoiceRecorder();
}
};

Expand Down Expand Up @@ -77,19 +86,16 @@ const VoiceRecorder: React.FC<VoiceRecorder> = ({

time = new Date();
const currentTime = time.getTime();

//time out:
if (currentTime > startTime + dialogMaxLength) {
recorder.stop();
return;
}

//a dialog detected:
if (anySoundDetected === true && currentTime > lastDetectedTime + delayBetweenDialogs) {
if (anySoundDetected === true && currentTime > lastDetectedTime + actualDelayBetweenDialogs) {
recorder.stop();
return;
}

//check for detection:
analyser.getByteFrequencyData(domainData);
for (let i = 0; i < bufferLength; i++)
Expand All @@ -98,12 +104,10 @@ const VoiceRecorder: React.FC<VoiceRecorder> = ({
time = new Date();
lastDetectedTime = time.getTime();
}

//continue the loop:
window?.requestAnimationFrame(detectSound);
};
window?.requestAnimationFrame(detectSound);

//stop event:
recorder.addEventListener('stop', () => {
//stop all the tracks:
Expand All @@ -116,6 +120,7 @@ const VoiceRecorder: React.FC<VoiceRecorder> = ({
});
});
}

const makeComputeAPICall = async (blob: Blob) => {
try {
setRecorderStatus('processing');
Expand All @@ -140,6 +145,8 @@ const VoiceRecorder: React.FC<VoiceRecorder> = ({
}
};

if (!showVoiceRecorder) return null;

return (
<div>
<div>
Expand Down

0 comments on commit a4e92c8

Please sign in to comment.