// 配置发音人
this.configureAdvancedVoice(utterance, voiceType, emotionScores);

// 应用高级情感参数
this.applyAdvancedEmotion(utterance, emotionScores);

utterance.onend = () => resolve();
utterance.onerror = () => resolve();

window.speechSynthesis.speak(utterance);
} else {
// 使用Web Audio API生成更高级的合成语音
this.generateAdvancedSyntheticSpeech(text, voiceType, emotionScores).then(resolve);
}
} catch (error) {
console.error('语音合成错误:', error);
resolve();
}
});
}

// 配置高级发音人
configureAdvancedVoice(utterance, voiceType, emotionScores) {
const voiceConfigs = {
female1: {
basePitch: 1.2, baseRate: 0.9, baseVolume: 1.0,
pitchRange: 0.3, rateRange: 0.4, volumeRange: 0.2
},
female2: {
basePitch: 1.3, baseRate: 1.1, baseVolume: 0.9,
pitchRange: 0.4, rateRange: 0.5, volumeRange: 0.3
},
male1: {
basePitch: 0.8, baseRate: 0.8, baseVolume: 1.0,
pitchRange: 0.2, rateRange: 0.3, volumeRange: 0.2
},
male2: {
basePitch: 0.7, baseRate: 1.0, baseVolume: 1.1,
pitchRange: 0.3, rateRange: 0.4, volumeRange: 0.3
},
child: {
basePitch: 1.5, baseRate: 1.2, baseVolume: 0.8,
pitchRange: 0.5, rateRange: 0.6, volumeRange: 0.4
}
};

const config = voiceConfigs[voiceType] || voiceConfigs.female1;

// 基于情感调整基础参数
const happiness = emotionScores.happiness / 100;
const sadness = emotionScores.sadness / 100;
const anger = emotionScores.anger / 100;
const calmness = emotionScores.calmness / 100;

let pitch = config.basePitch;
let rate = config.baseRate;
let volume = config.baseVolume;

// 情感影响计算
pitch += (happiness - 0.5) * config.pitchRange;
rate += (anger - 0.2) * config.rateRange;
volume += (happiness - 0.3) * config.volumeRange;

utterance.pitch = Math.max(0.5, Math.min(2.0, pitch));
utterance.rate = Math.max(0.3, Math.min(2.0, rate));
utterance.volume = Math.max(0.3, Math.min(1.0, volume));

// 选择系统语音
if (this.availableVoices) {
const chineseVoices = this.availableVoices.filter(voice =>
voice.lang.includes('zh') || voice.lang.includes('CN')
);
if (chineseVoices.length > 0) {
// 根据声音类型选择最合适的语音
const preferredVoices = chineseVoices.filter(voice =>
voice.name.toLowerCase().includes(voiceType.includes('female') ? 'female' : 'male')
);
utterance.voice = preferredVoices[0] || chineseVoices[0];
}
}
}

// 应用高级情感
applyAdvancedEmotion(utterance, emotionScores) {
// 这里可以添加更复杂的情感算法
// 比如基于情感的语调变化模式
}

// 生成高级合成语音
async generateAdvancedSyntheticSpeech(text, voiceType, emotionScores) {
return new Promise((resolve) => {
const duration = text.length * 0.3;

// 使用多个振荡器创建更真实的声音
this.playAdvancedEmotionalTone(duration, voiceType, emotionScores).then(resolve);
});
}

// 播放高级情感音调
async playAdvancedEmotionalTone(duration, voiceType, emotionScores) {
const baseFreq = this.getVoiceBaseFrequency(voiceType);
const emotionVariation = this.calculateAdvancedEmotionVariation(emotionScores);

// 创建多个振荡器模拟真实语音
const oscillators = [];
const formants = [800, 1200, 2400]; // 语音共振峰

formants.forEach((formant, index) => {
const oscillator = this.audioContext.createOscillator();
const gainNode = this.audioContext.createGain();
const filter = this.audioContext.createBiquadFilter();

oscillator.type = index === 0 ? 'sawtooth' : 'sine';
oscillator.frequency.setValueAtTime(baseFreq + formant + emotionVariation, this.audioContext.currentTime);

filter.type = 'bandpass';
filter.frequency.value = formant;
filter.Q.value = 10;

// 动态增益控制模拟语音包络
const currentTime = this.audioContext.currentTime;
gainNode.gain.setValueAtTime(0, currentTime);
gainNode.gain.linearRampToValueAtTime(0.1 * (1 - index * 0.3), currentTime + 0.1);
gainNode.gain.exponentialRampToValueAtTime(0.001, currentTime + duration);

oscillator.connect(filter);
filter.connect(gainNode);
gainNode.connect(this.audioNodes.get('compressor'));

oscillator.start();
oscillator.stop(currentTime + duration);
oscillators.push(oscillator);
});

return new Promise(resolve => {
setTimeout(() => {
oscillators.forEach(osc => osc.disconnect());
resolve();
}, duration * 1000);
});
}

// 计算高级情感变化
calculateAdvancedEmotionVariation(emotionScores) {
const happiness = emotionScores.happiness / 100;
const sadness = emotionScores.sadness / 100;
const anger = emotionScores.anger / 100;

let variation = 0;
variation += happiness * 80; // 喜悦显著增加变化
variation -= sadness * 50; // 悲伤减少变化
variation += anger * 60; // 愤怒增加变化但不稳定

// 添加随机性模拟真实语音
variation += (Math.random() - 0.5) * 40;

return variation;
}

// 添加背景音乐
async addBackgroundMusic(text, sectionType) {
const duration = this.calculateLineDuration(text);

Prev | Next
Pg.: 1 ... 11 12 13 14 15 16 17 18 19 20 21 ... 56


Back to home | File page

Subscribe | Register | Login | N