Skip to content
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
19 changes: 12 additions & 7 deletions lib/VoicePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class VoicePlayer extends Component {
}
}

createSpeech = () => {
createSpeech () {
const defaults = {
text: '',
volume: 1,
Expand All @@ -25,29 +25,35 @@ class VoicePlayer extends Component {
lang: 'en-US'
}

const options = Object.assign({}, defaults, this.props)

let speech = new SpeechSynthesisUtterance()

Object.assign(speech, defaults, this.props)
speech.text = options.text
speech.volume = options.volume
speech.rate = options.rate
speech.pitch = options.pitch
speech.lang = options.lang

return speech
}

speak = () => {
speak () {
window.speechSynthesis.speak(this.speech)
this.setState({ started: true, playing: true })
}

cancel = () => {
cancel () {
window.speechSynthesis.cancel()
this.setState({ started: false, playing: false })
}

pause = () => {
pause () {
window.speechSynthesis.pause()
this.setState({ playing: false })
}

resume = () => {
resume () {
window.speechSynthesis.resume()
this.setState({ playing: true })
}
Expand Down Expand Up @@ -98,4 +104,3 @@ class VoicePlayer extends Component {
}

export default VoicePlayer

23 changes: 13 additions & 10 deletions lib/VoiceRecognition.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@ class VoiceRecognition extends Component {
}
}

createRecognition = (SpeechRecognition) => {
createRecognition(SpeechRecognition){
const defaults = {
continuous: true,
interimResults: false,
lang: 'en-US'
}

const options = Object.assign({}, defaults, this.props)

let recognition = new SpeechRecognition()

Object.assign(recognition, defaults, this.props)

recognition.continuous = options.continuous
recognition.interimResults = options.interimResults
recognition.lang = options.lang

return recognition
}

bindResult = (event) => {
bindResult (props, event) {
let interimTranscript = ''
let finalTranscript = ''

Expand All @@ -43,18 +47,18 @@ class VoiceRecognition extends Component {
}
}

this.props.onResult({ interimTranscript, finalTranscript })
props.onResult({ interimTranscript, finalTranscript })
}

start = () => {
start () {
this.recognition.start()
}

stop = () => {
stop () {
this.recognition.stop()
}

abort = () => {
abort () {
this.recognition.abort()
}

Expand All @@ -75,7 +79,7 @@ class VoiceRecognition extends Component {
this.recognition.addEventListener(event.name, event.action)
})

this.recognition.addEventListener('result', this.bindResult)
this.recognition.addEventListener('result', this.bindResult.bind(null,this.props), false)

this.start()
}
Expand All @@ -90,4 +94,3 @@ class VoiceRecognition extends Component {
}

export default VoiceRecognition