2 \sourcefilename "smart-transpose.ly"
5 texidoc="@cindex Smart Transpose
7 There is a way to enforce enharmonic modifications for notes in order
8 to have the minimum number of accidentals. In that case, ``Double
9 accidentals should be removed, as well as E-sharp (-> F), bC (-> B),
10 bF (-> E), B-sharp (-> C).'', as proposed by a request for a new feature.
11 In this manner, the most natural enharmonic notes are chosen in this example.
15 % Modified to use the standard transpose mechanism. The question is
16 % how useful these enharmonic modifications are. Mats B.
18 % Why not to have a function that minimizes the number of accidentals? -HJJ
19 % Works also for quarter tones. -HJJ
22 #(define (naturalise-pitch p)
23 (let* ((o (ly:pitch-octave p))
24 (a (ly:pitch-alteration p))
25 (n (ly:pitch-notename p)))
28 ((and (> a 1) (or (eq? n 6) (eq? n 2)))
31 ((and (< a -1) (or (eq? n 0) (eq? n 3)))
36 ((> a 2) (set! a (- a 4)) (set! n (+ n 1)))
37 ((< a -2) (set! a (+ a 4)) (set! n (- n 1))))
39 (if (< n 0) (begin (set! o (- o 1)) (set! n (+ n 7))))
40 (if (> n 6) (begin (set! o (+ o 1)) (set! n (- n 7))))
42 (ly:make-pitch o n a)))
44 #(define (naturalise music)
45 (let* ((es (ly:music-property music 'elements))
46 (e (ly:music-property music 'element))
47 (p (ly:music-property music 'pitch)))
50 (ly:music-set-property!
52 (map (lambda (x) (naturalise x)) es)))
55 (ly:music-set-property!
61 (set! p (naturalise-pitch p))
62 (ly:music-set-property! music 'pitch p)))
66 music = \relative c' { c4 d e f g a b c }
69 #(define-music-function (parser location m)
75 \transpose c ais \music
76 \naturaliseMusic \transpose c ais \music
77 \transpose c deses \music
78 \naturaliseMusic \transpose c deses \music
80 \layout { ragged-right = ##t}