| [ << Pitches ] | [Top][Contents] | [ Rhythms >> ] |
| [ < Separating key cancellations from key signature changes ] | [ Up: Pitches ] | [ Turkish Makam example > ] |
Transposing pitches with minimum accidentals (“smart” transpose) ¶
This example uses some Scheme code to enforce enharmonic modifications for notes in order to have the minimum number of accidentals. In this case, the following rules apply:
- double accidentals should be removed
- b sharp → c
- e sharp → f
- c flat → b
- f flat → e
In this manner, the most natural enharmonic notes are chosen.
#(define (naturalize-pitch p) (let ((o (ly:pitch-octave p)) ;; `ly:pitch-alteration` returns quarter tone steps. (a (* 4 (ly:pitch-alteration p))) (n (ly:pitch-notename p))) (cond ((and (> a 1) (or (eqv? n 6) (eqv? n 2))) (set! a (- a 2)) (set! n (+ n 1))) ((and (< a -1) (or (eqv? n 0) (eqv? n 3))) (set! a (+ a 2)) (set! n (- n 1)))) (cond ((> a 2) (set! a (- a 4)) (set! n (+ n 1))) ((< a -2) (set! a (+ a 4)) (set! n (- n 1)))) (when (< n 0) (set! o (- o 1)) (set! n (+ n 7))) (when (> n 6) (set! o (+ o 1)) (set! n (- n 7))) (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))) (when (pair? es) (ly:music-set-property! music 'elements (map naturalize es))) (when (ly:music? e) (ly:music-set-property! music 'element (naturalize e))) (when (ly:pitch? p) (set! p (naturalize-pitch p)) (ly:music-set-property! music 'pitch p)) music)) naturalizeMusic = #(define-music-function (m) (ly:music?) (naturalize m)) music = \relative c' { c4 d e g } \new Staff { \transpose c ais { \music } \naturalizeMusic \transpose c ais { \music } \transpose c deses { \music } \naturalizeMusic \transpose c deses { \music } }
| [ << Pitches ] | [Top][Contents] | [ Rhythms >> ] |
| [ < Separating key cancellations from key signature changes ] | [ Up: Pitches ] | [ Turkish Makam example > ] |