]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-name-engraver.cc
* lily/include/translator.icc: new file.
[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--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "engraver.hh"
10 #include "chord-name.hh"
11 #include "output-def.hh"
12 #include "font-interface.hh"
13 #include "output-def.hh"
14 #include "dimensions.hh"
15 #include "item.hh"
16 #include "protected-scm.hh"
17 #include "context.hh"
18 #include "warn.hh"
19 #include "pitch.hh"
20
21 class Chord_name_engraver : public Engraver
22 {
23   TRANSLATOR_DECLARATIONS (Chord_name_engraver);
24 protected:
25   PRECOMPUTED_VIRTUAL void stop_translation_timestep ();
26   PRECOMPUTED_VIRTUAL void process_music ();
27   virtual bool try_music (Music *);
28   virtual void finalize ();
29   virtual void derived_mark () const;
30 private:
31   void add_note (Music *);
32
33   Item *chord_name_;
34   Link_array<Music> 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::add_note (Music *n)
58 {
59   notes_.push (n);
60 }
61
62 void
63 Chord_name_engraver::process_music ()
64 {
65   if (!notes_.size ())
66     return;
67
68   SCM bass = SCM_EOL;
69   SCM inversion = SCM_EOL;
70   SCM pitches = SCM_EOL;
71
72   Music *inversion_event = 0;
73   for (int i = 0; i < notes_.size (); i++)
74     {
75       Music *n = notes_[i];
76       SCM p = n->get_property ("pitch");
77       if (!unsmob_pitch (p))
78         continue;
79
80       if (n->get_property ("inversion") == SCM_BOOL_T)
81         {
82           inversion_event = n;
83           inversion = p;
84         }
85       else if (n->get_property ("bass") == SCM_BOOL_T)
86         bass = p;
87       else
88         pitches = scm_cons (p, pitches);
89     }
90
91   if (inversion_event)
92     {
93       SCM oct = inversion_event->get_property ("octavation");
94       if (scm_is_number (oct))
95         {
96           Pitch *p = unsmob_pitch (inversion_event->get_property ("pitch"));
97           int octavation = scm_to_int (oct);
98           Pitch orig = p->transposed (Pitch (-octavation, 0, 0));
99
100           pitches = scm_cons (orig.smobbed_copy (), pitches);
101         }
102       else
103         programming_error ("inversion does not have original pitch");
104     }
105
106   pitches = scm_sort_list (pitches, Pitch::less_p_proc);
107
108   SCM name_proc = get_property ("chordNameFunction");
109   SCM markup = scm_call_4 (name_proc, pitches, bass, inversion,
110                            context ()->self_scm ());
111
112   /*
113     Ugh.
114   */
115   SCM chord_as_scm = scm_cons (pitches, scm_cons (bass, inversion));
116
117   chord_name_ = make_item ("ChordName", notes_[0]->self_scm ());
118   chord_name_->set_property ("text", markup);
119
120   SCM s = get_property ("chordChanges");
121   if (to_boolean (s) && scm_is_pair (last_chord_)
122       && ly_is_equal (chord_as_scm, last_chord_))
123     chord_name_->set_property ("begin-of-line-visible", SCM_BOOL_T);
124
125   last_chord_ = chord_as_scm;
126 }
127
128 bool
129 Chord_name_engraver::try_music (Music *m)
130 {
131   /*
132     hmm. Should check?
133   */
134   if (m->is_mus_type ("note-event"))
135     {
136       add_note (m);
137       return true;
138     }
139   return false;
140 }
141
142 void
143 Chord_name_engraver::stop_translation_timestep ()
144 {
145   chord_name_ = 0;
146   notes_.clear ();
147 }
148
149 /*
150   The READs description is not strictly accurate:
151   which properties are read depend on the chord naming function active.
152 */
153 #include "translator.icc"
154
155 ADD_TRANSLATOR (Chord_name_engraver,
156                 /* descr */ "Catch note-events "
157                 "and generate the appropriate chordname.",
158                 /* creats*/ "ChordName",
159                 /* accepts */ "note-event",
160                 /* acks  */ "",
161                 /* reads */ "chordChanges chordNameExceptions chordNameFunction "
162                 "chordNoteNamer chordRootNamer chordNameExceptions majorSevenSymbol",
163                 /* write */ "");