-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf5ddfd
commit c79596c
Showing
9 changed files
with
181 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
.currently-reading { | ||
|
||
} | ||
|
||
.currently-reading .currently-reading-text { | ||
.currently-reading .current-sentence { | ||
font-size: 24px; | ||
} | ||
|
||
.currently-reading .currently-reading-text .currentword { | ||
.currently-reading .current-sentence .current-word { | ||
font-size: 24px; | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {parseContent} from "../utils/parseContent"; | ||
|
||
export const fetchSentences = async () => { | ||
const apiHost = location.href.replace(location.port, '5174'); | ||
const reply= await fetch(`${apiHost}content`); | ||
const {content} = await reply.json(); | ||
return parseContent(content); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,55 @@ | ||
import { useState } from 'react'; | ||
|
||
import { PlayingState } from './speech'; | ||
import {useEffect, useState} from 'react'; | ||
import { PlayingState, createSpeechEngine } from './speech'; | ||
import {makeRanges} from "../utils/makeRanges"; | ||
|
||
/* | ||
@description | ||
Implement a custom useSpeech hook that uses a speech engine defined in 'speech.ts' | ||
to play the sentences that have been fetched and parsed previously. | ||
This hook should return react friendly controls for playing, and pausing audio as well as provide information about | ||
the currently read word and sentence | ||
*/ | ||
|
||
const useSpeech = (sentences: Array<string>) => { | ||
const [currentSentenceIdx, setCurrentSentenceIdx] = useState(0); | ||
const [currentWordRange, setCurrentWordRange] = useState([0, 0]); | ||
const [currentSentenceWordIdx, setCurrentSentenceWordIdx] = useState(0); | ||
const [controls, setControls] = useState<{ play: () => void; pause: () => void; }>(); | ||
const [playbackState, setPlaybackState] = useState<PlayingState>('paused'); | ||
const sentencesRanges = makeRanges(sentences); | ||
|
||
useEffect(() => { | ||
|
||
const { | ||
play, | ||
pause, | ||
load | ||
} = createSpeechEngine({ | ||
onBoundary: ({ charIndex }) => { | ||
const sentenceIndex = sentencesRanges.findIndex(({ from, to }) => charIndex >= from && charIndex <= to); | ||
setCurrentSentenceIdx(sentenceIndex); | ||
|
||
const charactersLeft = sentencesRanges[sentenceIndex].from; | ||
const wordsRanges = makeRanges(sentences[sentenceIndex].split(' ')); | ||
const charIndexInWord = charIndex - charactersLeft; | ||
const wordIndex = wordsRanges.findIndex(({ from, to }) => charIndexInWord >= from && charIndexInWord <= to) | ||
setCurrentSentenceWordIdx(wordIndex); | ||
}, | ||
onEnd: () => {}, | ||
onStateUpdate: (state: PlayingState) => setPlaybackState(state), | ||
}) | ||
|
||
const [playbackState, setPlaybackState] = useState<PlayingState>("paused"); | ||
setControls({ play, pause }); | ||
load(sentences.join(' ')); | ||
|
||
const play = () => {}; | ||
const pause = () => {}; | ||
}, [sentences]); | ||
|
||
return { | ||
currentSentenceIdx, | ||
currentWordRange, | ||
currentSentenceWordIdx, | ||
playbackState, | ||
play, | ||
pause, | ||
...controls, | ||
}; | ||
}; | ||
|
||
export { useSpeech }; | ||
export default useSpeech; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export const makeRanges = (sentences: Array<string>) => | ||
sentences.reduce((acc: Array<{ from: number, to: number }>, v: string, i) => { | ||
|
||
let content = { | ||
from: 0, | ||
to: v.length, | ||
} | ||
|
||
if (i) { | ||
const spaceLength = 1; | ||
const from = acc[i - 1].to + spaceLength; | ||
content = { | ||
from, | ||
to: from + v.length | ||
} | ||
} | ||
|
||
acc.push(content); | ||
return acc; | ||
}, []); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const parseContent = (content: string) => | ||
content.replace('</speak>', '').replace('<speak>', '').split('</s>').reduce((acc: Array<string>, v: string) => { | ||
if (v.match('^<s>[\\s\\S]+')) { | ||
acc.push(v.replace('<s>', '')); | ||
} else if (v.includes('<s>')) { | ||
const placement = v.indexOf('<s>'); | ||
acc.push(v.slice(placement).replace('<s>', '')); | ||
} | ||
return acc; | ||
}, []); |