@aituber-onair/voice
Version:
Voice synthesis library for AITuber OnAir
42 lines (41 loc) • 1.25 kB
JavaScript
import { EmotionParser } from '../utils/emotionParser';
export const splitSentence = (text) => {
const splitMessages = text.split(/(?<=[。.!?\n])/g);
return splitMessages.filter((msg) => msg !== '');
};
export const textsToScreenplay = (texts) => {
const screenplays = [];
let prevExpression = 'neutral';
for (let i = 0; i < texts.length; i++) {
const text = texts[i];
const { emotion, cleanText } = EmotionParser.extractEmotion(text);
const tag = emotion || prevExpression;
let expression = prevExpression;
if (EmotionParser.isValidEmotion(tag)) {
expression = tag;
prevExpression = tag;
}
screenplays.push({
expression: expression,
talk: {
style: emotionToTalkStyle(expression),
message: cleanText,
},
});
}
return screenplays;
};
const emotionToTalkStyle = (emotion) => {
switch (emotion) {
case 'angry':
return 'angry';
case 'happy':
return 'happy';
case 'sad':
return 'sad';
case 'surprised':
return 'surprised';
default:
return 'talk';
}
};