]> git.donarmstrong.com Git - lilypond.git/blob - scm/chord-name.scm
fe4ba7ab12305dc9e126abfc13e203f9bfbe4e97
[lilypond.git] / scm / chord-name.scm
1 ;;;; chord-name.scm --  chord name utility functions
2 ;;;;
3 ;;;; source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2000--2006 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;;                 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 (define (natural-chord-alteration p)
9   "Return the natural alteration for step P."
10   (if (= (ly:pitch-steps p) 6)
11       FLAT
12       0))
13
14 ;; 
15 ;; TODO: make into markup.
16 ;; 
17 (define-public (alteration->text-accidental-markup alteration)
18   (make-smaller-markup
19    (make-raise-markup
20     (if (= alteration FLAT)
21         0.3
22         0.6)
23     (make-musicglyph-markup
24      (string-append "accidentals." (number->string alteration))))))
25   
26 (define (accidental->markup alteration)
27   "Return accidental markup for ALTERATION."
28   (if (= alteration 0)
29       (make-line-markup (list empty-markup))
30       (conditional-kern-before
31        (alteration->text-accidental-markup alteration)
32        (= alteration FLAT) 0.2)))
33
34 (define (accidental->markup-italian alteration)
35   "Return accidental markup for ALTERATION, for use after an italian chord root name."
36   (if (= alteration 0)
37       (make-hspace-markup 0.2)
38       (make-line-markup
39        (list
40         (make-hspace-markup (if (= alteration FLAT) 0.7 0.5))
41         (make-raise-markup 0.7 (alteration->text-accidental-markup alteration))
42         (make-hspace-markup (if (= alteration SHARP) 0.2 0.1))
43         ))))
44
45 (define-public (note-name->markup pitch)
46   "Return pitch markup for PITCH."
47   (make-line-markup
48    (list
49     (make-simple-markup
50      (vector-ref #("C" "D" "E" "F" "G" "A" "B") (ly:pitch-notename pitch)))
51      (accidental->markup (ly:pitch-alteration pitch)))))
52
53 (define-safe-public ((chord-name->german-markup B-instead-of-Bb) pitch)
54   "Return pitch markup for PITCH, using german note names.
55    If B-instead-of-Bb is set to #t real german names are returned.
56    Otherwise semi-german names (with Bb and below keeping the british names)
57 "
58   (let* ((name (ly:pitch-notename pitch))
59          (alt (ly:pitch-alteration pitch))
60          (n-a (if (member (cons name alt) `((6 . ,FLAT) (6 . ,DOUBLE-FLAT)))
61                  (cons 7 (+ (if B-instead-of-Bb SEMI-TONE 0) alt))
62                  (cons name alt))))
63     (make-line-markup
64      (list
65       (make-simple-markup
66        (vector-ref #("C" "D" "E" "F" "G" "A" "H" "B") (car n-a)))
67       (make-normal-size-super-markup
68        (accidental->markup (cdr n-a)))))))
69
70 (define-safe-public (note-name->german-markup pitch)
71   (let* ((name (ly:pitch-notename pitch))
72          (alt (ly:pitch-alteration pitch))
73          (n-a (if (member (cons name alt) `((6 . ,FLAT) (6 . ,DOUBLE-FLAT)))
74                   (cons 7 (+ SEMI-TONE alt))
75                   (cons name alt))))
76     (make-line-markup
77      (list
78       (string-append
79        (list-ref '("c" "d" "e" "f" "g" "a" "h" "b") (car n-a))
80        (if (or (equal? (car n-a) 2) (equal? (car n-a) 5))
81            (list-ref '( "ses" "s" "" "is" "isis") (+ 2 (/ (cdr n-a) 2)))
82            (list-ref '("eses" "es" "" "is" "isis") (+ 2 (/ (cdr n-a) 2)))))))))
83
84 (define-public ((chord-name->italian-markup re-with-eacute) pitch)
85   "Return pitch markup for PITCH, using italian/french note names.
86    If re-with-eacute is set to #t, french 'ré' is returned for D instead of 're'
87 "
88   (let* ((name (ly:pitch-notename pitch))
89          (alt (ly:pitch-alteration pitch)))
90     (make-line-markup
91      (list
92       (make-simple-markup
93        (vector-ref
94         (if re-with-eacute
95             #("Do" "Ré" "Mi" "Fa" "Sol" "La" "Si")
96             #("Do" "Re" "Mi" "Fa" "Sol" "La" "Si"))
97         name))
98       (accidental->markup-italian alt)
99       ))))
100
101 ;; fixme we should standardize on omit-root (or the other one.)
102 ;; perhaps the default should also be reversed --hwn
103 (define-safe-public (sequential-music-to-chord-exceptions seq . rest)
104   "Transform sequential music SEQ of type <<c d e>>-\\markup{ foobar }
105 to (cons CDE-PITCHES FOOBAR-MARKUP), or to (cons DE-PITCHES
106 FOOBAR-MARKUP) if OMIT-ROOT is given and non-false.
107 "
108
109   (define (chord-to-exception-entry m)
110     (let* ((elts (ly:music-property m 'elements))
111            (omit-root (and (pair? rest) (car rest)))
112            (pitches (map (lambda (x) (ly:music-property x 'pitch))
113                          (filter
114                           (lambda (y) (memq 'note-event
115                                             (ly:music-property y 'types)))
116                           elts)))
117            (sorted (sort pitches ly:pitch<?))
118            (root (car sorted))
119            
120            ;; ugh?
121            ;;(diff (ly:pitch-diff root (ly:make-pitch -1 0 0)))
122            ;; FIXME.  This results in #<Pitch c> ...,
123            ;; but that is what we need because default octave for
124            ;; \chords has changed to c' too?
125            (diff (ly:pitch-diff root (ly:make-pitch 0 0 0)))
126            (normalized (map (lambda (x) (ly:pitch-diff x diff)) sorted))
127            (texts (map (lambda (x) (ly:music-property x 'text))
128                        (filter
129                         (lambda (y) (memq 'text-script-event
130                                           (ly:music-property y 'types)))
131                         elts)))
132
133            (text (if (null? texts) #f (if omit-root (car texts) texts))))
134       (cons (if omit-root (cdr normalized) normalized) text)))
135
136   (define (is-event-chord? m)
137     (and
138      (memq 'event-chord (ly:music-property m 'types))
139      (not (equal? ZERO-MOMENT (ly:music-length m)))))
140
141   (let* ((elts (filter is-event-chord? (ly:music-property seq 'elements)))
142          (alist (map chord-to-exception-entry elts)))
143     (filter (lambda (x) (cdr x)) alist)))
144