#!/usr/bin/env python3 """ 创建HTML可视化页面 """ import os import json import music21 from music21 import converter def create_html_visualizer(midi_path, output_dir="output"): """创建HTML可视化页面""" print("正在创建HTML可视化页面...") # 加载MIDI文件 score = music21.converter.parse(midi_path) # 提取音符数据 voice_data = [] voice_names = ['Soprano', 'Alto', 'Tenor', 'Bass'] voice_colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4'] for i, part in enumerate(score.parts): if i >= 4: break part_notes = [] for element in part.flat.notes: if hasattr(element, 'pitch'): part_notes.append({ 'pitch': element.pitch.midi, 'note_name': element.pitch.nameWithOctave, 'start': float(element.offset), 'duration': float(element.quarterLength), 'velocity': element.volume.velocity if hasattr(element.volume, 'velocity') else 80 }) elif hasattr(element, 'pitches'): for p in element.pitches: part_notes.append({ 'pitch': p.midi, 'note_name': p.nameWithOctave, 'start': float(element.offset), 'duration': float(element.quarterLength), 'velocity': element.volume.velocity if hasattr(element.volume, 'velocity') else 80 }) voice_data.append({ 'name': voice_names[i], 'color': voice_colors[i], 'notes': part_notes }) # 创建HTML内容 html_content = f"""