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