Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fake Mic recording #122

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 15 additions & 38 deletions src/lib/bot/comm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { MdMic, MdStop } from "react-icons/md"
import { api } from "../api"
import { FaStopCircle } from "react-icons/fa"
import "./comm.css"
import { useReactMediaRecorder } from "react-media-recorder"
import mp3 from "./signpostai demo.mp3"


async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}

interface Props {
bot: number
Expand All @@ -15,51 +20,25 @@ export function Comm(props: Props) {
const [state, setState] = useState<"ready" | "recoding" | "waiting" | "playing">("ready")
const [audio, setAudio] = useState<string>(null)

const {
status,
startRecording,
stopRecording,
mediaBlobUrl,
clearBlobUrl,
} = useReactMediaRecorder({ audio: true })



async function onStart() {
setState("recoding")
clearBlobUrl()
startRecording()



}

const onStop = () => {
const onStop = async () => {
setAudio(mp3)

setState("waiting")
stopRecording()
}
await sleep(1000)

const handleRecordingStop = async () => {
if (mediaBlobUrl) {
try {
const response = await fetch(mediaBlobUrl)
const blob = await response.blob()
const audio = await blobToBase64(blob)
setState("playing")

const { messages } = await api.askbot({ audio }, true, [{ label: "", value: bot, history: [] }])
setAudio(messages[0].message)

clearBlobUrl()
setState("playing")
} catch (error) {
console.error("Error processing the recording: ", error)
}
}
}

useEffect(() => {
if (status === "stopped" && mediaBlobUrl) {
handleRecordingStop()
}
}, [status, mediaBlobUrl])


let classNameColor = state === "recoding" ? "bg-red-500" : "bg-blue-500"

Expand Down Expand Up @@ -168,10 +147,8 @@ function SpeechVisualizer({ audio, onEnd }: SpeechVisualizerProps) {

const player = audioRef.current

const blob = base64ToBlob(audio)

player.pause()
player.src = blob
player.src = audio

const audioContext = new AudioContext()
const source = audioContext.createMediaElementSource(player)
Expand Down
Binary file added src/lib/bot/signpostai demo.mp3
Binary file not shown.