Skip to content

Commit

Permalink
feat: added emotion to XML audio tag
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepat0 committed Oct 8, 2024
1 parent 80e4889 commit c188673
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 86 deletions.
3 changes: 2 additions & 1 deletion src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const Avatar: React.FC<Props> = ({
const { t } = useTranslation();
const [isClient, setIsClient] = useState(false);

const { setMeshRef, clearVisemes } = useViseme();
const { setMeshRef, clearVisemes, setEmotion } = useViseme();

useEffect(() => {
setIsClient(true);
Expand Down Expand Up @@ -145,6 +145,7 @@ const Avatar: React.FC<Props> = ({
setMeshRef={setMeshRef}
isZoomed={isZoomed}
chatEmission={chatProps?.dialogState?.emission}
setEmotion={setEmotion}
/>
</ErrorBoundary>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface Props {
chatEmission: any;
setMeshRef: any;
clearVisemes: () => void;
setEmotion: (emotion: string) => void;
}

interface BaseAction {
Expand Down Expand Up @@ -59,6 +60,7 @@ export const AvatarView: React.FC<Props & { halfBody: boolean }> = ({
speaking,
halfBody,
isZoomed,
setEmotion,
}) => {
const [currentBaseAction, setCurrentBaseAction] = useState({
action: animation || 'Idle1',
Expand All @@ -76,7 +78,7 @@ export const AvatarView: React.FC<Props & { halfBody: boolean }> = ({
const [timeScale, setTimeScale] = useState(0.8);

// Set the morph target influences for the given emotions
const setEmotion = useCallback((action: string) => {
const setEmotionMorphTargetInfluences = useCallback((action: string) => {
const emotionMap: Record<string, Record<string, number>> = {
Gioia: { Gioria: 1 },
Rabbia: { Rabbia: 1 },
Expand All @@ -85,6 +87,10 @@ export const AvatarView: React.FC<Props & { halfBody: boolean }> = ({
Timore: { Timore: 1 },
};

//remove the last character from the action
const newEmotion = action.slice(0, -1);
setEmotion(newEmotion);

const defaultEmotions = Object.keys(emotionMap).reduce((acc, key) => {
acc[key] = 0;
return acc;
Expand All @@ -101,7 +107,7 @@ export const AvatarView: React.FC<Props & { halfBody: boolean }> = ({
}, []);

const onBaseActionChange = useCallback((action: string) => {
setEmotion(action);
setEmotionMorphTargetInfluences(action);
setCurrentBaseAction({
action,
weight: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default function HalfBodyAvatar({
);

// Update morph target influences
useFrame((_, delta) => {
useFrame((_) => {
if (avatarMeshRef.current && avatarMeshRef.current.morphTargetDictionary) {
updateMorphTargetInfluences();
}
Expand Down
3 changes: 3 additions & 0 deletions src/components/Avatar/AvatarView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface Props {
chatEmission?: any;
setMeshRef?: any;
clearVisemes: () => void;
setEmotion: (emotion: string) => void;
}

const defaultStyles = {
Expand Down Expand Up @@ -89,6 +90,7 @@ export default function ContainerAvatarView({
chatEmission,
setMeshRef,
clearVisemes,
setEmotion,
}: Props) {
return (
<Canvas
Expand All @@ -114,6 +116,7 @@ export default function ContainerAvatarView({
chatEmission={chatEmission}
setMeshRef={setMeshRef}
clearVisemes={clearVisemes}
setEmotion={setEmotion}
/>
</Suspense>
</Canvas>
Expand Down
16 changes: 11 additions & 5 deletions src/components/MemoriWidget/MemoriWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,13 @@ const MemoriWidget = ({
);
const [hideEmissions, setHideEmissions] = useState(false);

const { addVisemeToQueue, processVisemeQueue, clearVisemes } = useViseme();
const {
addVisemeToQueue,
processVisemeQueue,
clearVisemes,
emotion,
getAzureStyleForEmotion,
} = useViseme();

useEffect(() => {
setIsPlayingAudio(!!speechSynthesizer);
Expand Down Expand Up @@ -1962,13 +1968,13 @@ const MemoriWidget = ({
});
};

speechSynthesizer.speakSsmlAsync(
`<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" xml:lang="${getCultureCodeByLanguage(
console.log(getAzureStyleForEmotion(emotion))
speechSynthesizer.speakSsmlAsync(`<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" xml:lang="${getCultureCodeByLanguage(
userLang
)}"><voice name="${getTTSVoice(userLang)}"><s>${replaceTextWithPhonemes(
)}"><voice name="${getTTSVoice(userLang)}"> <mstts:express-as style="${getAzureStyleForEmotion(emotion)}"><s>${replaceTextWithPhonemes(
escapeHTML(stripMarkdown(stripEmojis(stripOutputTags(text)))),
userLang.toLowerCase()
)}</s></voice></speak>`,
)}</s></mstts:express-as></voice></speak>`,
result => {
if (result) {
setIsPlayingAudio(true);
Expand Down
Loading

0 comments on commit c188673

Please sign in to comment.