You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在LilyPond中定义可直接调用的自定义音符snarerim?

如何在LilyPond中定义snarerim军鼓边击音符

现有可行实现

以下代码可渲染出军鼓与军鼓边击效果:

\version "2.24.4"
\include "utils.ly" % 见附录
\drums {
    snare \withSlash snare
}

渲染效果:
军鼓与军鼓边击

期望实现方式

希望通过以下更直观的代码实现相同效果,且snarerim能在和弦(如<bd snarerim>)中组合使用:

\version "2.24.4"
\include "utils.ly" % 见附录
\drums {
    snare snarerim
}

解决方案

要定义可在和弦中使用的snarerim,只需在代码中添加如下定义:

% 定义snarerim:继承snare的位置,自动应用slash边击效果
snarerim = \once \override NoteHead.stencil = #slash-over-note snare

完整单文件示例

\version "2.24.4"

% 生成音符上方斜线的模板
slash-over-note = #(lambda (grob)
  (let* ((note (ly:note-head::print grob))
         (x-extent (ly:stencil-extent note X))
         (y-extent (ly:stencil-extent note Y))
         (scaling (magstep (ly:grob-property grob 'font-size 0.0)))
         (slash (ly:make-stencil (list 'draw-line 0.15 0 1.5 1.5 0) '(0 . 1.5) '(0 . 1.5)))
         (transformed (ly:stencil-translate
           (ly:stencil-scale (centered-stencil slash) scaling scaling)
           (cons (interval-center x-extent) (interval-center y-extent))))
         (combined (ly:stencil-add note transformed)))
    (ly:make-stencil (ly:stencil-expr combined) x-extent y-extent)))

% 定义snarerim:可单独使用或加入和弦
snarerim = \once \override NoteHead.stencil = #slash-over-note snare

\drums {
    % 单独使用示例
    snare snarerim
    % 和弦组合示例
    <bd snarerim>
}

该定义的核心是让snarerim作为snare的“带效果别名”,通过\once确保仅对当前音符生效,完美支持和弦内的组合使用,解决了原\withSlash无法在和弦中针对单个音符生效的问题。

附录:utils.ly代码

% utils.ly
% 来源:GNU LilyPond用户邮件列表2018-09
slash-over-note = #(lambda (grob)
  (let* ((note (ly:note-head::print grob))
         (x-extent (ly:stencil-extent note X))
         (y-extent (ly:stencil-extent note Y))
         (scaling (magstep (ly:grob-property grob 'font-size 0.0)))
         (slash (ly:make-stencil (list 'draw-line 0.15 0 1.5 1.5 0) '(0 . 1.5) '(0 . 1.5)))
         (transformed (ly:stencil-translate
           (ly:stencil-scale (centered-stencil slash) scaling scaling)
           (cons (interval-center x-extent) (interval-center y-extent))))
         (combined (ly:stencil-add note transformed)))
    (ly:make-stencil (ly:stencil-expr combined) x-extent y-extent)))

slashesOn = \override NoteHead.stencil = #slash-over-note
slashesOff = \revert NoteHead.stencil
withSlash = \once \slashesOn

内容的提问来源于stack exchange,提问作者Flo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 10:53:21