lilypond-user
[Top][All Lists]
Advanced

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

Re: Enharmonic Spilling


From: Pierre Perol-Schneider
Subject: Re: Enharmonic Spilling
Date: Tue, 16 Dec 2014 09:47:01 +0100

Thank you Peter.
Cheers,
Pierre

2014-12-15 21:27 GMT+01:00 Peter Gentry <address@hidden>:
Find below is the corrected version of the modified snippet.

\version "2.18.2"
%{ P Gentry 15 December 2014 08:23
;; Modified version of the LilyPond snippet enharmonic.ly (probably better as enharmonic.ily)
;; This script will enharmonically spell a music object
;; Semi-tone intervals are dealt in accordance with the Key signature
;; If the Key is C Major the first accidental encountered is chosen as the preferred accidental
;; For other keys the preferred accidental is taken from the key signature
;; It may not be elegant but it seems to work
%}

#(define s 0)
#(define k 0)
#(define nbuf 0)
#(define abuf 0)
#(define keysig 0)

 #(define (naturalize-pitch p tonic)
 (define sharpkey-list '((0 . 0) (1 . 1) (2 . 1) (3 . 0) (4 . 1) (5 . 1) (6 . 1)))  ;key signatures  D, E, G, A, B contain sharps
 (define flatkey-list  '((0 . 0) (1 . 0) (2 . 0) (3 . 1) (4 . 0) (5 . 0) (6 . 0)))  ;key signature F contains a  flat
 (define sharp-list    '((0 . 0) (1 . 0) (2 . 1) (3 . 0) (4 . 0) (5 . 0) (6 . 1)))  ; notes E# and B# may be changed to F and C
dependant on key
 (define flat-list     '((0 . 1) (1 . 0) (2 . 0) (3 . 1) (4 . 0) (5 . 0) (6 . 0)))  ; notes Cb and Fb may be changed to B and E
dependant on key

 (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)))

;if keychange event  determine if key contains flats s=1, naturals s=2 , or sharps s=3 ")
(cond ((eq? tonic  1 )
  (begin
   (set! keysig 0) ;C natural key contains no accidentals
   (set! k 0)
  (if (or (and (eq? a 0) (eq? (cdr(assoc n sharpkey-list)) 1))  (> a 0)) (set! keysig 3)) ; key contains sharps
  (if (or (and (eq? a 0) (eq? (cdr(assoc n flatkey-list)) 1)) (< a 0))   (set! keysig 6))  ;key contains flats
  (if (and (eq? n 0) (eq? a 2) )  (set! keysig 1))  ;key C#
  (if (and (eq? n 3) (eq? a 2) )  (set! keysig 2))  ;key F#
  (if (and (eq? n 0) (eq? a -2) )  (set! keysig 4)) ;key Cb
  (if (and (eq? n 4) (eq? a -2) )  (set! keysig 5)) ;key Gb

;; if not C major set preferred accidental
  (if  (and (< keysig 4)  (> keysig 0)  (eq? k 0))  (set! k  2) )    ;; sharp key
  (if  (and (> keysig 3)   (eq? k 0))   (set! k -2) )                ;;  flat key
  (set! tonic 0)   ))
)

(set! nbuf n)
(set! abuf a)

; if no keychange event  naturalize the pitch

; first remove double accidentals

;deal with double sharps
(cond ((and (eq? tonic 0) (> a 2)) (begin  (set! n (+ n 1))  (set! a (- a 4)) )))
(cond ((> n 6)  (begin  (set! n (- n 7))  (set! o(+ o 1))  )) )
(cond ((and (eq? (cdr(assoc nbuf sharp-list )) 1) (eq? abuf 4))  (begin (set! a (+ a 2)) )))

;deal with double flats
(cond ((and (eq? tonic 0) (< a -2))
      (begin (set! n (- n 1)) (set! a(+ a 4)) )))
(cond ((< n 0)
      (begin (set! n(+ n 7)) (set! o(- o 1)) )))
(cond ((and (eq? (cdr(assoc nbuf flat-list )) 1) (eq? abuf -4))
      (begin (set! a (- a 2)) )))

;; if key C Major save first accidental as preferred
(if  (and (eq? tonic 0) (eq? keysig 0) (eq? k 0) (not (eq? a 0)))   (set! k a) )

;; enharmonic spell accidentals
;  (if (and (eq? tonic 0) (or ( eq? keysig 5) (eq? keysig 0)) (eq? n 3) (eq? a -2))
;      (begin (set! a 0) (set! n (- n 1))))
; (if (and (eq? tonic 0) (or (eq? keysig 6)   (eq? keysig 0)) (or (eq? n 0) (eq? n 3)) (eq? a -2))
;      (begin (set! a 0) (set! n (- n 1))))

;;deal with semi-tones Cb to B and Fb to E
 (if (and (eq? tonic 0)  (or (eq? keysig 6 ) (eq? keysig 0)) (< a -1)   (eq? (cdr(assoc n flat-list)) 1) )
     (begin (set! a  0)  (set! n (- n 1))))
; if keysig 4  and  note is  B or E make note flat
 (if (and  (eq? tonic 0) (eq? keysig 4)   (or (eq? n 2) (eq? n 6)) (eq? a 0))
      (begin (set! a -2) (set! n (+ n 1))))
; if keysig 5 and  note is   F  make note flat
 (if (and (eq? tonic 0) (eq? keysig 5) (eq? n 3) (eq? a 0))
     (begin (set! a  -2)  (set! n (+ n 1))))

;;deal with semi-tones B# to C and E# toF
; if keysig 3 or 0  and note is E# or B# make note natural
(if (and (eq? tonic 0) (or (eq? keysig 3 ) (eq? keysig 0) ) (> a 1) (eq? (cdr(assoc n sharp-list)) 1) )
     (begin (set! a  0)  (set! n (+ n 1))))
; if keysig 1  and  note is  C or F make note #
 (if (and  (eq? tonic 0) (eq? keysig 1)  (or (eq? n 0) (eq? n 3)) (eq? a 0))
      (begin (set! a 2) (set! n (- n 1))))
; if keysig 2 and  note is   F  make note #
 (if (and (eq? tonic 0) (eq? keysig 2) (eq? n 3) (eq? a 0))
     (begin (set! a  2)  (set! n (- n 1))))

;; modify the octave if necessary
(if ( < n 0) (begin (set! o (- o 1)) (set! n (+ n 7))))
(if  (> n 6) (begin (set! o (+ o 1)) (set! n (- n 7))))

;take account of preferred flats
(cond  ( (and  (eq? tonic 0) ( < k 0 )  ( > a  0) (eq? (cdr(assoc n sharp-list )) 0 )) (begin  (set! a (* a -1)) (set! n (+ n 1)))))
(cond  ( (and  (eq? tonic 0) ( < k 0 )  ( > a  0) (eq? (cdr(assoc n sharp-list )) 1 )) (begin  (set! a 0)        (set! n (+ n 1)))))

;take account of preferred sharps
(cond  ((and  (eq? tonic 0) ( > k 0 )  ( < a  0)  (eq? (cdr(assoc n flat-list )) 0 ))  (begin  (set! a (* a -1)) (set! n (-  n
1)))))
(cond  ((and  (eq? tonic 0) ( > k 0 )  ( < a  0)  (eq? (cdr(assoc n flat-list )) 1 ))  (begin  (set! a 0)        (set! n (-  n
1)))))

;; modify the octave if necessary
(if ( < n 0) (begin (set! o (- o 1)) (set! n (+ n 7))))
(if  (> n 6) (begin (set! o (+ o 1)) (set! n (- n 7))))
(ly:make-pitch o n (/ a 4))

;; end scope of let
 )
;; end scope of naturalize-pitch
 )


#(define (naturalize music)
; get music data from the music file
(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)))
;write back to Lilypond
(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 lilypond pitch then naturalize before write back
(if (ly:pitch? p)
(begin
(set! p (naturalize-pitch p 0))
(ly:music-set-property! music 'pitch p)))

(
  ; if lilypond tonic then call naturalize but don't modify the pitch before write back
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))


regards
Peter Gentry



_______________________________________________
lilypond-user mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/lilypond-user

reply via email to

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