]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
* lily/translator.cc, lily/context.cc:, lily/translator-group.cc:
[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   /*
62     we take the duration log from the Event, since the duration-log
63     for a note head is always <= 2.
64   */
65   Stream_event *ev = gi.event_cause ();
66   Duration *dur = unsmob_duration (ev->get_property ("duration"));
67
68   stem_->set_property ("duration-log", dur ? scm_from_int (dur->duration_log ()) : 0);
69
70   if (tremolo_ev_)
71     {
72       /* Stem tremolo is never applied to a note by default,
73          is must me requested.  But there is a default for the
74          tremolo value:
75
76          c4:8 c c:
77
78          the first and last (quarter) note bothe get one tremolo flag.  */
79       int requested_type
80         = scm_to_int (tremolo_ev_->get_property ("tremolo-type"));
81       SCM f = get_property ("tremoloFlags");
82       if (!requested_type)
83         {
84           if (scm_is_number (f))
85             requested_type = scm_to_int (f);
86           else
87             requested_type = 8;
88         }
89       else
90         context ()->set_property ("tremoloFlags", scm_from_int (requested_type));
91
92       int tremolo_flags = intlog2 (requested_type) - 2
93         - (dur->duration_log () > 2 ? dur->duration_log () - 2 : 0);
94       if (tremolo_flags <= 0)
95         {
96           tremolo_ev_->origin ()->warning (_ ("tremolo duration is too long"));
97           tremolo_flags = 0;
98         }
99
100       if (tremolo_flags)
101         {
102           tremolo_ = make_item ("StemTremolo", tremolo_ev_->self_scm ());
103
104           /* The number of tremolo flags is the number of flags of the
105              tremolo-type minus the number of flags of the note itself.  */
106           tremolo_->set_property ("flag-count", scm_from_int (tremolo_flags));
107           tremolo_->set_parent (stem_, X_AXIS);
108           stem_->set_object ("tremolo-flag", tremolo_->self_scm ());
109           tremolo_->set_object ("stem", stem_->self_scm ());
110         }
111     }
112 }
113
114 void
115 Stem_engraver::acknowledge_rhythmic_head (Grob_info gi)
116 {
117   if (Rhythmic_head::get_stem (gi.grob ()))
118     return;
119
120   Stream_event *cause = gi.event_cause ();
121   if (!cause)
122     return;
123   Duration *d = unsmob_duration (cause->get_property ("duration"));
124   if (!d)
125     return;
126
127   if (!stem_)
128     make_stem (gi);
129
130   if (Stem::duration_log (stem_) != d->duration_log ())
131     {
132       // FIXME: 
133       gi.event_cause ()->origin ()->warning (_f ("adding note head to incompatible stem (type = %d)",
134                                                  1 << Stem::duration_log (stem_)));
135       gi.event_cause ()->origin ()->warning (_f ("maybe input should specify polyphonic voices"));
136     }
137
138   Stem::add_head (stem_, gi.grob ());
139 }
140
141 void
142 Stem_engraver::stop_translation_timestep ()
143 {
144   tremolo_ = 0;
145   if (stem_)
146     {
147       /* FIXME: junk these properties.  */
148       SCM prop = get_property ("stemLeftBeamCount");
149       if (scm_is_number (prop))
150         {
151           Stem::set_beaming (stem_, scm_to_int (prop), LEFT);
152           context ()->unset_property (ly_symbol2scm ("stemLeftBeamCount"));
153         }
154       prop = get_property ("stemRightBeamCount");
155       if (scm_is_number (prop))
156         {
157           Stem::set_beaming (stem_, scm_to_int (prop), RIGHT);
158           context ()->unset_property (ly_symbol2scm ("stemRightBeamCount"));
159         }
160       stem_ = 0;
161     }
162   tremolo_ev_ = 0;
163 }
164
165 IMPLEMENT_TRANSLATOR_LISTENER (Stem_engraver, tremolo);
166 void
167 Stem_engraver::listen_tremolo (Stream_event *ev)
168 {
169   ASSIGN_EVENT_ONCE (tremolo_ev_, ev);
170 }
171
172 ADD_ACKNOWLEDGER (Stem_engraver, rhythmic_head);
173
174 ADD_TRANSLATOR (Stem_engraver,
175                 /* doc */ "Create stems and single-stem tremolos.  It also works together with "
176                 "the beam engraver for overriding beaming.",
177                 /* create */
178                 "Stem "
179                 "StemTremolo ",
180                 /* accept */ "tremolo-event",
181                 /* read */
182                 "tremoloFlags "
183                 "stemLeftBeamCount "
184                 "stemRightBeamCount ",
185                 /* write */ "");