bell notificationshomepageloginNewPostedit profiledmBox

Hoots : How to define a Lilypond function to return set of notes given a pitch? I'm working on a song with a repetitive rhythm that changes pitch. As this would normally involve a large amount of repeated typing, I would like to - freshhoot.com

10% popularity   0 Reactions

How to define a Lilypond function to return set of notes given a pitch?
I'm working on a song with a repetitive rhythm that changes pitch. As this would normally involve a large amount of repeated typing, I would like to define a Lily pond function to do this for me. I'm completely lost on how to change the note duration (I figured out the argument for a pitch, but have yet to figure out how to add the duration in the function body, considering that * seems to register as invalid syntax, and plain numbers are interpreted as part of the variable name), and I can only get it to return one note at a time. Repeating the note variable in the function body as shown below does nothing (this is expected, as Scheme seems to implicitly return only the last value in the function)

Is there any way to get a function like this to work? Alternativly, is there a way to multiply notes in the same way as measure-long rests are multiplied (R1*n)?

My attempts:

Trying to return multiple values (tried without commas first)

pattern_one = #(define-music-function
(parser location note)
(ly:music?)
note, note, note, note, note, note)

Trying to return list:

pattern_one = #(define-music-function
(parser location note)
(ly:music?)
(note, note, note, note, note, note))

Using Lilypond syntax (this worked, but transposed the entire group of notes up one octave per call to note, and still leaves the problem of duration):

pattern_one = #(define-music-function
(parser location note)
(ly:music?)
#{
#note #note #note #note #note #note #note
#})

What I want to happen:

Input = d, output = d8 d8 d8 d8 d8 d8 or (preferably) d4 d8 d8 d8 d8 d8 d8


Load Full (2)

Login to follow hoots

2 Comments

Sorted by latest first Latest Oldest Best

10% popularity   0 Reactions

Your last attempt is almost good, you just need to use $note instead of #note . What's the difference? # introduces a Scheme expression directly as a music expression bypassing any change or interpretation. Which in this case means that you have the identical (not just the same) expression 7 times in a row, and if you transpose the expression, this identical expression in it will get transposed 7 times as well. So you want a copy each time, and $note will achieve that.

On another note, you want to add a rhythm here. That means that note cannot be a music expression (which already would have a duration) but only a pitch. So you should replace the ly:music? predicate with ly:pitch? in order to have $note just be a single pitch.

And the final complication is that if you use

relative {
pattern_one c'
}

you don't want to have this turn into

relative {
c'8 c'4 c' c' c' c' c' c'
}

since the octaves will go through the roof, because the ' is applied to each consecutive note. You want any modifiers to apply only to the first occurrence. There is a trick macro for that purpose called make-relative. Putting all of this together, you get

pattern_one = #(define-music-function
(parser location note)
(ly:pitch?)
(make-relative (note) note
#{
$note 4 $note 8 $note 8 $note 8 $note 8 $note 8 $note 8
#}))

Now if you are taking a somewhat recent development version of LilyPond, rhythms can stand on their own so you only actually need a single $note and can save yourself the make-relative trouble. Also argument lists are simpler, so you can write

pattern_one = #(define-music-function (note) (ly:pitch?)
#{
$note 4 8 8 8 8 8 8
#})

Given the legibility and conciseness of that expression, the question is whether the music function is even worth writing in the first place. For more complex rhythms, possibly also involving articulations, it might. For this amount of complexity, it seems a bit of overkill. Also the function will require a lot more work to deal with chords while the written-out version will just work fine with them.


10% popularity   0 Reactions

Is this repetitive rhythm played all on one pitch every time? How about transpose? First define it:

riff = { d4 d8 d d d d d }

then

transpose d c' riff

or whatever pitch you want it at.


Back to top Use Dark theme