gnu-music-discuss
[Top][All Lists]
Advanced

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

Re: shift-music


From: Laurent Martelli
Subject: Re: shift-music
Date: 20 Feb 2001 18:25:37 +0100
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7

>>>>> "Laurent" == Laurent Martelli <address@hidden> writes:

>>>>> "Rune" == Rune Zedeler <address@hidden> writes:

  Rune> But this is not so neat.  Isn't it possible to define some
  Rune> short of shortcut for this, somehow?  Lilypond functions... :)

  Laurent> And it does not work as expected (at least not for me) for
  Laurent> keys other than C major. But I'm working on it.

That should work. However, I have to explicitly pass the key to
shift-music. Does anybody knows how to get it automatically ?

#(define (alteration-in-key note key)
  (cdr (assv note key)))

#(define (normalize-notename note)
  (modulo note 7))

#(define  (shift-pitch p delta key)
  (let* ((a (pitch-alteration p))
         (old-note (pitch-notename p))
         (raw-note (+ delta (pitch-notename p)))
         (new-note (normalize-notename raw-note)))
   (make-pitch 
    (+ (if (negative? raw-note) -1 0) (pitch-octave p) (quotient raw-note 7))
    new-note
    ( +(alteration-in-key new-note key)
       (- a (alteration-in-key old-note key))))
   ))

#(define (shift-music music delta key)
  (let* ((es (ly-get-mus-property music 'elements))
         (e (ly-get-mus-property music 'element))
         (p (ly-get-mus-property music 'pitch))
         (body (ly-get-mus-property music 'body))
         (alts (ly-get-mus-property music 'alternatives)))

    (if (pair? es)
        (ly-set-mus-property
         music 'elements
         (map (lambda (x) (shift-music x delta key)) es)))

    (if (music? alts)
        (ly-set-mus-property
         music 'alternatives
         (shift-music alts delta key)))

    (if (music? body)
        (ly-set-mus-property
         music 'body
         (shift-music body delta key)))

    (if (music? e)
        (ly-set-mus-property
         music 'element
         (shift-music e delta key)))

    (if (pitch? p)
        (begin
          (set! p (shift-pitch p delta key))
          (ly-set-mus-property music 'pitch p)))

    music))

#(define c-major '((0 . 0) (1 . 0) (2 . 0) (3 . 0) (4 . 0) (5 . 0) (6 . 0)))
#(define g-major '((0 . 0) (1 . 0) (2 . 0) (3 . 1) (4 . 0) (5 . 0) (6 . 0)))
#(define d-major '((0 . 1) (1 . 0) (2 . 0) (3 . 1) (4 . 0) (5 . 0) (6 . 0)))
#(define a-major '((0 . 1) (1 . 0) (2 . 0) (3 . 1) (4 . 1) (5 . 0) (6 . 0)))
#(define e-major '((0 . 1) (1 . 1) (2 . 0) (3 . 1) (4 . 1) (5 . 0) (6 . 0)))
#(define b-major '((0 . 1) (1 . 1) (2 . 0) (3 . 1) (4 . 1) (5 . 1) (6 . 0)))
#(define fis-major '((0 . 1) (1 . 1) (2 . 1) (3 . 1) (4 . 1) (5 . 1) (6 . 0)))
#(define cis-major '((0 . 1) (1 . 1) (2 . 1) (3 . 1) (4 . 1) (5 . 1) (6 . 1)))

#(define key g-major)

patterna = \notes \relative c' { c8 d16 e fis g a bes | c4 r | }
patternb = \notes \relative c'' { cis8 b16 a g fis e d | c4 r | }

\score {
  \context Staff {
    \property Staff.instrument = #(make-instr-name 1)
    \notes \relative c' {
      \time 2/4;
      \key g \major;
      \patternb
      \apply #(lambda (x) (shift-music x 1 key)) \patterna
      \apply #(lambda (x) (shift-music x -2 key)) \patternb
      \apply #(lambda (x) (shift-music x 3 key)) \patterna
      \apply #(lambda (x) (shift-music x -4 key)) \patternb
      \apply #(lambda (x) (shift-music x 5 key)) \patterna
    } 
  }
}


-- 
Laurent Martelli
address@hidden              http://www.linuxfan.com/~laurent




reply via email to

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