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