]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/snippets/transposing-pitches-with-minimum-accidentals-smart-transpose.ly
New upstream version 2.19.65
[lilypond.git] / Documentation / snippets / transposing-pitches-with-minimum-accidentals-smart-transpose.ly
1 % DO NOT EDIT this file manually; it is automatically
2 % generated from Documentation/snippets/new
3 % Make any changes in Documentation/snippets/new/
4 % and then run scripts/auxiliar/makelsr.py
5 %
6 % This file is in the public domain.
7 %% Note: this file works from version 2.19.22
8 \version "2.19.22"
9
10 \header {
11   lsrtags = "pitches, scheme-language, workaround"
12
13   texidoc = "
14 This example uses some Scheme code to enforce enharmonic modifications
15 for notes in order to have the minimum number of accidentals.  In this
16 case, the following rules apply:
17
18 Double accidentals should be removed
19
20
21 B sharp -> C
22
23
24 E sharp -> F
25
26
27 C flat -> B
28
29
30 F flat -> E
31
32
33 In this manner, the most natural enharmonic notes are chosen.
34
35 "
36   doctitle = "Transposing pitches with minimum accidentals (\"Smart\" transpose)"
37 } % begin verbatim
38
39 #(define (naturalize-pitch p)
40    (let ((o (ly:pitch-octave p))
41          (a (* 4 (ly:pitch-alteration p)))
42          ;; alteration, a, in quarter tone steps,
43          ;; for historical reasons
44          (n (ly:pitch-notename p)))
45      (cond
46       ((and (> a 1) (or (eqv? n 6) (eqv? n 2)))
47        (set! a (- a 2))
48        (set! n (+ n 1)))
49       ((and (< a -1) (or (eqv? n 0) (eqv? n 3)))
50        (set! a (+ a 2))
51        (set! n (- n 1))))
52      (cond
53       ((> a 2) (set! a (- a 4)) (set! n (+ n 1)))
54       ((< a -2) (set! a (+ a 4)) (set! n (- n 1))))
55      (if (< n 0) (begin (set! o (- o 1)) (set! n (+ n 7))))
56      (if (> n 6) (begin (set! o (+ o 1)) (set! n (- n 7))))
57      (ly:make-pitch o n (/ a 4))))
58
59 #(define (naturalize music)
60    (let ((es (ly:music-property music 'elements))
61          (e (ly:music-property music 'element))
62          (p (ly:music-property music 'pitch)))
63      (if (pair? es)
64          (ly:music-set-property!
65           music 'elements
66           (map naturalize es)))
67      (if (ly:music? e)
68          (ly:music-set-property!
69           music 'element
70           (naturalize e)))
71      (if (ly:pitch? p)
72          (begin
73            (set! p (naturalize-pitch p))
74            (ly:music-set-property! music 'pitch p)))
75      music))
76
77 naturalizeMusic =
78 #(define-music-function (m)
79    (ly:music?)
80    (naturalize m))
81
82 music = \relative c' { c4 d e g }
83
84 \score {
85   \new Staff {
86     \transpose c ais { \music }
87     \naturalizeMusic \transpose c ais { \music }
88     \transpose c deses { \music }
89     \naturalizeMusic \transpose c deses { \music }
90   }
91   \layout { }
92 }