Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

  1. 系统架构概述

该系统由以下几个主要模块组成:

  • MIDI文件处理模块:负责加载和解析MIDI文件
  • 音乐数据预处理模块:将音符序列转换为模型可处理的格式
  • 混合神经网络模型:结合LSTM和CNN的音乐生成模型
  • 音乐生成器:使用训练好的模型生成新音乐
  • 实用工具:包括保存生成的音乐到MIDI文件等功能
  1. 核心功能模块分析

2.1 MIDI文件处理 (load_midi_filesEnhancedMidiPreprocessor)

这部分代码负责加载和解析MIDI文件:

  • 支持多种MIDI文件格式(.mid, .midi, .MID, .MIDI)
  • 使用music21库进行专业级的音乐解析
  • 实现了音符量化、和弦处理、调性分析等专业音乐处理功能
  • 包含数据增强功能,如随机转调和节奏变化
def parse_midi(self, filepath):
    """解析MIDI文件,提取音符序列和音乐特征"""
    try:
        score = music21.converter.parse(filepath)
        notes = []
        key = score.analyze('key') if len(score.flat.notes) > 10 else music21.key.Key('C')
        tempo = score.metronomeMarkBoundaries()[0][2].number if score.metronomeMarkBoundaries() else 120 
        
        for element in score.flatten().notesAndRests:
            # 处理音符和和弦 
            if element.isChord:
                chord_type = element.chordKind if hasattr(element, 'chordKind') else 'major'
                root = element.root().midi 
                pitches = f"chord_{root}_{chord_type}"
            else:
                pitches = str(element.pitch.midi)
            
            # 量化特征提取 
            duration = self.quantize_duration(element.duration.quarterLength)
            velocity = element.volume.velocity if hasattr(element, 'volume') else 90 
            offset = round(float(element.offset), 2)
            beat = int(element.beat) if hasattr(element, 'beat') else 1 
            
            # 组合所有特征 
            encoded = (f"{pitches}_d{duration}_v{velocity}_"
                     f"b{beat}_k{key.tonic.name}_t{tempo}_o{offset}")
            notes.append(encoded)
        
        return notes if len(notes) >= self.min_notes else None 

2.2 神经网络模型 (EnhancedHybridMusicModel)

模型架构特点:

  • 混合架构:结合了LSTM和CNN的优势
  • 双向LSTM层捕捉长时音乐依赖关系
  • 卷积层提取局部音乐模式
  • 分离处理音高和节奏特征,最后进行特征融合
  • 包含Layer Normalization和Dropout等正则化技术
def _build_enhanced_model(self):
    """构建混合音乐生成模型"""
    # 音高输入流 
    pitch_input = Input(shape=(self.seq_length,), name='pitch_input')
    x = Embedding(self.vocab_size, 256)(pitch_input)
    x = Dropout(0.2)(x)
    
    # 双向LSTM层 
    x = Bidirectional(LSTM(512, return_sequences=True))(x)
    x = LayerNormalization()(x)
    x = Dropout(0.3)(x)
    
    # 卷积层捕捉局部模式 
    x = Conv1D(256, 5, activation='relu', padding='same')(x)
    x = LayerNormalization()(x)
    
    # 节奏输入流 
    rhythm_input = Input(shape=(self.seq_length, 2), name='rhythm_input')
    y = Conv1D(128, 3, activation='relu', padding='same')(rhythm_input)
    y = Bidirectional(LSTM(128, return_sequences=True))(y)
    y = LayerNormalization()(y)
    y = LSTM(64)(y)
    
    # 特征融合 
    combined = Concatenate()([x, y])
    combined = Dense(512, activation='relu')(combined)
    combined = Dropout(0.4)(combined)
    
    # 输出层 
    pitch_output = Dense(self.vocab_size, activation='softmax', name='pitch_output')(combined)
    
    return Model(inputs=[pitch_input, rhythm_input], outputs=pitch_output)

2.3 音乐生成器 (EnhancedMusicGenerator)

音乐生成特点:

  • 使用温度采样控制生成多样性
  • 动态调整温度参数,生成过程中逐渐减少随机性
  • 包含音乐性验证和修正机制
  • 支持种子序列输入或从训练数据中随机选择种子
def generate(self, seed_sequence=None, length=500, temperature=0.7):
    """生成音乐序列"""
    if seed_sequence is None:
        # 从训练数据中选择有音乐性的种子 
        if hasattr(self.preprocessor, 'training_sequences'):
            valid_seeds = [seq for seq in self.preprocessor.training_sequences 
                         if len(set(seq)) > 5]
            if valid_seeds:
                seed_seq = random.choice(valid_seeds)
                seed_sequence = [self.preprocessor.note_to_int[note] 
                               for note in seed_seq[-self.seq_length:]]
    
    # 动态温度调整 
    current_temp = temperature * 1.5  # 初始温度更高 
    for i in range(length):
        if i < 50:
            current_temp = max(temperature * (1.5 - i/100), temperature)
        else:
            if i % 50 == 0:
                current_temp *= self.diversity_factors['temperature_decay']
        
        # 模型预测下一个音符 
        current_seq = np.array([generated[-self.seq_length:]])
        rhythm_features = self._extract_rhythm_features(current_seq)
        predictions = self.model.predict([current_seq, rhythm_features], verbose=0)
        
        # 温度采样 
        next_pitch = self._temperature_sampling(predictions[0], current_temp)
        
        # 音乐性修正 
        if self._is_musically_invalid(next_pitch, generated):
            next_pitch = self._correct_pitch(generated)
        
        generated.append(next_pitch)
    
    return [self.preprocessor.int_to_note[i] for i in generated]
  1. 系统工作流程

I. 数据加载与预处理:

  • 从指定目录加载MIDI文件
  • 解析为音符序列并提取音乐特征
  • 创建音符到整数的映射词汇表

II. 训练序列准备:

  • 将连续的音符序列转换为输入-输出对
  • 应用数据增强技术增加数据多样性

III. 模型训练:

  • 使用准备好的数据训练混合神经网络
  • 应用EarlyStopping、ModelCheckpoint等回调函数

IV. 音乐生成:

  • 使用训练好的模型生成新的音符序列
  • 应用温度采样和音乐性约束

V. 结果保存:

  • 将生成的音符序列保存为MIDI文件
  1. 创新点与优势

I. 混合模型架构:结合了LSTM和CNN的优势,既能捕捉长时依赖关系,又能识别局部音乐模式。

II. 专业音乐特征处理:

  • 完整的音乐理论支持(调性、和弦、节奏等)
  • 音符量化处理
  • 分离处理音高和节奏特征

III. 高级生成控制:

  • 动态温度调整
  • 音乐性验证和修正
  • 基于音乐理论的候选音高选择

IV. 鲁棒性设计:

  • 全面的错误处理
  • 备用数据生成方案
  • 自动词汇表更新机制
  1. 潜在改进方向

I. 模型架构:

  • 可以尝试加入Transformer或Attention机制
  • 增加更多音乐特定特征的处理头

II. 训练策略:

  • 实现课程学习,从简单到复杂的音乐模式
  • 加入对抗训练或强化学习反馈

III. 音乐性控制:

  • 增加更多音乐规则约束
  • 实现风格控制参数
  • 支持用户指定的和弦进行或节奏模式

IV. 部署优化:

  • 模型量化减小体积
  • 实时生成能力
  • 交互式生成界面

这个程序实现了一个专业级的音乐生成系统,结合了深度学习技术和音乐理论知识,能够生成具有较高音乐性的作品。系统的模块化设计使其易于扩展和改进。

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages