]> git.donarmstrong.com Git - lilypond.git/blob - input/test/smart-transpose.ly
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / input / test / smart-transpose.ly
1
2 \version "2.7.39"
3
4 \header {
5 texidoc="@cindex Smart Transpose
6
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.
12
13 "
14 }
15 %
16 % Modified to use the standard transpose mechanism. The question is
17 % how useful these enharmonic modifications are. Mats B.
18
19 % Why not to have a function that minimizes the number of accidentals? -HJJ
20 % Works also for quarter tones. -HJJ
21 %
22
23 #(define  (naturalise-pitch p)
24   (let* ((o (ly:pitch-octave p))
25          (a (ly:pitch-alteration p))
26          (n (ly:pitch-notename p)))
27
28     (cond
29      ((and (> a 1) (or (eq? n 6) (eq? n 2)))
30       (set! a (- a 2))
31       (set! n (+ n 1)))
32      ((and (< a -1) (or (eq? n 0) (eq? n 3)))
33       (set! a (+ a 2))
34       (set! n (- n 1))))
35
36     (cond
37      ((> a 2) (set! a (- a 4)) (set! n (+ n 1)))
38      ((< a -2) (set! a (+ a 4)) (set! n (- n 1))))
39
40     (if (< n 0) (begin (set!  o (- o 1)) (set! n (+ n 7))))
41     (if (> n 6) (begin (set!  o (+ o 1)) (set! n (- n 7))))
42
43     (ly:make-pitch o n a)))
44
45 #(define (naturalise music)
46   (let* ((es (ly:music-property music 'elements))
47          (e (ly:music-property music 'element))
48          (p (ly:music-property music 'pitch)))
49
50     (if (pair? es)
51         (ly:music-set-property!
52          music 'elements
53          (map (lambda (x) (naturalise x)) es)))
54
55     (if (ly:music? e)
56         (ly:music-set-property!
57          music 'element
58          (naturalise e)))
59
60     (if (ly:pitch? p)
61         (begin
62           (set! p (naturalise-pitch p))
63           (ly:music-set-property! music 'pitch p)))
64
65     music))
66
67 music =  \relative c' { c4 d  e f g a b  c }
68
69 \score {
70    \context Staff {
71     \transpose c ais \music
72     \applyMusic #naturalise \transpose c ais \music
73     \transpose c deses \music
74     \applyMusic #naturalise \transpose c deses \music
75   }
76   \layout { ragged-right = ##t}
77 }
78
79