]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-name-engraver.cc
26b4a607bb13bb39fbcd6954da896ab293161625
[lilypond.git] / lily / chord-name-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2012 Jan Nieuwenhuizen <janneke@gnu.org>
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
20 #include "chord-name.hh"
21 #include "context.hh"
22 #include "dimensions.hh"
23 #include "engraver.hh"
24 #include "font-interface.hh"
25 #include "item.hh"
26 #include "output-def.hh"
27 #include "pitch.hh"
28 #include "protected-scm.hh"
29 #include "stream-event.hh"
30 #include "text-interface.hh"
31 #include "warn.hh"
32
33 #include "translator.icc"
34
35 class Chord_name_engraver : public Engraver
36 {
37   TRANSLATOR_DECLARATIONS (Chord_name_engraver);
38 protected:
39   void stop_translation_timestep ();
40   void process_music ();
41   virtual void finalize ();
42   virtual void derived_mark () const;
43   DECLARE_TRANSLATOR_LISTENER (note);
44   DECLARE_TRANSLATOR_LISTENER (rest);
45 private:
46   Item *chord_name_;
47   vector<Stream_event *> notes_;
48
49   SCM last_chord_;
50   Stream_event *rest_event_;
51 };
52
53 void
54 Chord_name_engraver::finalize ()
55 {
56 }
57
58 void
59 Chord_name_engraver::derived_mark () const
60 {
61   scm_gc_mark (last_chord_);
62 }
63
64 Chord_name_engraver::Chord_name_engraver ()
65 {
66   chord_name_ = 0;
67   last_chord_ = SCM_EOL;
68   rest_event_ = 0;
69 }
70
71 void
72 Chord_name_engraver::process_music ()
73 {
74   if (!rest_event_ && !notes_.size ())
75    return;
76
77   chord_name_ = make_item ("ChordName",
78                            rest_event_ ? rest_event_->self_scm () : notes_[0]->self_scm ());
79
80   SCM maybe_markup = chord_name_->get_property_data ("text");
81
82   bool make_markup = !(Text_interface::is_markup (maybe_markup)
83                        || ly_is_procedure (maybe_markup));
84
85   SCM markup;
86
87   if (rest_event_ && !make_markup) { }
88   else if (rest_event_)
89     {
90       SCM no_chord_markup = get_property ("noChordSymbol");
91       if (!Text_interface::is_markup (no_chord_markup))
92         return;
93       markup = no_chord_markup;
94     }
95   else
96     {
97       SCM bass = SCM_EOL;
98       SCM inversion = SCM_EOL;
99       SCM pitches = SCM_EOL;
100
101       Stream_event *inversion_event = 0;
102       for (vsize i = 0; i < notes_.size (); i++)
103         {
104           Stream_event *n = notes_[i];
105           SCM p = n->get_property ("pitch");
106           if (!unsmob_pitch (p))
107             continue;
108
109           if (n->get_property ("inversion") == SCM_BOOL_T)
110             {
111               inversion_event = n;
112               inversion = p;
113             }
114           else if (n->get_property ("bass") == SCM_BOOL_T)
115             bass = p;
116           else
117             pitches = scm_cons (p, pitches);
118         }
119
120       if (inversion_event)
121         {
122           SCM oct = inversion_event->get_property ("octavation");
123           if (scm_is_number (oct))
124             {
125               Pitch *p = unsmob_pitch (inversion_event->get_property ("pitch"));
126               int octavation = scm_to_int (oct);
127               Pitch orig = p->transposed (Pitch (-octavation, 0, 0));
128
129               pitches = scm_cons (orig.smobbed_copy (), pitches);
130             }
131           else
132             programming_error ("inversion does not have original pitch");
133         }
134
135       pitches = scm_sort_list (pitches, Pitch::less_p_proc);
136
137       SCM name_proc = get_property ("chordNameFunction");
138       if (make_markup)
139         markup = scm_call_4 (name_proc, pitches, bass, inversion,
140                              context ()->self_scm ());
141     }
142   /*
143     Ugh.
144   */
145   if (make_markup)
146     chord_name_->set_property ("text", markup);
147   else if (Text_interface::is_markup (maybe_markup))
148     markup = maybe_markup;
149
150   SCM chord_changes = get_property ("chordChanges");
151   if (to_boolean (chord_changes) && scm_is_pair (last_chord_)
152       && ly_is_equal (markup, last_chord_))
153     chord_name_->set_property ("begin-of-line-visible", SCM_BOOL_T);
154
155   last_chord_ = markup;
156 }
157
158 IMPLEMENT_TRANSLATOR_LISTENER (Chord_name_engraver, note);
159 void
160 Chord_name_engraver::listen_note (Stream_event *ev)
161 {
162   notes_.push_back (ev);
163 }
164
165 IMPLEMENT_TRANSLATOR_LISTENER (Chord_name_engraver, rest);
166 void
167 Chord_name_engraver::listen_rest (Stream_event *ev)
168 {
169   ASSIGN_EVENT_ONCE (rest_event_, ev);
170 }
171
172 void
173 Chord_name_engraver::stop_translation_timestep ()
174 {
175   chord_name_ = 0;
176   notes_.clear ();
177   rest_event_ = 0;
178 }
179
180 /*
181   The READs description is not strictly accurate:
182   which properties are read depend on the chord naming function active.
183 */
184 ADD_TRANSLATOR (Chord_name_engraver,
185                 /* doc */
186                 "Catch note and rest events and generate the appropriate chordname.",
187
188                 /* create */
189                 "ChordName ",
190
191                 /* read */
192                 "chordChanges "
193                 "chordNameExceptions "
194                 "chordNameFunction "
195                 "chordNoteNamer "
196                 "chordRootNamer "
197                 "chordNameExceptions "
198                 "majorSevenSymbol "
199                 "noChordSymbol ",
200
201                 /* write */
202                 ""
203                );