-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- update packages - add speech button - add filesystem utils - add ssml as i18next context - add settings for voice selection - refactor chapter sections providing
- Loading branch information
1 parent
fa453e7
commit 95fe32e
Showing
43 changed files
with
2,278 additions
and
1,532 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Azure Cognitive Service (Text-To-Speech) | ||
REACT_APP_AZURE_COGNITIVE_TTS_SUBSCRIPTION_KEY="" | ||
REACT_APP_AZURE_COGNITIVE_TTS_SERVICE_REGION="" |
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 |
---|---|---|
|
@@ -19,5 +19,5 @@ | |
"splashFullScreen": true, | ||
"splashImmersive": true | ||
} | ||
}, | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,33 @@ | ||
import { IonFab, IonFabButton } from '@ionic/react'; | ||
import React from 'react'; | ||
import { useParams } from 'react-router'; | ||
import { ChapterId } from '../../utils/chapters'; | ||
import { formatSecondsToTimeMinutes } from '../../utils/format'; | ||
import { useSpeechAudio } from './hooks/useSpeechAudio'; | ||
import { SpeechFabButtonContent } from './SpeechFabButtonContent'; | ||
|
||
interface RouteChapterProps { | ||
chapterId: ChapterId; | ||
sectionId: string; | ||
} | ||
|
||
export const SpeechFabButton = () => { | ||
const { chapterId, sectionId } = useParams<RouteChapterProps>(); | ||
const { isLoading, isPlaying, pause, play, restTime } = useSpeechAudio( | ||
chapterId, | ||
sectionId, | ||
); | ||
|
||
return ( | ||
<IonFab vertical="top" horizontal="end" edge slot="fixed"> | ||
<IonFabButton | ||
color="secondary" | ||
onClick={() => (isPlaying ? pause() : play())} | ||
> | ||
<SpeechFabButtonContent isLoading={isLoading} isPlaying={isPlaying}> | ||
{formatSecondsToTimeMinutes(restTime)} | ||
</SpeechFabButtonContent> | ||
</IonFabButton> | ||
</IonFab> | ||
); | ||
}; |
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,33 @@ | ||
import { IonIcon, IonSpinner } from '@ionic/react'; | ||
import { pause, play } from 'ionicons/icons'; | ||
import React from 'react'; | ||
import './styles.css'; | ||
|
||
type Props = { | ||
isLoading?: boolean; | ||
isPlaying?: boolean; | ||
}; | ||
|
||
export const SpeechFabButtonContent: React.FC<Props> = ({ | ||
isLoading = false, | ||
isPlaying = false, | ||
children, | ||
}) => { | ||
return ( | ||
<> | ||
{isLoading ? ( | ||
<IonSpinner /> | ||
) : isPlaying ? ( | ||
<> | ||
<IonIcon | ||
className={children ? 'audio-fab-button-show-time' : ''} | ||
icon={pause} | ||
/> | ||
{children} | ||
</> | ||
) : ( | ||
<IonIcon icon={play} /> | ||
)} | ||
</> | ||
); | ||
}; |
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,26 @@ | ||
import { useEffect } from 'react'; | ||
import { ChapterId } from '../../../utils/chapters'; | ||
import { useChapterSectionSsml } from '../../../utils/hooks/useChapterSectionSsml'; | ||
import { useAudio } from '../../../utils/hooks/useAudio'; | ||
import { useSpeechSource } from './useSpeechSource'; | ||
|
||
export const useSpeechAudio = (chapterId: ChapterId, sectionId: string) => { | ||
const { filesystem, speak } = useChapterSectionSsml(chapterId, sectionId); | ||
|
||
const { isLoading, load, source } = useSpeechSource(speak, filesystem.path); | ||
const { isPlaying, pause, play, restTime } = useAudio(source); | ||
|
||
useEffect(() => { | ||
pause(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [speak]); | ||
|
||
const playAudio = async () => { | ||
if (!source) { | ||
await load(); | ||
} | ||
play(); | ||
}; | ||
|
||
return { isLoading, isPlaying, play: playAudio, pause, restTime }; | ||
}; |
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,32 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { | ||
existsFileInCache, | ||
readFileFromCache, | ||
} from '../../../utils/filesystem'; | ||
import { synthesizeSpeechToFile } from '../utils'; | ||
|
||
export const useSpeechSource = (ssml: string, path: string) => { | ||
const [source, setSource] = useState(''); | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
useEffect(() => { | ||
setSource(''); | ||
}, [path]); | ||
|
||
const load = async () => { | ||
setIsLoading(true); | ||
try { | ||
const existFile = await existsFileInCache(path); | ||
if (!existFile) { | ||
await synthesizeSpeechToFile(ssml, path); | ||
} | ||
const file = await readFileFromCache(path); | ||
setSource(file); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
setIsLoading(false); | ||
}; | ||
|
||
return { isLoading, load, source }; | ||
}; |
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 @@ | ||
export { SpeechFabButton } from './SpeechFabButton'; |
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,4 @@ | ||
.audio-fab-button-show-time { | ||
opacity: 0.25; | ||
position: absolute; | ||
} |
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,46 @@ | ||
import { | ||
AudioConfig, | ||
AudioOutputStream, | ||
SpeechConfig, | ||
SpeechSynthesizer, | ||
} from 'microsoft-cognitiveservices-speech-sdk'; | ||
import { writeAudioBufferToCache } from '../../utils/filesystem'; | ||
|
||
const getAudioConfig = () => { | ||
const stream = AudioOutputStream.createPullStream(); | ||
const audioConfig = AudioConfig.fromStreamOutput(stream); | ||
return audioConfig; | ||
}; | ||
|
||
const getSpeechConfig = () => { | ||
const speechConfig = SpeechConfig.fromSubscription( | ||
process.env.REACT_APP_AZURE_COGNITIVE_TTS_SUBSCRIPTION_KEY || '', | ||
process.env.REACT_APP_AZURE_COGNITIVE_TTS_SERVICE_REGION || '', | ||
); | ||
return speechConfig; | ||
}; | ||
|
||
export const synthesizeSpeechToFile = async (ssml: string, path: string) => { | ||
const audioConfig = getAudioConfig(); | ||
const speechConfig = getSpeechConfig(); | ||
const synthesizer = new SpeechSynthesizer(speechConfig, audioConfig); | ||
|
||
return new Promise<ArrayBuffer>((resolve, reject) => | ||
synthesizer.speakSsmlAsync( | ||
ssml, | ||
async (result) => { | ||
synthesizer.close(); | ||
const { audioData, errorDetails } = result; | ||
if (!audioData) { | ||
reject(errorDetails); | ||
} | ||
await writeAudioBufferToCache(path, audioData, 'audio/wav'); | ||
resolve(audioData); | ||
}, | ||
(error) => { | ||
synthesizer.close(); | ||
reject(error); | ||
}, | ||
), | ||
); | ||
}; |
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
Oops, something went wrong.