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