]> 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
21 class Chord_name_engraver : public Engraver 
22 {
23   TRANSLATOR_DECLARATIONS( Chord_name_engraver);
24 protected:
25   virtual void stop_translation_timestep ();
26   virtual void process_music ();
27   virtual bool try_music (Music *);
28
29 private:
30   void add_note (Music *);
31   
32   Item* chord_name_;
33
34   Protected_scm chord_;
35   Protected_scm last_chord_;
36 };
37
38
39
40 Chord_name_engraver::Chord_name_engraver ()
41 {
42   chord_name_ = 0;
43   chord_ = gh_cons (SCM_EOL, gh_cons (SCM_EOL, SCM_EOL));
44   last_chord_ = chord_;
45 }
46
47 void
48 Chord_name_engraver::add_note (Music * n)
49 {
50   SCM pitches = ly_car (chord_);
51   SCM modifiers = ly_cdr (chord_);
52   SCM inversion = modifiers == SCM_EOL ? SCM_EOL : ly_car (modifiers);
53   SCM bass = modifiers == SCM_EOL ? SCM_EOL : ly_cdr (modifiers);
54   
55   if (n->get_mus_property ("inversion") == SCM_BOOL_T)
56     inversion = n->get_mus_property ("pitch");
57   else if (n->get_mus_property ("bass") == SCM_BOOL_T)
58     bass = n->get_mus_property ("pitch");
59   else
60     pitches = scm_sort_list (gh_cons (n->get_mus_property ("pitch"), pitches),
61                              Pitch::less_p_proc);
62   chord_ = gh_cons (pitches, gh_cons (inversion, bass));
63 }
64
65 bool
66 Chord_name_engraver::try_music (Music* m)
67 {
68   /*
69     hmm. Should check? 
70    */
71   if (m->is_mus_type ("note-event"))
72     {
73       add_note (m);
74       return true;
75     }
76   return false;
77 }
78
79 void
80 Chord_name_engraver::process_music ()
81 {
82   if (ly_car (chord_) != SCM_EOL)
83     {
84       chord_name_ = new Item (get_property ("ChordName"));
85       chord_name_->set_grob_property ("chord", chord_);
86       announce_grob(chord_name_, SCM_EOL);
87       SCM s = get_property ("chordChanges");
88       if (to_boolean (s) && ly_car (last_chord_) != SCM_EOL
89                   && gh_equal_p (chord_, last_chord_))
90         chord_name_->set_grob_property ("begin-of-line-visible", SCM_BOOL_T);
91     }
92 }
93
94 void
95 Chord_name_engraver::stop_translation_timestep ()
96 {
97   if (chord_name_)
98     {
99       typeset_grob (chord_name_);
100     }
101   chord_name_ = 0;
102
103   if (ly_car (chord_) != SCM_EOL)
104     last_chord_ = chord_;
105   chord_ = gh_cons (SCM_EOL, gh_cons (SCM_EOL, SCM_EOL));
106 }
107
108 ENTER_DESCRIPTION(Chord_name_engraver,
109 /* descr */       "Catch note-events, Tonic_reqs, Inversion_reqs, Bass_req "
110 "and generate the appropriate chordname.",
111 /* creats*/       "ChordName",
112 /* accepts */     "note-event busy-playing-event",
113 /* acks  */      "",
114 /* reads */       "chordChanges",
115 /* write */       "");