]> git.donarmstrong.com Git - lilypond.git/blob - scm/chord-name.scm
(alteration->text-accidental-markup): change
[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--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;;                 Han-Wen Nienhuys <hanwen@cs.uu.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-public (note-name->markup pitch)
35   "Return pitch markup for PITCH."
36   (make-line-markup
37    (list
38     (make-simple-markup
39      (vector-ref #("C" "D" "E" "F" "G" "A" "B") (ly:pitch-notename pitch)))
40      (accidental->markup (ly:pitch-alteration pitch)))))
41
42 (define-public ((chord-name->german-markup B-instead-of-Bb) pitch)
43   "Return pitch markup for PITCH, using german note names.
44    If B-instead-of-Bb is set to #t real german names are returned.
45    Otherwise semi-german names (with Bb and below keeping the british names)
46 "
47   (let* ((name (ly:pitch-notename pitch))
48          (alt (ly:pitch-alteration pitch))
49          (n-a (if (member (cons name alt) `((6 . ,FLAT) (6 . ,DOUBLE-FLAT)))
50                  (cons 7 (+ (if B-instead-of-Bb SEMI-TONE 0) alt))
51                  (cons name alt))))
52     (make-line-markup
53      (list
54       (make-simple-markup
55        (vector-ref #("C" "D" "E" "F" "G" "A" "H" "B") (car n-a)))
56       (make-normal-size-super-markup
57        (accidental->markup (cdr n-a)))))))
58
59 (define-public (note-name->german-markup pitch)
60   (let* ((name (ly:pitch-notename pitch))
61          (alt (ly:pitch-alteration pitch))
62          (n-a (if (member (cons name alt) `((6 . ,FLAT) (6 . ,DOUBLE-FLAT)))
63                   (cons 7 (+ SEMI-TONE alt))
64                   (cons name alt))))
65     (make-line-markup
66      (list
67       (string-append
68        (list-ref '("c" "d" "e" "f" "g" "a" "h" "b") (car n-a))
69        (if (or (equal? (car n-a) 2) (equal? (car n-a) 5))
70            (list-ref '( "ses" "s" "" "is" "isis") (+ 2 (/ (cdr n-a) 2)))
71            (list-ref '("eses" "es" "" "is" "isis") (+ 2 (/ (cdr n-a) 2)))))))))
72
73 ;; fixme we should standardize on omit-root (or the other one.)
74 ;; perhaps the default should also be reversed --hwn
75 (define-public (sequential-music-to-chord-exceptions seq . rest)
76   "Transform sequential music SEQ of type <<c d e>>-\\markup{ foobar }
77 to (cons CDE-PITCHES FOOBAR-MARKUP), or to (cons DE-PITCHES
78 FOOBAR-MARKUP) if OMIT-ROOT is given and non-false.
79 "
80
81   (define (chord-to-exception-entry m)
82     (let* ((elts (ly:music-property m 'elements))
83            (omit-root (and (pair? rest) (car rest)))
84            (pitches (map (lambda (x) (ly:music-property x 'pitch))
85                          (filter
86                           (lambda (y) (memq 'note-event
87                                             (ly:music-property y 'types)))
88                           elts)))
89            (sorted (sort pitches ly:pitch<?))
90            (root (car sorted))
91            
92            ;; ugh?
93            ;;(diff (ly:pitch-diff root (ly:make-pitch -1 0 0)))
94            ;; FIXME.  This results in #<Pitch c> ...,
95            ;; but that is what we need because default octave for
96            ;; \chords has changed to c' too?
97            (diff (ly:pitch-diff root (ly:make-pitch 0 0 0)))
98            (normalized (map (lambda (x) (ly:pitch-diff x diff)) sorted))
99            (texts (map (lambda (x) (ly:music-property x 'text))
100                        (filter
101                         (lambda (y) (memq 'text-script-event
102                                           (ly:music-property y 'types)))
103                         elts)))
104
105            (text (if (null? texts) #f (if omit-root (car texts) texts))))
106       (cons (if omit-root (cdr normalized) normalized) text)))
107
108   (define (is-event-chord? m)
109     (and
110      (memq 'event-chord (ly:music-property m 'types))
111      (not (equal? ZERO-MOMENT (ly:music-length m)))))
112
113   (let* ((elts (filter is-event-chord? (ly:music-property seq 'elements)))
114          (alist (map chord-to-exception-entry elts)))
115     (filter (lambda (x) (cdr x)) alist)))
116