]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-name-engraver.cc
Run `make grand-replace'.
[lilypond.git] / lily / chord-name-engraver.cc
1 /*
2   chord-name-engraver.cc -- implement Chord_name_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2008 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
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"
20
21 #include "translator.icc"
22
23 class Chord_name_engraver : public Engraver
24 {
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_;
35
36   SCM last_chord_;
37 };
38
39 void
40 Chord_name_engraver::finalize ()
41 {
42 }
43
44 void
45 Chord_name_engraver::derived_mark () const
46 {
47   scm_gc_mark (last_chord_);
48 }
49
50 Chord_name_engraver::Chord_name_engraver ()
51 {
52   chord_name_ = 0;
53   last_chord_ = SCM_EOL;
54 }
55
56 void
57 Chord_name_engraver::process_music ()
58 {
59   if (!notes_.size ())
60     return;
61
62   SCM bass = SCM_EOL;
63   SCM inversion = SCM_EOL;
64   SCM pitches = SCM_EOL;
65
66   Stream_event *inversion_event = 0;
67   for (vsize i = 0; i < notes_.size (); i++)
68     {
69       Stream_event *n = notes_[i];
70       SCM p = n->get_property ("pitch");
71       if (!unsmob_pitch (p))
72         continue;
73
74       if (n->get_property ("inversion") == SCM_BOOL_T)
75         {
76           inversion_event = n;
77           inversion = p;
78         }
79       else if (n->get_property ("bass") == SCM_BOOL_T)
80         bass = p;
81       else
82         pitches = scm_cons (p, pitches);
83     }
84
85   if (inversion_event)
86     {
87       SCM oct = inversion_event->get_property ("octavation");
88       if (scm_is_number (oct))
89         {
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));
93
94           pitches = scm_cons (orig.smobbed_copy (), pitches);
95         }
96       else
97         programming_error ("inversion does not have original pitch");
98     }
99
100   pitches = scm_sort_list (pitches, Pitch::less_p_proc);
101
102   SCM name_proc = get_property ("chordNameFunction");
103   SCM markup = scm_call_4 (name_proc, pitches, bass, inversion,
104                            context ()->self_scm ());
105
106   /*
107     Ugh.
108   */
109   SCM chord_as_scm = scm_cons (pitches, scm_cons (bass, inversion));
110
111   chord_name_ = make_item ("ChordName", notes_[0]->self_scm ());
112   chord_name_->set_property ("text", markup);
113
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);
118
119   last_chord_ = chord_as_scm;
120 }
121
122 IMPLEMENT_TRANSLATOR_LISTENER (Chord_name_engraver, note);
123 void
124 Chord_name_engraver::listen_note (Stream_event *ev)
125 {
126   notes_.push_back (ev);
127 }
128
129 void
130 Chord_name_engraver::stop_translation_timestep ()
131 {
132   chord_name_ = 0;
133   notes_.clear ();
134 }
135
136 /*
137   The READs description is not strictly accurate:
138   which properties are read depend on the chord naming function active.
139 */
140 ADD_TRANSLATOR (Chord_name_engraver,
141                 /* doc */
142                 "Catch note events and generate the appropriate chordname.",
143
144                 /* create */
145                 "ChordName ",
146
147                 /* read */
148                 "chordChanges "
149                 "chordNameExceptions "
150                 "chordNameFunction "
151                 "chordNoteNamer "
152                 "chordRootNamer "
153                 "chordNameExceptions "
154                 "majorSevenSymbol ",
155
156                 /* write */
157                 ""
158                 );