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