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