如何在Abjad中编写Dead Notes?解决\xNote解析报错问题
关于Abjad中实现Dead Notes的报错问题及解决方法
问题描述
我参考LilyPond的Dead Notes(消音音符)示例编写了Abjad脚本,预期让特定音符显示为叉形符头,但运行时抛出异常:
import abjad notes = r"c''4 b \xNote { e f } c b < g \xNote c f > b" voice_1 = abjad.Voice(notes) staff = abjad.Staff([voice_1]) abjad.show(staff)
报错信息:
Exception: LilyPondParser can not emulate music function: \xNote.
报错原因
- Abjad的LilyPond解析器仅支持有限的LilyPond语法子集,无法直接解析LilyPond原生的自定义音乐函数,
\xNote属于LilyPond原生函数,不在Abjad解析器的支持范围内。 - Abjad并没有内置将
\xNote映射为自身API的逻辑,因此直接在字符串乐谱中使用该函数会触发解析失败。
正确实现方法
Abjad中实现Dead Notes需要通过自定义符头样式来完成,推荐直接操作音符对象而非依赖字符串解析,具体示例如下:
基础实现代码
import abjad # 创建所有音符/和弦对象 notes = [ abjad.Note("c''4"), abjad.Note("b4"), abjad.Note("e4"), abjad.Note("f4"), abjad.Note("c''4"), abjad.Note("b4"), abjad.Chord("<g c f>4"), abjad.Note("b4"), ] # 为指定音符设置叉形符头 abjad.setting(notes[2]).note_head.style = "cross" # 第3个音符e abjad.setting(notes[3]).note_head.style = "cross" # 第4个音符f # 处理和弦中的单个音符(这里给和弦里的c设置叉形符头) target_chord = notes[6] for note_head in target_chord.note_heads: if note_head.written_pitch == abjad.NamedPitch("c"): abjad.setting(note_head).style = "cross" # 构建谱表并展示 voice = abjad.Voice(notes) staff = abjad.Staff([voice]) abjad.show(staff)
批量/单个音符的快捷方式
如果只需给单个音符设置叉形符头,也可以用attach方法:
single_note = abjad.Note("e4") abjad.attach(abjad.NoteHeadStyle("cross"), single_note)
内容的提问来源于stack exchange,提问作者canto123
相关产品推荐
相关产品推荐

