#!/usr/bin/env python3 """ LilyPond乐谱模板生成器 生成标准的LilyPond格式乐谱文件 """ import os import sys from pathlib import Path def create_lilypond_template(song_name, key="c", tempo=90, difficulty="standard"): """ 创建LilyPond模板 Args: song_name: 歌曲名称 key: 调性(默认c大调) tempo: 速度 difficulty: 难度级别("simple" 或 "standard") Returns: LilyPond代码字符串 """ template = f'''\\version "2.24.0" % {song_name} - LilyPond 格式 % 可以使用 LilyPond 软件编译生成专业五线谱 \\header {{ title = "{song_name}" composer = "Traditional" tagline = "" }} global = {{ \\key {key} \\major \\time 4/4 \\tempo 4 = {tempo} }} % 右手旋律(高音谱号) upper = \\relative c' {{ \\clef treble % TODO: 添加旋律 c4 c g g | a a g2 | f4 f e e | d d c2 | }} % 左手伴奏(低音谱号) ''' if difficulty == "simple": template += """lower = \\relative c { \\clef bass % 简化版:只弹根音 c4 c g g | g2 r2 | f4 f e e | e2 r2 | }""" else: template += """lower = \\relative c { \\clef bass % 标准版:完整和弦 4 | 2 r2 | 4 | 2 r2 | }""" template += """ \\score { \\new PianoStaff << \\new Staff = "upper" \\with { midiInstrument = "acoustic grand" } { \\global \\upper } \\new Staff = "lower" \\with { midiInstrument = "acoustic grand" } { \\global \\lower } >> \\layout { } \\midi { \\tempo 4 = tempo } }""" return template def save_lilypond_file(content, filename): """保存LilyPond文件""" with open(filename, "w", encoding="utf-8") as f: f.write(content) print(f"LilyPond文件已保存: {filename}") def main(): import argparse parser = argparse.ArgumentParser(description="LilyPond模板生成器") parser.add_argument("song_name", help="歌曲名称") parser.add_argument("--key", "-k", default="c", help="调性(默认c)") parser.add_argument("--tempo", "-t", type=int, default=90, help="速度(默认90)") parser.add_argument( "--difficulty", "-d", choices=["simple", "standard"], default="standard", help="难度级别", ) parser.add_argument("--output", "-o", default=None, help="输出文件名") args = parser.parse_args() if args.output is None: args.output = f"{args.song_name.replace(' ', '_')}.ly" try: content = create_lilypond_template( args.song_name, args.key, args.tempo, args.difficulty ) save_lilypond_file(content, args.output) except Exception as e: print(f"错误: {e}") sys.exit(1) if __name__ == "__main__": main()