lilypond-user
[Top][All Lists]
Advanced

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

Modified enharmonic.ly (from snippets)


From: Peter Gentry
Subject: Modified enharmonic.ly (from snippets)
Date: Tue, 2 Dec 2014 17:38:43 -0000

I have suceeded in producing a desired result although I don't think it is of general use. The code could be more elegant but it works.

\version "2.18.2"

% OS Vista and Frescobaldi

#(define s 0)
#(define (naturalize-pitch p tonic)
(define sharp-list '((0 . 0) (1 . 0) (2 . 1) (3 . 0) (4 . 0) (5 . 0) (6 . 1)))
(define flat-list    '((0 . 1) (1 . 0) (2 . 0) (3 . 1) (4 . 0) (5 . 0) (6 . 0)))
(let ((o (ly:pitch-octave p))
(a (* 4 (ly:pitch-alteration p)))
;; alteration, a, in quarter tone steps,
;; for historical reasons
;; get currrent pitch
(n (ly:pitch-notename p)))
(cond
;; B# to C and E# toF
((and (> a 1) (or (eq? n 6) (eq? n 2)))
(set! a (- a 2))
(set! n (+ n 1)))
;; Cb to B and Fb to E
((and (< a -1) (or (eq? n 0) (eq? n 3)))
(set! a (+ a 2))
(set! n (- n 1))))

(cond
;; if a > 2 then set a=a-4 n=n+1
((> a 2) (set! a (- a 4)) (set! n (+ n 1)))
;; if a < -2 then set a=a+4 n=n-1
((< a -2) (set! a (+ a 4)) (set! n (- n 1))))

(if (< n 0) (begin (set! o (- o 1)) (set! n (+ n 7))))
;; if n > 6 then set o=o+1 n=n-7
(if (> n 6) (begin (set! o (+ o 1)) (set! n (- n 7))))

 (cond
  ((eq? tonic  1 )
;   determine key contains flats s=1, naturals s=0 , or sharps s=3 ")  
  (if (and (eq? n 0) (eq? a 2) ) (set! s 3))
  (if (and (eq? n 1) (eq? a 0) ) (set! s 3))
  (if (and (eq? n 2) (eq? a 0) ) (set! s 3))
  (if (and (eq? n 3) (eq? a 2) ) (set! s 3))
  (if (and (eq? n 4) (eq? a 0) ) (set! s 3))
  (if (and (eq? n 5) (eq? a 0) ) (set! s 3))
  (if (and (eq? n 6) (eq? a 0) ) (set! s 3))
  (if (and (eq? n 0) (eq? a 0) ) (set! s 2))
  (if (and (eq? n 0) (eq? a -2) ) (set! s 1))
  (if (and (eq? n 1) (eq? a -2) ) (set! s 1))
  (if (and (eq? n 2) (eq? a -2) ) (set! s 1))
  (if (and (eq? n 3) (eq? a 0) ) (set! s 1))
  (if (and (eq? n 4) (eq? a -2) ) (set! s 1))
  (if (and (eq? n 5) (eq? a -2) ) (set! s 1))
  (if (and (eq? n 6) (eq? a -2) ) (set! s 1))
   (set! tonic 0)))

(cond
 ((eq? tonic 0 )
; ntonic not detected skip to naturalize the pitch

(cond
 ( (eq? s 3)
; key contains sharps use list of flat modifiers
 (if (and (eq?  (cdr (assoc n flat-list)) 1)   (< a  0)  )  (set! a (* a -1) ))
 (if (and (eq?  (cdr (assoc n flat-list)) 0)   (< a  0)  ) (begin (set! n (- n 1))  (set! a (* a -1) )))
 ))

(cond
 ((eq? s 1)
; key contains flats use list of sharp modifiers
 (if (and (eq? (cdr(assoc n sharp-list)) 1)   (> a  0)  )  (set! a (* a -1) ))
 (if (and (eq? (cdr(assoc n sharp-list)) 0)   (> a  0)  ) (begin (set! n (+ n 1))  (set! a (* a -1) )))
 ))

))

(ly:make-pitch o n (/ a 4)))
)


#(define (naturalize music)
(let ((es (ly:music-property music 'elements))
       (e   (ly:music-property music 'element))
       (p   (ly:music-property music 'pitch))
       (t   (ly:music-property music 'tonic)))
(if (pair? es)
(ly:music-set-property!
music 'elements
(map (lambda (x) (naturalize x)) es)))

(if (ly:music? e)
(ly:music-set-property!
music 'element
(naturalize e)))

(if (ly:pitch? p)
(begin
(set! p (naturalize-pitch p 0))
(ly:music-set-property! music 'pitch p)))

(if (ly:pitch? t)
(begin
(set! t (naturalize-pitch t 1))
(ly:music-set-property! music 'tonic t)))

music)
)


naturalizeMusic =
#(define-music-function (parser location m)
(ly:music?)
(naturalize m))

\version "2.18.2"
% Bach Minuet from Partita No1
#(set-global-staff-size 23)
  \header {  title =   "Minuet from Partita No 1"
                 composer =  "J S Bach"   }
\include "./ranges/enharmonic.ly"
\score {
\include "Bach Part Notes Test.ly"}
{\naturalizeMusic
\include "Bach Part Notes Test.ly" }



regards
Peter Gentry


reply via email to

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