Context
I'm new to lilypond and scheme, and for a score I need to repeat many times one single note (same pitch, same rythm). So I tried to implement a (loopnote)
function, which takes an integer (number of repeats) and a note as arguments. This function is designed to work within \relative
mode: when the given note has an octave modifier, it will keep it only for the first note, and reset it for all the others, in order to avoid out-of-control repeated octaves up.
Issue
But it does not work as expected:
- Note octaves are "often" demultiplied and go up very quickly, each time the second duplicated note is encountered;
- When I set an octave modifier for the relative mode e.g.
\relative c'
, things become worse: octaves go up even more quickly; - When I store
\loopnote 16 c'
in a variable and use it, it works "better" than when if I use the function directly (but not in all cases, there are still problems with\relative c'
).
When I use \displayMusic
, I don't see any difference between { c' c c c }
, \loopnote 4 c'
or \myvar
(where myvar = \loopnote 4 c'
).
What am I missing?
EDIT
When I wrote this question I was not aware of the command \repeat unfold
, which is doing exactly what I want... However, this does not explain why my own code is not working, and how to fix it...
Source code and example images
Here is the lilypond source code which gave me the two images below:
\version "2.19.82"
\language "english"
#(define dup (lambda (el n) (if (= n 0) '() (cons el (dup el (- n 1))))))
#(define greaterThanZero? (lambda (val) (and (integer? val) (> val 0))))
loopnote = #(define-music-function
(parser location n note)
(greaterThanZero? ly:music?)
(let (
(noteCopy (ly:music-deep-copy note))
(pitch (ly:music-property note 'pitch)))
(let (
(notename (ly:pitch-notename pitch))
(alteration (ly:pitch-alteration pitch)))
; Reset pitch octave information for every note but the first:
(set! (ly:music-property noteCopy 'pitch) (ly:make-pitch -1 notename alteration))
(make-music 'SequentialMusic 'elements (cons note (dup noteCopy (- n 1)))))))
% EXAMPLE 1:
myvar = \loopnote 4 c'
\relative c {
\myvar % Working well
\myvar % Working well
\loopnote 4 c % Problem
}
% EXAMPLE 2:
% myvar = \loopnote 4 c
% \relative c' {
% \myvar
% \myvar
% \myvar
% \loopnote 4 c
% \myvar
% \myvar
% \loopnote 4 c
% }