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

音声出力多言語対応 #638

Merged
merged 1 commit into from
Aug 30, 2024
Merged
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
31 changes: 27 additions & 4 deletions packages/web/src/hooks/useSpeach.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import { useState } from 'react';
import { useState, useEffect } from 'react';
import { fetchAuthSession } from 'aws-amplify/auth';
import { fromCognitoIdentityPool } from '@aws-sdk/credential-provider-cognito-identity';
import { CognitoIdentityClient } from '@aws-sdk/client-cognito-identity';
import { Polly, SynthesizeSpeechCommand } from '@aws-sdk/client-polly';
import { Polly, SynthesizeSpeechCommand, VoiceId } from '@aws-sdk/client-polly';

const useSpeach = () => {
// Engine=neural のものが指定可能
// https://docs.aws.amazon.com/ja_jp/polly/latest/dg/available-voices.html
const LanguageVoiceMapping: Record<string, VoiceId> = {
英語: 'Joanna',
日本語: 'Kazuha',
中国語: 'Zhiyu',
韓国語: 'Seoyeon',
フランス語: 'Lea',
スペイン語: 'Lucia',
ドイツ語: 'Vicki',
};

const useSpeach = (language: string) => {
const [loading, setLoading] = useState(false);
const [voiceId, setVoiceId] = useState<VoiceId>('Joanna');

useEffect(() => {
const tmpVoiceId = LanguageVoiceMapping[language];
if (tmpVoiceId) {
setVoiceId(tmpVoiceId);
} else {
console.error(`No voiceId found for language ${language}`);
}
}, [language]);

return {
loading,
Expand Down Expand Up @@ -39,7 +61,8 @@ const useSpeach = () => {
const command = new SynthesizeSpeechCommand({
Text: text,
OutputFormat: 'mp3',
VoiceId: 'Joanna', // TODO: 多言語対応
VoiceId: voiceId,
Engine: 'neural',
});

const response = await polly.send(command);
Expand Down
18 changes: 8 additions & 10 deletions packages/web/src/pages/TranslatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const TranslatePage: React.FC = () => {
const stopReason = getStopReason();
const [auto, setAuto] = useLocalStorageBoolean('Auto_Translate', true);
const [audio, setAudioInput] = useState(false); // 音声入力フラグ
const { synthesizeSpeach, loading: speachIsLoading } = useSpeach();
const { synthesizeSpeach, loading: speachIsLoading } = useSpeach(language);

useEffect(() => {
updateSystemContextByModel();
Expand Down Expand Up @@ -381,15 +381,13 @@ const TranslatePage: React.FC = () => {
</div>
)}
<div className="flex w-full justify-end">
{language === '英語' && (
<ButtonIcon onClick={startOrStopSpeach}>
{isSpeachPlaying ? (
<PiSpeakerSimpleHighFill />
) : (
<PiSpeakerSimpleHigh />
)}
</ButtonIcon>
)}
<ButtonIcon onClick={startOrStopSpeach}>
{isSpeachPlaying ? (
<PiSpeakerSimpleHighFill />
) : (
<PiSpeakerSimpleHigh />
)}
</ButtonIcon>
<ButtonCopy
text={translatedSentence}
interUseCasesKey="translatedSentence"></ButtonCopy>
Expand Down
Loading