]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-name-engraver.cc
* lily/include/translator.hh (ENTER_DESCRIPTION): add
[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--2002 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "engraver.hh"
10 #include "chord-name.hh"
11 #include "chord.hh"
12 #include "musical-request.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 (Note_req *);
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 (Note_req* 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   if (Note_req* n = dynamic_cast<Note_req*> (m))
70     {
71       add_note (n);
72       return true;
73     }
74   return false;
75 }
76
77 void
78 Chord_name_engraver::process_music ()
79 {
80   if (ly_car (chord_) != SCM_EOL)
81     {
82       chord_name_ = new Item (get_property ("ChordName"));
83       chord_name_->set_grob_property ("chord", chord_);
84       announce_grob(chord_name_, SCM_EOL);
85       SCM s = get_property ("chordChanges");
86       if (to_boolean (s) && ly_car (last_chord_) != SCM_EOL
87                   && gh_equal_p (chord_, last_chord_))
88         chord_name_->set_grob_property ("begin-of-line-visible", SCM_BOOL_T);
89     }
90 }
91
92 void
93 Chord_name_engraver::stop_translation_timestep ()
94 {
95   if (chord_name_)
96     {
97       typeset_grob (chord_name_);
98     }
99   chord_name_ = 0;
100
101   if (ly_car (chord_) != SCM_EOL)
102     last_chord_ = chord_;
103   chord_ = gh_cons (SCM_EOL, gh_cons (SCM_EOL, SCM_EOL));
104 }
105
106 ENTER_DESCRIPTION(Chord_name_engraver,
107 /* descr */       "Catch Note_req's, Tonic_reqs, Inversion_reqs, Bass_req
108 and generate the appropriate chordname.",
109 /* creats*/       "ChordName",
110 /* accepts */     "general-music",
111 /* acks  */      "",
112 /* reads */       "chordChanges",
113 /* write */       "");