lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

rhythm template function


From: Jay Anderson
Subject: rhythm template function
Date: Mon, 19 May 2008 21:44:17 -0700

Here's a snippet I've used a couple times. If there's interest I'll
add it to the LSR. (I'm currently getting an 'invalid security
certificate' when I go here
https://lsr.dsi.unimi.it/list.php?type=usr). This is very similar to
http://lsr.dsi.unimi.it/LSR/Item?id=302 except that it's easier to
define a new rhythm template and add rests, articulations, and other
stuff as well.

-----Jay

\version "2.11.45"

%Rhythm template stuff
#(define (create-note pitch duration)
  (make-music 'NoteEvent
     'duration duration
     'pitch pitch))

#(define (make-chord chord-pitches chordevent)
  (let* ((skip (car (ly:music-property chordevent 'elements)))
         (rest (cdr (ly:music-property chordevent 'elements)))
         (duration (ly:music-property skip 'duration)))
    (if (eq? (ly:music-property skip 'name) 'SkipEvent)
      (make-music 'EventChord
        'elements (append
                    (map (lambda (x) (create-note x duration)) chord-pitches)
                    rest))
      chordevent)))

#(define (make-rhythm pitches template)
  (cond ((null? template) '())
        ((ly:pitch? (car pitches))
          (cons
            (make-chord pitches (car template))
            (make-rhythm pitches (cdr template))))
        (else
          (cons
            (make-chord (car pitches) (car template))
            (make-rhythm (cadr pitches) (cdr template))))))

% Returns two pitches from the input:
%  ( <The original pitch(es)>
%    <The pitch(es) in octave '-1'> )
% This makes the function work in \relative sections
% To make it work outside of \relative only return p.
#(define (get-pitches mus)
  (let* ((p (map
              (lambda (x) (ly:music-property x 'pitch))
              (ly:music-property mus 'elements)))
         (first-p (car p))
         (pitch (ly:pitch-notename first-p))
         (alteration (ly:pitch-alteration first-p))
         (octave -1)
         (px (ly:make-pitch octave pitch alteration)))
    (list p (cons px (cdr p)))))

#(define (rhythm-template template)
  (define-music-function (parser location mus) (ly:music?)
    (make-music
      'SequentialMusic
      'elements (make-rhythm
                  (get-pitches mus)
                  (ly:music-property template 'elements)))))

%Example usage:
%These methods should only be used within a \relative section.
rhya = #(rhythm-template #{s8.-> s16 s8#} )
rhyb = #(rhythm-template #{s8[ r16 s16 s8]#} )
rhyc = #(rhythm-template #{s16 s~ s4#} )

\score
{
  \new Staff \relative c'
  {
    \time 6/8
    \rhya c \rhyb c |
    \rhya <c e> \rhyb <c f> |
    \rhyc <c e> \rhyc <c d> |
  }
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]