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