3 doctitle = "Transposing music with minimum accidentals"
5 texidoc = "This example uses some Scheme code to enforce enharmonic modifications for
6 notes in order to have the minimum number of accidentals. In this
7 case, the following rules apply:
11 Double accidentals should be removed
27 In this manner, the most natural enharmonic notes are chosen.
30 #(define (naturalize-pitch p)
31 (let* ((o (ly:pitch-octave p))
32 (a (* 4 (ly:pitch-alteration p)))
33 ; alteration, a, in quarter tone steps, for historical reasons
34 (n (ly:pitch-notename p)))
36 ((and (> a 1) (or (eq? n 6) (eq? n 2)))
39 ((and (< a -1) (or (eq? n 0) (eq? n 3)))
43 ((> a 2) (set! a (- a 4)) (set! n (+ n 1)))
44 ((< a -2) (set! a (+ a 4)) (set! n (- n 1))))
45 (if (< n 0) (begin (set! o (- o 1)) (set! n (+ n 7))))
46 (if (> n 6) (begin (set! o (+ o 1)) (set! n (- n 7))))
47 (ly:make-pitch o n (/ a 4))))
49 #(define (naturalize music)
50 (let* ((es (ly:music-property music 'elements))
51 (e (ly:music-property music 'element))
52 (p (ly:music-property music 'pitch)))
54 (ly:music-set-property!
56 (map (lambda (x) (naturalize x)) es)))
58 (ly:music-set-property!
63 (set! p (naturalize-pitch p))
64 (ly:music-set-property! music 'pitch p)))
68 #(define-music-function (parser location m)
72 music = \relative c' { c4 d e g }
76 \transpose c ais \music
77 \naturalizeMusic \transpose c ais \music
78 \transpose c deses \music
79 \naturalizeMusic \transpose c deses \music
81 \layout { ragged-right = ##t }