frequency.value = note.frequency;

// 配置音量包络
const startTime = this.audioContext.currentTime + (index * chord.duration);
gainNode.gain.setValueAtTime(0, startTime);
gainNode.gain.linearRampToValueAtTime(note.volume, startTime + 0.1);
gainNode.gain.exponentialRampToValueAtTime(0.001, startTime + chord.duration);

oscillator.connect(gainNode);
gainNode.connect(this.audioContext.destination);

oscillator.start(startTime);
oscillator.stop(startTime + chord.duration);

oscillators.push(oscillator);
gainNodes.push(gainNode);
});
});

// 设置总时长后resolve
setTimeout(() => {
resolve();
}, duration * 1000);

} catch (error) {
console.error('背景音乐生成错误:', error);
resolve();
}
});
}

// 配置乐器音色
configureInstrument(oscillator, instrument) {
const instrumentConfigs = {
piano: { type: 'sine', detune: 0 },
strings: { type: 'sawtooth', detune: 5 },
guitar: { type: 'square', detune: -3 },
flute: { type: 'sine', detune: 2 },
drums: { type: 'triangle', detune: 0 }
};

const config = instrumentConfigs[instrument] || instrumentConfigs.piano;
oscillator.type = config.type;
if (oscillator.detune) {
oscillator.detune.value = config.detune;
}
}

// 生成和弦进行
generateChords(style, duration) {
const chordProgressions = {
verse: [
{ notes: [{frequency: 261.63, volume: 0.1}, {frequency: 329.63, volume: 0.08}, {frequency: 392.00, volume: 0.06}], duration: duration / 4 },
{ notes: [{frequency: 293.66, volume: 0.1}, {frequency: 349.23, volume: 0.08}, {frequency: 440.00, volume: 0.06}], duration: duration / 4 },
{ notes: [{frequency: 329.63, volume: 0.1}, {frequency: 392.00, volume: 0.08}, {frequency: 493.88, volume: 0.06}], duration: duration / 4 },
{ notes: [{frequency: 349.23, volume: 0.1}, {frequency: 440.00, volume: 0.08}, {frequency: 523.25, volume: 0.06}], duration: duration / 4 }
],
chorus: [
{ notes: [{frequency: 261.63, volume: 0.15}, {frequency: 329.63, volume: 0.12}, {frequency: 392.00, volume: 0.1}], duration: duration / 4 },
{ notes: [{frequency: 293.66, volume: 0.15}, {frequency: 349.23, volume: 0.12}, {frequency: 440.00, volume: 0.1}], duration: duration / 4 },
{ notes: [{frequency: 261.63, volume: 0.15}, {frequency: 329.63, volume: 0.12}, {frequency: 392.00, volume: 0.1}], duration: duration / 4 },
{ notes: [{frequency: 349.23, volume: 0.15}, {frequency: 440.00, volume: 0.12}, {frequency: 523.25, volume: 0.1}], duration: duration / 4 }
],
'pre-chorus': [
{ notes: [{frequency: 293.66, volume: 0.12}, {frequency: 349.23, volume: 0.1}, {frequency: 440.00, volume: 0.08}], duration: duration / 3 },
{ notes: [{frequency: 329.63, volume: 0.12}, {frequency: 392.00, volume: 0.1}, {frequency: 493.88, volume: 0.08}], duration: duration / 3 },
{ notes: [{frequency: 349.23, volume: 0.12}, {frequency: 440.00, volume: 0.1}, {frequency: 523.25, volume: 0.08}], duration: duration / 3 }
]
};

return chordProgressions[style] || chordProgressions.verse;
}

// 获取段落音乐风格
getMusicStyleForSection(sectionType) {
const styleMap = {
verse: 'verse',
chorus: 'chorus',
'pre-chorus': 'pre-chorus',
bridge: 'verse',
interlude: 'verse'
};
return styleMap[sectionType] || 'verse';
}

// 计算间奏时长
calculateInterludeDuration(section) {
// 解析间奏描述中的小节数
const text = section.lyrics.join(' ');
const match = text.match(/(\d+)\s*小节/);
if (match) {
const bars = parseInt(match[1]);
return bars * (60 / this.bpm) * 4; // 4拍每小节
}
return 8 * (60 / this.bpm); // 默认8小节
}

// 计算行时长
calculateLineDuration(line) {
const baseDuration = 2.0; // 基础时长2秒
const lengthFactor = line.length * 0.1; // 每字符0.1秒
const emotionFactor = 1 + (this.emotionProfile.happiness - 50) / 100; // 情感影响

return Math.max(1.0, baseDuration + lengthFactor) * emotionFactor;
}

// 计算总时长
calculateTotalDuration() {
let total = 0;
this.sections.forEach(section => {
if (section.type === 'interlude') {
total += this.calculateInterludeDuration(section);
} else {
section.lyrics.forEach(line => {
total += this.calculateLineDuration(line) + 0.2; // 加上行间间隔
});
}
});
return total;
}

// 更新节奏
updateTempo() {
// 更新所有基于BPM的计算
this.showMessage(`节奏已更新: ${this.bpm} BPM`);
}

// 更新情感
updateEmotion() {
this.showMessage('情感参数已更新');
}

// 初始化可视化
initVisualization() {
this.createWaveform();
this.createSpectrum();
this.createTimeline();
}

// 创建波形显示
createWaveform() {
const container = document.getElementById('waveform');
// 创建波形可视化
for (let i = 0; i < 200; i++) {
const bar = document.createElement('div');
bar.style.position = 'absolute';
bar.style.bottom = '0';
bar.style.left = `${(i / 200) * 100}%`;
bar.style.width = '1px';
bar.style.height = '0px';
bar.style.background = 'linear-gradient(to top, #667eea, #764ba2)';
bar.

Prev | Next
Pg.: 1 2 3 4 5 6 7 8 9 10 11 12 ... 56


Back to home | File page

Subscribe | Register | Login | N