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