]> git.donarmstrong.com Git - lilypond.git/blob - input/test/smart-transpose.ly
* scripts/lilypond-book.py (do_file): do not overwrite input file.
[lilypond.git] / input / test / smart-transpose.ly
1
2 \version "2.1.26"
3
4 \header {
5 texidoc="@cindex Smart Transpose
6 @example
7         Here's a copy of my feature request :
8 @quotation
9         Your task, if you accept it is to implement a \smarttranspose
10         command>> that would translate such oddities into more natural
11         notations. Double accidentals should be removed, as well as E-sharp
12         (-> F), bC (-> B), bF (-> E), B-sharp (-> C).
13 @end quotation
14
15 You mean like this. (Sorry 'bout the nuked indentation.)
16
17 Modified to use the standard transpose mechanism. The question is
18 how useful these enharmonic modifications are. Mats B.
19 @end example
20 "
21 }
22
23 #(define  (unhair-pitch p)
24   (let* ((o (ly:pitch-octave p))
25          (a (ly:pitch-alteration p))
26          (n (ly:pitch-notename p)))
27
28     (cond
29      ((and (> a 2) (or (eq? n 6) (eq? n 2)))
30       (set! a (- a 2))
31       (set! n (+ n 1)))
32      ((and (< a -2) (or (eq? n 0) (eq? n 3)))
33       (set! a (+ a 2))
34       (set! n (- n 1))))
35
36     (cond
37      ((eq? a 4) (set! a 0) (set! n (+ n 1)))
38      ((eq? a -4) (set! a 0) (set! n (- n 1))))
39
40     (if (< n 0) (begin (set!  o (- o 1)) (set! n (+ n 7))))
41     (if (> n 6) (begin (set!  o (+ o 1)) (set! n (- n 7))))
42
43     (ly:make-pitch o n a)))
44
45 #(define (simplify music)
46   (let* ((es (ly:music-property music 'elements))
47          (e (ly:music-property music 'element))
48          (p (ly:music-property music 'pitch)))
49
50     (if (pair? es)
51         (ly:music-set-property!
52          music 'elements
53          (map (lambda (x) (simplify x)) es)))
54
55     (if (ly:music? e)
56         (ly:music-set-property!
57          music 'element
58          (simplify e)))
59
60     (if (ly:pitch? p)
61         (begin
62           (set! p (unhair-pitch p))
63           (ly:music-set-property! music 'pitch p)))
64
65     music))
66
67 music = \notes \relative c' { c4 d  e f g a b  c }
68
69 \score {
70   \notes \context Staff {
71     \transpose c ais \music
72     \apply #simplify \transpose c ais \music
73   }
74   \paper { raggedright = ##t}
75 }
76
77