-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomePage.jsx
88 lines (73 loc) · 3.49 KB
/
HomePage.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import React, { useState, useEffect, useRef } from 'react'
export default function HomePage(props) {
const { setAudioStream, setFile } = props
const [recordingStatus, setRecordingStatus] = useState('inactive')
const [audioChunks, setAudioChunks] = useState([])
const [duration, setDuration] = useState(0)
const mediaRecorder = useRef(null)
const mimeType = 'audio/webm'
async function startRecording() {
let tempStream
console.log('Start recording')
try {
const streamData = await navigator.mediaDevices.getUserMedia({
audio: true,
video: false
})
tempStream = streamData
} catch (err) {
console.log(err.message)
return
}
setRecordingStatus('recording')
//create new Media recorder instance using the stream
const media = new MediaRecorder(tempStream, { type: mimeType })
mediaRecorder.current = media
mediaRecorder.current.start()
let localAudioChunks = []
mediaRecorder.current.ondataavailable = (event) => {
if (typeof event.data === 'undefined') { return }
if (event.data.size === 0) { return }
localAudioChunks.push(event.data)
}
setAudioChunks(localAudioChunks)
}
async function stopRecording() {
setRecordingStatus('inactive')
console.log('Stop recording')
mediaRecorder.current.stop()
mediaRecorder.current.onstop = () => {
const audioBlob = new Blob(audioChunks, { type: mimeType })
setAudioStream(audioBlob)
setAudioChunks([])
setDuration(0)
}
}
useEffect(() => {
if (recordingStatus === 'inactive') { return }
const interval = setInterval(() => {
setDuration(curr => curr + 1)
}, 1000)
return () => clearInterval(interval)
})
return (
<main className='flex-1 p-4 flex flex-col gap-3 text-center sm:gap-4 justify-center pb-20'>
<h1 className='font-semibold text-5xl sm:text-6xl md:text-7xl'><span className='text-gray-600 bold'>Scribe</span> <span className='text-blue-400'>AI</span></h1>
<h3 className='font-medium md:text-lg'>Record <span className='text-blue-400'>→</span> Transcribe <span className='text-blue-400'>→</span> Translate</h3>
<button onClick={recordingStatus === 'recording' ? stopRecording : startRecording} className='flex specialBtn px-4 py-2 rounded-xl items-center text-base justify-between gap-4 mx-auto w-72 max-w-full my-4'>
<p className='text-blue-400'>{recordingStatus === 'inactive' ? 'Record' : `Stop recording`}</p>
<div className='flex items-center gap-2'>
{/* {duration !== 0 && (
<p className='text-sm'>{duration}s</p>
)} */}
<i className={"fa-solid duration-200 fa-microphone " + (recordingStatus === 'recording' ? ' text-rose-300' : "")}></i>
</div>
</button>
<p className='text-base'>Or <label className='text-blue-400 cursor-pointer hover:text-blue-600 duration-200'>upload <input onChange={(e) => {
const tempFile = e.target.files[0]
setFile(tempFile)
}} className='hidden' type='file' accept='.mp3,.wave' /></label> a mp3 file</p>
<p className='italic text-slate-400'>Free now free forever</p>
</main>
)
}