]> git.donarmstrong.com Git - lilypond.git/blob - input/lsr/transposing-pitches-with-minimum-accidentals-smart-transpose.ly
Move input/new doctitles and update LSR
[lilypond.git] / input / lsr / transposing-pitches-with-minimum-accidentals-smart-transpose.ly
1 %% Do not edit this file; it is auto-generated from input/new
2 %% This file is in the public domain.
3 \version "2.11.38"
4 \header {
5   lsrtags = "pitches"
6   texidoc = "This example uses some Scheme code to enforce enharmonic modifications for
7 notes in order to have the minimum number of accidentals. In this
8 case, the following rules apply:
9
10 @itemize
11 @item
12 Double accidentals should be removed
13
14 @item
15 B sharp -> C
16
17 @item
18 E sharp -> F
19
20 @item
21 C flat -> B
22
23 @item
24 F flat -> E
25
26 @end itemize
27
28 In this manner, the most natural enharmonic notes are chosen.
29 "
30   doctitle = "Transposing music with minimum accidentals"
31 } % begin verbatim
32
33 #(define  (naturalize-pitch p)
34   (let* ((o (ly:pitch-octave p))
35          (a (* 4 (ly:pitch-alteration p))) 
36     ; alteration, a, in quarter tone steps, for historical reasons
37          (n (ly:pitch-notename p)))
38     (cond
39      ((and (> a 1) (or (eq? n 6) (eq? n 2)))
40       (set! a (- a 2))
41       (set! n (+ n 1)))
42      ((and (< a -1) (or (eq? n 0) (eq? n 3)))
43       (set! a (+ a 2))
44       (set! n (- n 1))))
45     (cond
46      ((> a 2) (set! a (- a 4)) (set! n (+ n 1)))
47      ((< a -2) (set! a (+ a 4)) (set! n (- n 1))))
48     (if (< n 0) (begin (set!  o (- o 1)) (set! n (+ n 7))))
49     (if (> n 6) (begin (set!  o (+ o 1)) (set! n (- n 7))))
50     (ly:make-pitch o n (/ a 4))))
51
52 #(define (naturalize music)
53   (let* ((es (ly:music-property music 'elements))
54          (e (ly:music-property music 'element))
55          (p (ly:music-property music 'pitch)))
56     (if (pair? es)
57         (ly:music-set-property!
58          music 'elements
59          (map (lambda (x) (naturalize x)) es)))
60     (if (ly:music? e)
61         (ly:music-set-property!
62          music 'element
63          (naturalize e)))
64     (if (ly:pitch? p)
65         (begin
66           (set! p (naturalize-pitch p))
67           (ly:music-set-property! music 'pitch p)))
68     music))
69
70 naturalizeMusic =
71 #(define-music-function (parser location m)
72                                         (ly:music?)
73                         (naturalize m))
74
75 music =  \relative c' { c4 d e g }
76
77 \score {
78   \new Staff {
79     \transpose c ais \music
80     \naturalizeMusic \transpose c ais \music
81     \transpose c deses \music
82     \naturalizeMusic \transpose c deses \music
83   }
84   \layout { ragged-right = ##t }
85 }