]> git.donarmstrong.com Git - lilypond.git/blob - input/new/transposing-pitches-with-minimum-accidentals-smart-transpose.ly
Merge branch 'master' of ssh+git://git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / input / new / transposing-pitches-with-minimum-accidentals-smart-transpose.ly
1 \version "2.11.33"
2 \header {
3   doctitle = "Transposing music with minimum accidentals"
4   lsrtags = "pitches"
5  texidoc = "There is a way to enforce enharmonic modifications for
6 notes in order to have the minimum number of accidentals. In this
7 case, the following rules apply:
8
9 \"Double accidentals should be removed, as well as E sharp (-> F),
10 C flat (-> B), F flat (-> E) and B sharp (-> C)\".
11
12 In this manner, the most natural enharmonic notes are chosen.
13 "}
14
15 #(define  (naturalize-pitch p)
16   (let* ((o (ly:pitch-octave p))
17          (a (* 4 (ly:pitch-alteration p))) 
18     ; alteration, a, in quarter tone steps, for historical reasons
19          (n (ly:pitch-notename p)))
20     (cond
21      ((and (> a 1) (or (eq? n 6) (eq? n 2)))
22       (set! a (- a 2))
23       (set! n (+ n 1)))
24      ((and (< a -1) (or (eq? n 0) (eq? n 3)))
25       (set! a (+ a 2))
26       (set! n (- n 1))))
27     (cond
28      ((> a 2) (set! a (- a 4)) (set! n (+ n 1)))
29      ((< a -2) (set! a (+ a 4)) (set! n (- n 1))))
30     (if (< n 0) (begin (set!  o (- o 1)) (set! n (+ n 7))))
31     (if (> n 6) (begin (set!  o (+ o 1)) (set! n (- n 7))))
32     (ly:make-pitch o n (/ a 4))))
33
34 #(define (naturalize music)
35   (let* ((es (ly:music-property music 'elements))
36          (e (ly:music-property music 'element))
37          (p (ly:music-property music 'pitch)))
38     (if (pair? es)
39         (ly:music-set-property!
40          music 'elements
41          (map (lambda (x) (naturalize x)) es)))
42     (if (ly:music? e)
43         (ly:music-set-property!
44          music 'element
45          (naturalize e)))
46     (if (ly:pitch? p)
47         (begin
48           (set! p (naturalize-pitch p))
49           (ly:music-set-property! music 'pitch p)))
50     music))
51
52 naturalizeMusic =
53 #(define-music-function (parser location m)
54                                         (ly:music?)
55                         (naturalize m))
56
57 music =  \relative c' { c4 d e g }
58
59 \score {
60   \new Staff {
61     \transpose c ais \music
62     \naturalizeMusic \transpose c ais \music
63     \transpose c deses \music
64     \naturalizeMusic \transpose c deses \music
65   }
66   \layout { ragged-right = ##t }
67 }