2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "engraver.hh"
23 #include "directional-element-interface.hh"
24 #include "duration.hh"
25 #include "international.hh"
28 #include "rhythmic-head.hh"
29 #include "script-interface.hh"
30 #include "staff-symbol-referencer.hh"
31 #include "stem-tremolo.hh"
33 #include "stream-event.hh"
35 #include "translator.icc"
37 class Stem_engraver : public Engraver
41 vector <Grob *> maybe_flags_;
42 Stream_event *rhythmic_ev_;
43 Stream_event *tremolo_ev_;
46 TRANSLATOR_DECLARATIONS (Stem_engraver);
49 void make_stem (Grob_info, bool);
51 void listen_tremolo (Stream_event *);
52 void listen_tuplet_span (Stream_event *);
53 void acknowledge_rhythmic_head (Grob_info);
54 void stop_translation_timestep ();
56 void kill_unused_flags ();
59 Stem_engraver::Stem_engraver ()
65 tuplet_start_ = false;
69 Stem_engraver::make_stem (Grob_info gi, bool tuplet_start)
71 /* Announce the cause of the head as cause of the stem. The
72 stem needs a rhythmic structure to fit it into a beam. */
73 stem_ = make_item ("Stem", gi.grob ()->self_scm ());
75 stem_->set_property ("tuplet-start", SCM_BOOL_T);
76 (void) make_item ("StemStub", gi.grob ()->self_scm ());
79 /* Stem tremolo is never applied to a note by default,
80 it must be requested. But there is a default for the
85 the first and last (quarter) note both get one tremolo flag. */
87 = robust_scm2int (tremolo_ev_->get_property ("tremolo-type"), 8);
90 we take the duration log from the Event, since the duration-log
91 for a note head is always <= 2.
93 Stream_event *ev = gi.event_cause ();
94 Duration *dur = unsmob<Duration> (ev->get_property ("duration"));
96 int tremolo_flags = intlog2 (requested_type) - 2
97 - (dur->duration_log () > 2 ? dur->duration_log () - 2 : 0);
98 if (tremolo_flags <= 0)
100 tremolo_ev_->origin ()->warning (_ ("tremolo duration is too long"));
106 tremolo_ = make_item ("StemTremolo", tremolo_ev_->self_scm ());
108 /* The number of tremolo flags is the number of flags of the
109 tremolo-type minus the number of flags of the note itself. */
110 tremolo_->set_property ("flag-count", scm_from_int (tremolo_flags));
111 tremolo_->set_parent (stem_, X_AXIS);
112 stem_->set_object ("tremolo-flag", tremolo_->self_scm ());
113 tremolo_->set_object ("stem", stem_->self_scm ());
119 Stem_engraver::acknowledge_rhythmic_head (Grob_info gi)
121 if (Rhythmic_head::get_stem (gi.grob ()))
124 Stream_event *cause = gi.event_cause ();
127 Duration *d = unsmob<Duration> (cause->get_property ("duration"));
132 make_stem (gi, tuplet_start_);
134 int ds = Stem::duration_log (stem_);
135 int dc = d->duration_log ();
137 // half notes and quarter notes all have compatible stems.
138 // Longas are done differently (oops?), so we can't unify
139 // them with the other stemmed notes.
144 // whole notes and brevis both have no stems
152 gi.event_cause ()->origin ()->warning (_f ("adding note head to incompatible stem (type = %d/%d)",
153 ds < 0 ? 1 << -ds : 1,
154 ds > 0 ? 1 << ds : 1));
155 gi.event_cause ()->origin ()->warning (_ ("maybe input should specify polyphonic voices"));
158 Stem::add_head (stem_, gi.grob ());
160 if (Stem::is_normal_stem (stem_)
161 && Stem::duration_log (stem_) > 2
162 && !(unsmob<Grob> (stem_->get_object ("flag"))))
164 Item *flag = make_item ("Flag", stem_->self_scm ());
165 flag->set_parent (stem_, X_AXIS);
166 stem_->set_object ("flag", flag->self_scm ());
167 maybe_flags_.push_back (flag);
170 stem_->set_property ("tuplet-start", SCM_BOOL_T);
174 Stem_engraver::kill_unused_flags ()
176 for (vsize i = 0; i < maybe_flags_.size (); i++)
177 if (unsmob<Grob> (maybe_flags_[i]->get_parent (X_AXIS)->get_object ("beam")))
178 maybe_flags_[i]->suicide ();
182 Stem_engraver::finalize ()
184 kill_unused_flags ();
188 Stem_engraver::stop_translation_timestep ()
190 if (scm_is_string (get_property ("whichBar")))
191 kill_unused_flags ();
196 /* FIXME: junk these properties. */
197 SCM prop = get_property ("stemLeftBeamCount");
198 if (scm_is_number (prop))
200 Stem::set_beaming (stem_, scm_to_int (prop), LEFT);
201 context ()->unset_property (ly_symbol2scm ("stemLeftBeamCount"));
203 prop = get_property ("stemRightBeamCount");
204 if (scm_is_number (prop))
206 Stem::set_beaming (stem_, scm_to_int (prop), RIGHT);
207 context ()->unset_property (ly_symbol2scm ("stemRightBeamCount"));
211 tuplet_start_ = false;
216 Stem_engraver::listen_tuplet_span (Stream_event *ev)
218 Direction dir = to_dir (ev->get_property ("span-direction"));
221 // set stem property if stem already exists
223 stem_->set_property ("tuplet-start", SCM_BOOL_T);
224 tuplet_start_ = true; // stash the value for use in later creation
229 Stem_engraver::listen_tremolo (Stream_event *ev)
231 ASSIGN_EVENT_ONCE (tremolo_ev_, ev);
236 Stem_engraver::boot ()
238 ADD_LISTENER (Stem_engraver, tuplet_span);
239 ADD_LISTENER (Stem_engraver, tremolo);
240 ADD_ACKNOWLEDGER (Stem_engraver, rhythmic_head);
243 ADD_TRANSLATOR (Stem_engraver,
245 "Create stems, flags and single-stem tremolos. It also works"
246 " together with the beam engraver for overriding beaming.",
256 "stemRightBeamCount "