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