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