]> git.donarmstrong.com Git - lilypond.git/blob - input/lsr/transposing-pitches-with-minimum-accidentals-smart-transpose.ly
Merge commit 'ce4b499'
[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 that
9 case, ``Double accidentals should be removed, as well as E-sharp
10 (-> F), bC (-> B), bF (-> E), B-sharp (-> C).'', as proposed by a
11 request for a new feature.  In this manner, the most natural
12 enharmonic notes are chosen in this example.  "
13 }
14 % begin verbatim
15 #(define  (naturalise-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
21     (cond
22      ((and (> a 1) (or (eq? n 6) (eq? n 2)))
23       (set! a (- a 2))
24       (set! n (+ n 1)))
25      ((and (< a -1) (or (eq? n 0) (eq? n 3)))
26       (set! a (+ a 2))
27       (set! n (- n 1))))
28
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
33     (if (< n 0) (begin (set!  o (- o 1)) (set! n (+ n 7))))
34     (if (> n 6) (begin (set!  o (+ o 1)) (set! n (- n 7))))
35
36     (ly:make-pitch o n (/ a 4))))
37
38 #(define (naturalise music)
39   (let* ((es (ly:music-property music 'elements))
40          (e (ly:music-property music 'element))
41          (p (ly:music-property music 'pitch)))
42
43     (if (pair? es)
44         (ly:music-set-property!
45          music 'elements
46          (map (lambda (x) (naturalise x)) es)))
47
48     (if (ly:music? e)
49         (ly:music-set-property!
50          music 'element
51          (naturalise e)))
52
53     (if (ly:pitch? p)
54         (begin
55           (set! p (naturalise-pitch p))
56           (ly:music-set-property! music 'pitch p)))
57
58     music))
59
60 music =  \relative c' { c4 d  e f g a b  c }
61
62 naturaliseMusic =
63 #(define-music-function (parser location m)
64                                         (ly:music?)
65                         (naturalise m))
66
67 \score {
68    \new Staff {
69      \transpose c ais \music
70      \naturaliseMusic \transpose c ais \music 
71     \break
72     \transpose c deses \music
73     \naturaliseMusic \transpose c deses \music
74   }
75   \layout { ragged-right = ##t}
76 }
77
78