Nitpick: ly:spanner-bound grob name slur -> spanner.
[lilypond.git] / lily / chord-name-engraver.cc
blobf8858998a1d963669bc18d37f1ccc3d0aa8b6d43
1 /*
2 chord-name-engraver.cc -- implement Chord_name_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
9 #include "chord-name.hh"
10 #include "context.hh"
11 #include "dimensions.hh"
12 #include "engraver.hh"
13 #include "font-interface.hh"
14 #include "item.hh"
15 #include "output-def.hh"
16 #include "pitch.hh"
17 #include "protected-scm.hh"
18 #include "stream-event.hh"
19 #include "warn.hh"
21 #include "translator.icc"
23 class Chord_name_engraver : public Engraver
25 TRANSLATOR_DECLARATIONS (Chord_name_engraver);
26 protected:
27 void stop_translation_timestep ();
28 void process_music ();
29 virtual void finalize ();
30 virtual void derived_mark () const;
31 DECLARE_TRANSLATOR_LISTENER (note);
32 private:
33 Item *chord_name_;
34 vector<Stream_event*> notes_;
36 SCM last_chord_;
39 void
40 Chord_name_engraver::finalize ()
44 void
45 Chord_name_engraver::derived_mark () const
47 scm_gc_mark (last_chord_);
50 Chord_name_engraver::Chord_name_engraver ()
52 chord_name_ = 0;
53 last_chord_ = SCM_EOL;
56 void
57 Chord_name_engraver::process_music ()
59 if (!notes_.size ())
60 return;
62 SCM bass = SCM_EOL;
63 SCM inversion = SCM_EOL;
64 SCM pitches = SCM_EOL;
66 Stream_event *inversion_event = 0;
67 for (vsize i = 0; i < notes_.size (); i++)
69 Stream_event *n = notes_[i];
70 SCM p = n->get_property ("pitch");
71 if (!unsmob_pitch (p))
72 continue;
74 if (n->get_property ("inversion") == SCM_BOOL_T)
76 inversion_event = n;
77 inversion = p;
79 else if (n->get_property ("bass") == SCM_BOOL_T)
80 bass = p;
81 else
82 pitches = scm_cons (p, pitches);
85 if (inversion_event)
87 SCM oct = inversion_event->get_property ("octavation");
88 if (scm_is_number (oct))
90 Pitch *p = unsmob_pitch (inversion_event->get_property ("pitch"));
91 int octavation = scm_to_int (oct);
92 Pitch orig = p->transposed (Pitch (-octavation, 0, 0));
94 pitches = scm_cons (orig.smobbed_copy (), pitches);
96 else
97 programming_error ("inversion does not have original pitch");
100 pitches = scm_sort_list (pitches, Pitch::less_p_proc);
102 SCM name_proc = get_property ("chordNameFunction");
103 SCM markup = scm_call_4 (name_proc, pitches, bass, inversion,
104 context ()->self_scm ());
107 Ugh.
109 SCM chord_as_scm = scm_cons (pitches, scm_cons (bass, inversion));
111 chord_name_ = make_item ("ChordName", notes_[0]->self_scm ());
112 chord_name_->set_property ("text", markup);
114 SCM s = get_property ("chordChanges");
115 if (to_boolean (s) && scm_is_pair (last_chord_)
116 && ly_is_equal (chord_as_scm, last_chord_))
117 chord_name_->set_property ("begin-of-line-visible", SCM_BOOL_T);
119 last_chord_ = chord_as_scm;
122 IMPLEMENT_TRANSLATOR_LISTENER (Chord_name_engraver, note);
123 void
124 Chord_name_engraver::listen_note (Stream_event *ev)
126 notes_.push_back (ev);
129 void
130 Chord_name_engraver::stop_translation_timestep ()
132 chord_name_ = 0;
133 notes_.clear ();
137 The READs description is not strictly accurate:
138 which properties are read depend on the chord naming function active.
140 ADD_TRANSLATOR (Chord_name_engraver,
141 /* doc */
142 "Catch note events and generate the appropriate chordname.",
144 /* create */
145 "ChordName ",
147 /* read */
148 "chordChanges "
149 "chordNameExceptions "
150 "chordNameFunction "
151 "chordNoteNamer "
152 "chordRootNamer "
153 "chordNameExceptions "
154 "majorSevenSymbol ",
156 /* write */