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

如何从Lilypond乐谱的Scheme代码中提取MIDI速度?

在Scheme中提取LilyPond乐谱的MIDI速度参数

LilyPond里的Score是内部记录类型,打印显示的#只是它的简写标识,直接打印看不到细节,但可以通过官方提供的Scheme接口来访问它的MIDI设置。

具体操作方法

1. 直接获取Score的MIDI设置

用ly:score-midi-settings函数能直接提取Score关联的MIDI配置结构,示例代码:

; 获取当前上下文里的Score对象
(define my-score (ly:parser-lookup (ly:parser-main) 'Score))
; 提取MIDI设置
(define midi-settings (ly:score-midi-settings my-score))

2. 提取速度参数

MIDI设置里的速度参数可以通过ly:midi-settings-tempo直接获取:

(define midi-tempo (ly:midi-settings-tempo midi-settings))
; 打印查看数值
(display midi-tempo)

3. 处理自定义速度场景

如果原乐谱里用了\tempo命令或者自定义速度曲线,还可以遍历Score的音乐表达式提取细节:

; 获取Score的顶层音乐表达式
(define top-music (ly:score-top-music my-score))

; 递归遍历找tempo属性
(define (find-tempo music)
  (cond
    ((ly:music-property music 'tempo) => (lambda (t) t))
    ((ly:music-property music 'elements)
     => (lambda (els)
          (let loop ((els els))
            (if (null? els)
                #f
                (or (find-tempo (car els)) (loop (cdr els)))))))
    (else #f)))

(define custom-tempo (find-tempo top-music))

4. 处理多Score的书籍场景

如果是从包含多个乐谱的书籍里提取,先遍历Book的子Score即可:

(define my-book (ly:parser-lookup (ly:parser-main) 'Book))
(define scores (ly:book-scores my-book))

; 逐个提取每个Score的MIDI速度
(for-each (lambda (score)
            (define tempo (ly:midi-settings-tempo (ly:score-midi-settings score)))
            (display (format "当前乐谱速度: ~a\n" tempo)))
          scores)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 03:09:55