]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
* lily/include/event.hh: remove file.
[lilypond.git] / lily / stem-engraver.cc
1 /*
2   stem-engraver.cc -- implement Stem_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "context.hh"
10 #include "directional-element-interface.hh"
11 #include "engraver.hh"
12 #include "item.hh"
13 #include "misc.hh"
14 #include "rhythmic-head.hh"
15 #include "script-interface.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "stem-tremolo.hh"
18 #include "stem.hh"
19 #include "duration.hh"
20
21 /**
22    Make stems upon receiving noteheads.
23 */
24 class Stem_engraver : public Engraver
25 {
26   Grob *stem_;
27   Grob *tremolo_;
28   Music *rhythmic_ev_;
29   Music *tremolo_ev_;
30
31   TRANSLATOR_DECLARATIONS (Stem_engraver);
32
33 protected:
34   void make_stem (Grob_info);
35
36   virtual void acknowledge_grob (Grob_info);
37   virtual void stop_translation_timestep ();
38   virtual bool try_music (Music *);
39 };
40
41 Stem_engraver::Stem_engraver ()
42 {
43   tremolo_ev_ = 0;
44   stem_ = 0;
45   tremolo_ = 0;
46   rhythmic_ev_ = 0;
47 }
48
49 void
50 Stem_engraver::make_stem (Grob_info gi)
51 {
52   /* Announce the cause of the head as cause of the stem.  The
53      stem needs a rhythmic structure to fit it into a beam.  */
54   stem_ = make_item ("Stem", gi.music_cause ()->self_scm ());
55
56   /*
57     we take the duration log from the Event, since the duration-log
58     for a note head is always <= 2.
59   */
60   Music *music = gi.music_cause ();
61   Duration *dur = unsmob_duration (music->get_property ("duration"));
62   
63   stem_->set_property ("duration-log", dur ? scm_int2num (dur->duration_log ()) : 0);
64
65   if (tremolo_ev_)
66     {
67       /* Stem tremolo is never applied to a note by default,
68          is must me requested.  But there is a default for the
69          tremolo value:
70
71          c4:8 c c:
72
73          the first and last (quarter) note bothe get one tremolo flag.  */
74       int requested_type
75         = scm_to_int (tremolo_ev_->get_property ("tremolo-type"));
76       SCM f = get_property ("tremoloFlags");
77       if (!requested_type)
78         {
79           if (scm_is_number (f))
80             requested_type = scm_to_int (f);
81           else
82             requested_type = 8;
83         }
84       else
85         context ()->set_property ("tremoloFlags", scm_int2num (requested_type));
86
87       int tremolo_flags = intlog2 (requested_type) - 2
88         - (dur->duration_log () > 2 ? dur->duration_log () - 2 : 0);
89       if (tremolo_flags <= 0)
90         {
91           tremolo_ev_->origin ()->warning (_ ("tremolo duration is too long"));
92           tremolo_flags = 0;
93         }
94
95       if (tremolo_flags)
96         {
97           tremolo_ = make_item ("StemTremolo", tremolo_ev_->self_scm ());
98
99           /* The number of tremolo flags is the number of flags of the
100              tremolo-type minus the number of flags of the note itself.  */
101           tremolo_->set_property ("flag-count", scm_int2num (tremolo_flags));
102           tremolo_->set_parent (stem_, X_AXIS);
103           stem_->set_property ("tremolo-flag", tremolo_->self_scm ());
104           tremolo_->set_property ("stem", stem_->self_scm ());
105         }
106     }
107 }
108
109 void
110 Stem_engraver::acknowledge_grob (Grob_info gi)
111 {
112   if (Rhythmic_head::has_interface (gi.grob ()))
113     {
114       if (Rhythmic_head::get_stem (gi.grob ()))
115         return;
116
117       Music *cause = gi.music_cause ();
118       if (!cause)
119         return;
120       Duration *d = unsmob_duration (cause->get_property ("duration"));
121       if (!d)
122         return ;
123       
124       if (!stem_)
125         make_stem (gi);
126       
127       if (Stem::duration_log (stem_) != d->duration_log ())
128         {
129           // FIXME: 
130           gi.music_cause ()->origin ()->warning (_f ("adding note head to incompatible stem (type = %d)",
131                                                      1 << Stem::duration_log (stem_)));
132           gi.music_cause ()->origin ()->warning (_f ("maybe input should specify polyphonic voices"));
133         }
134
135       Stem::add_head (stem_, gi.grob ());
136     }
137 }
138
139 void
140 Stem_engraver::stop_translation_timestep ()
141 {
142   tremolo_ = 0;
143   if (stem_)
144     {
145       /* FIXME: junk these properties.  */
146       SCM prop = get_property ("stemLeftBeamCount");
147       if (scm_is_number (prop))
148         {
149           Stem::set_beaming (stem_, scm_to_int (prop), LEFT);
150           context ()->unset_property (ly_symbol2scm ("stemLeftBeamCount"));
151         }
152       prop = get_property ("stemRightBeamCount");
153       if (scm_is_number (prop))
154         {
155           Stem::set_beaming (stem_, scm_to_int (prop), RIGHT);
156           context ()->unset_property (ly_symbol2scm ("stemRightBeamCount"));
157         }
158       stem_ = 0;
159     }
160   tremolo_ev_ = 0;
161 }
162
163 bool
164 Stem_engraver::try_music (Music *m)
165 {
166   if (m->is_mus_type ("tremolo-event"))
167     {
168       tremolo_ev_ = m;
169       return true;
170     }
171   return false;
172 }
173
174 ADD_TRANSLATOR (Stem_engraver,
175                 /* descr */ "Create stems and single-stem tremolos.  It also works together with "
176                 "the beam engraver for overriding beaming.",
177                 /* creats*/ "Stem StemTremolo",
178                 /* accepts */ "tremolo-event",
179                 /* acks  */ "rhythmic-head-interface",
180                 /* reads */ "tremoloFlags stemLeftBeamCount stemRightBeamCount",
181                 /* write */ "");