]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-name-engraver.cc
0a78017c8fe5b0a39d25f9580b0a3acc8734357c
[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--2001 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
25   TRANSLATOR_DECLARATIONS( Chord_name_engraver);
26
27 protected:
28   virtual void stop_translation_timestep ();
29   virtual void acknowledge_grob (Grob_info i);
30   virtual void create_grobs ();
31   virtual bool try_music (Music *);
32
33 private:
34   void add_note (Note_req *);
35   
36   Item* chord_name_p_;
37
38   Protected_scm chord_;
39   Protected_scm last_chord_;
40 };
41
42
43
44 Chord_name_engraver::Chord_name_engraver ()
45 {
46   chord_name_p_ = 0;
47   chord_ = gh_cons (SCM_EOL, gh_cons (SCM_EOL, SCM_EOL));
48   last_chord_ = chord_;
49 }
50
51 void
52 Chord_name_engraver::add_note (Note_req* n)
53 {
54   SCM pitches = ly_car (chord_);
55   SCM modifiers = ly_cdr (chord_);
56   SCM inversion = modifiers == SCM_EOL ? SCM_EOL : ly_car (modifiers);
57   SCM bass = modifiers == SCM_EOL ? SCM_EOL : ly_cdr (modifiers);
58   
59   if (n->get_mus_property ("inversion") == SCM_BOOL_T)
60     inversion = n->get_mus_property ("pitch");
61   else if (n->get_mus_property ("bass") == SCM_BOOL_T)
62     bass = n->get_mus_property ("pitch");
63   else
64     pitches = scm_sort_list (gh_cons (n->get_mus_property ("pitch"), pitches),
65                              Pitch::less_p_proc);
66   chord_ = gh_cons (pitches, gh_cons (inversion, bass));
67 }
68
69 bool
70 Chord_name_engraver::try_music (Music* m)
71 {
72   if (Note_req* n = dynamic_cast<Note_req*> (m))
73     {
74       add_note (n);
75       return true;
76     }
77   return false;
78 }
79
80 void
81 Chord_name_engraver::acknowledge_grob (Grob_info i)
82 {
83   if (Note_req* n = dynamic_cast<Note_req*> (i.music_cause ()))
84     add_note (n);
85 }
86
87 void
88 Chord_name_engraver::create_grobs ()
89 {
90   if (!chord_name_p_ && ly_car (chord_) != SCM_EOL)
91     {
92       chord_name_p_ = new Item (get_property ("ChordName"));
93       chord_name_p_->set_grob_property ("chord", chord_);
94       announce_grob (chord_name_p_, 0);
95       SCM s = get_property ("chordChanges");
96       if (to_boolean (s) && ly_car (last_chord_) != SCM_EOL
97                   && gh_equal_p (chord_, last_chord_))
98         chord_name_p_->set_grob_property ("begin-of-line-visible", SCM_BOOL_T);
99     }
100 }
101
102 void
103 Chord_name_engraver::stop_translation_timestep ()
104 {
105   if (chord_name_p_)
106     {
107       typeset_grob (chord_name_p_);
108     }
109   chord_name_p_ = 0;
110
111   if (ly_car (chord_) != SCM_EOL)
112     last_chord_ = chord_;
113   chord_ = gh_cons (SCM_EOL, gh_cons (SCM_EOL, SCM_EOL));
114 }
115
116 ENTER_DESCRIPTION(Chord_name_engraver,
117 /* descr */       "Catch Note_req's, Tonic_reqs, Inversion_reqs, Bass_req
118 and generate the appropriate chordname.",
119 /* creats*/       "ChordName",
120 /* acks  */       "grob-interface",
121 /* reads */       "chordChanges",
122 /* write */       "");