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