]> git.donarmstrong.com Git - lilypond.git/blob - scm/chord-name.scm
Add '-dcrop' option to ps and svg backends
[lilypond.git] / scm / chord-name.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2000--2015 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 (define (conditional-string-downcase str condition)
26   (if condition
27       (string-downcase str)
28       str))
29
30 ;;
31 ;; TODO: make into markup.
32 ;;
33 (define-public (alteration->text-accidental-markup alteration)
34
35   (make-smaller-markup
36    (make-raise-markup
37     (if (= alteration FLAT)
38         0.3
39         0.6)
40     (make-musicglyph-markup
41      (assoc-get alteration standard-alteration-glyph-name-alist "")))))
42
43 (define (accidental->markup alteration)
44   "Return accidental markup for ALTERATION."
45   (if (= alteration 0)
46       (make-line-markup (list empty-markup))
47       (conditional-kern-before
48        (alteration->text-accidental-markup alteration)
49        (= alteration FLAT) 0.094725)))
50
51 (define (accidental->markup-italian alteration)
52   "Return accidental markup for ALTERATION, for use after an italian chord root name."
53   (if (= alteration 0)
54       (make-hspace-markup 0.2)
55       (make-line-markup
56        (list
57         (make-hspace-markup (if (= alteration FLAT) 0.57285385 0.5))
58         (make-raise-markup 0.7 (alteration->text-accidental-markup alteration))
59         (make-hspace-markup (if (= alteration SHARP) 0.2 0.1))
60         ))))
61
62 (define-public (note-name->markup pitch lowercase?)
63   "Return pitch markup for @var{pitch}."
64   (make-line-markup
65    (list
66     (make-simple-markup
67      (conditional-string-downcase
68       (vector-ref #("C" "D" "E" "F" "G" "A" "B") (ly:pitch-notename pitch))
69       lowercase?))
70     (accidental->markup (ly:pitch-alteration pitch)))))
71
72 (define (pitch-alteration-semitones pitch)
73   (inexact->exact (round (* (ly:pitch-alteration pitch) 2))))
74
75 (define-safe-public ((chord-name->german-markup B-instead-of-Bb)
76                      pitch lowercase?)
77   "Return pitch markup for PITCH, using german note names.
78    If B-instead-of-Bb is set to #t real german names are returned.
79    Otherwise semi-german names (with Bb and below keeping the british names)
80 "
81   (let* ((name (ly:pitch-notename pitch))
82          (alt-semitones  (pitch-alteration-semitones pitch))
83          (n-a (if (member (cons name alt-semitones) `((6 . -1) (6 . -2)))
84                   (cons 7 (+ (if B-instead-of-Bb 1 0) alt-semitones))
85                   (cons name alt-semitones))))
86     (make-line-markup
87      (list
88       (make-simple-markup
89        (conditional-string-downcase
90         (vector-ref #("C" "D" "E" "F" "G" "A" "H" "B") (car n-a))
91         lowercase?))
92       (make-normal-size-super-markup
93        (accidental->markup (/ (cdr n-a) 2)))))))
94
95 (define-safe-public (note-name->german-markup pitch lowercase?)
96   (let* ((name (ly:pitch-notename pitch))
97          (alt-semitones (pitch-alteration-semitones pitch))
98          (n-a (if (member (cons name alt-semitones) `((6 . -1) (6 . -2)))
99                   (cons 7 (+ 1 alt-semitones))
100                   (cons name alt-semitones))))
101     (make-line-markup
102      (list
103       (string-append
104        (list-ref '("c" "d" "e" "f" "g" "a" "h" "b") (car n-a))
105        (if (or (equal? (car n-a) 2) (equal? (car n-a) 5))
106            (list-ref '( "ses" "s" "" "is" "isis") (+ 2 (cdr n-a)))
107            (list-ref '("eses" "es" "" "is" "isis") (+ 2 (cdr n-a)))))))))
108
109 (define ((chord-name->italian-markup re-with-eacute) pitch lowercase?)
110   "Return pitch markup for @var{pitch}, using Italian/@/French note names.
111 If @var{re-with-eacute} is set to @code{#t}, french `ré' is returned for
112 pitch@tie{}D instead of `re'."
113
114   (let* ((name (ly:pitch-notename pitch))
115          (alt (ly:pitch-alteration pitch)))
116     (make-line-markup
117      (list
118       (make-simple-markup
119        (conditional-string-downcase
120         (vector-ref
121          (if re-with-eacute
122              #("Do" "Ré" "Mi" "Fa" "Sol" "La" "Si")
123              #("Do" "Re" "Mi" "Fa" "Sol" "La" "Si"))
124          name)
125         lowercase?))
126       (accidental->markup-italian alt)
127       ))))
128 (export chord-name->italian-markup)
129
130 ;; fixme we should standardize on omit-root (or the other one.)
131 ;; perhaps the default should also be reversed --hwn
132 (define-safe-public (sequential-music-to-chord-exceptions seq . rest)
133   "Transform sequential music SEQ of type <<c d e>>-\\markup{ foobar }
134 to (cons CDE-PITCHES FOOBAR-MARKUP), or to (cons DE-PITCHES
135 FOOBAR-MARKUP) if OMIT-ROOT is given and non-false.
136 "
137
138   (define (chord-to-exception-entry m)
139     (let* ((elts (ly:music-property m 'elements))
140            (omit-root (and (pair? rest) (car rest)))
141            (pitches (map (lambda (x) (ly:music-property x 'pitch))
142                          (filter
143                           (lambda (y) (memq 'note-event
144                                             (ly:music-property y 'types)))
145                           elts)))
146            (sorted (sort pitches ly:pitch<?))
147            (root (car sorted))
148
149            ;; ugh?
150            ;;(diff (ly:pitch-diff root (ly:make-pitch -1 0 0)))
151            ;; FIXME.  This results in #<Pitch c> ...,
152            ;; but that is what we need because default octave for
153            ;; \chords has changed to c' too?
154            (diff (ly:pitch-diff root (ly:make-pitch 0 0 0)))
155            (normalized (map (lambda (x) (ly:pitch-diff x diff)) sorted))
156            (texts (map (lambda (x) (ly:music-property x 'text))
157                        (filter
158                         (lambda (y) (memq 'text-script-event
159                                           (ly:music-property y 'types)))
160                         elts)))
161
162            (text (if (null? texts) #f (if omit-root (car texts) texts))))
163       (cons (if omit-root (cdr normalized) normalized) text)))
164
165   (define (is-event-chord? m)
166     (and
167      (memq 'event-chord (ly:music-property m 'types))
168      (not (equal? ZERO-MOMENT (ly:music-length m)))))
169
170   (let* ((elts (filter is-event-chord? (ly:music-property seq 'elements)))
171          (alist (map chord-to-exception-entry elts)))
172     (filter cdr alist)))