]> git.donarmstrong.com Git - lilypond.git/blob - scm/chord-name.scm
Merge branch 'master' of git://git.sv.gnu.org/lilypond
[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      (assoc-get alteration standard-alteration-glyph-name-alist "")))))
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 (pitch-alteration-semitones pitch)
54   (inexact->exact (round (* (ly:pitch-alteration pitch) 2))))
55
56 (define-safe-public ((chord-name->german-markup B-instead-of-Bb) pitch)
57   "Return pitch markup for PITCH, using german note names.
58    If B-instead-of-Bb is set to #t real german names are returned.
59    Otherwise semi-german names (with Bb and below keeping the british names)
60 "
61   (let* ((name (ly:pitch-notename pitch))
62          (alt-semitones  (pitch-alteration-semitones pitch))
63          (n-a (if (member (cons name alt-semitones) `((6 . -1) (6 . -2)))
64                   (cons 7 (+ (if B-instead-of-Bb 1 0) alt-semitones))
65                   (cons name alt-semitones))))
66     (make-line-markup
67      (list
68       (make-simple-markup
69        (vector-ref #("C" "D" "E" "F" "G" "A" "H" "B") (car n-a)))
70       (make-normal-size-super-markup
71        (accidental->markup (/ (cdr n-a) 2)))))))
72
73 (define-safe-public (note-name->german-markup pitch)
74   (let* ((name (ly:pitch-notename pitch))
75          (alt-semitones (pitch-alteration-semitones pitch))
76          (n-a (if (member (cons name alt-semitones) `((6 . -1) (6 . -2)))
77                   (cons 7 (+ 1 alt-semitones))
78                   (cons name alt-semitones))))
79     (make-line-markup
80      (list
81       (string-append
82        (list-ref '("c" "d" "e" "f" "g" "a" "h" "b") (car n-a))
83        (if (or (equal? (car n-a) 2) (equal? (car n-a) 5))
84            (list-ref '( "ses" "s" "" "is" "isis") (+ 2 (cdr n-a)))
85            (list-ref '("eses" "es" "" "is" "isis") (+ 2 (cdr n-a)))))))))
86
87 (define-public ((chord-name->italian-markup re-with-eacute) pitch)
88   "Return pitch markup for PITCH, using italian/french note names.
89    If re-with-eacute is set to #t, french 'ré' is returned for D instead of 're'
90 "
91   (let* ((name (ly:pitch-notename pitch))
92          (alt (ly:pitch-alteration pitch)))
93     (make-line-markup
94      (list
95       (make-simple-markup
96        (vector-ref
97         (if re-with-eacute
98             #("Do" "Ré" "Mi" "Fa" "Sol" "La" "Si")
99             #("Do" "Re" "Mi" "Fa" "Sol" "La" "Si"))
100         name))
101       (accidental->markup-italian alt)
102       ))))
103
104 ;; fixme we should standardize on omit-root (or the other one.)
105 ;; perhaps the default should also be reversed --hwn
106 (define-safe-public (sequential-music-to-chord-exceptions seq . rest)
107   "Transform sequential music SEQ of type <<c d e>>-\\markup{ foobar }
108 to (cons CDE-PITCHES FOOBAR-MARKUP), or to (cons DE-PITCHES
109 FOOBAR-MARKUP) if OMIT-ROOT is given and non-false.
110 "
111
112   (define (chord-to-exception-entry m)
113     (let* ((elts (ly:music-property m 'elements))
114            (omit-root (and (pair? rest) (car rest)))
115            (pitches (map (lambda (x) (ly:music-property x 'pitch))
116                          (filter
117                           (lambda (y) (memq 'note-event
118                                             (ly:music-property y 'types)))
119                           elts)))
120            (sorted (sort pitches ly:pitch<?))
121            (root (car sorted))
122            
123            ;; ugh?
124            ;;(diff (ly:pitch-diff root (ly:make-pitch -1 0 0)))
125            ;; FIXME.  This results in #<Pitch c> ...,
126            ;; but that is what we need because default octave for
127            ;; \chords has changed to c' too?
128            (diff (ly:pitch-diff root (ly:make-pitch 0 0 0)))
129            (normalized (map (lambda (x) (ly:pitch-diff x diff)) sorted))
130            (texts (map (lambda (x) (ly:music-property x 'text))
131                        (filter
132                         (lambda (y) (memq 'text-script-event
133                                           (ly:music-property y 'types)))
134                         elts)))
135
136            (text (if (null? texts) #f (if omit-root (car texts) texts))))
137       (cons (if omit-root (cdr normalized) normalized) text)))
138
139   (define (is-event-chord? m)
140     (and
141      (memq 'event-chord (ly:music-property m 'types))
142      (not (equal? ZERO-MOMENT (ly:music-length m)))))
143
144   (let* ((elts (filter is-event-chord? (ly:music-property seq 'elements)))
145          (alist (map chord-to-exception-entry elts)))
146     (filter (lambda (x) (cdr x)) alist)))
147