]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-engraver.cc
Merge branch 'lilypond/translation' into staging
[lilypond.git] / lily / beam-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2012 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 "beam.hh"
21 #include "beaming-pattern.hh"
22 #include "context.hh"
23 #include "directional-element-interface.hh"
24 #include "drul-array.hh"
25 #include "duration.hh"
26 #include "engraver.hh"
27 #include "international.hh"
28 #include "item.hh"
29 #include "rest.hh"
30 #include "spanner.hh"
31 #include "stream-event.hh"
32 #include "stem.hh"
33 #include "warn.hh"
34
35 #include "translator.icc"
36
37 class Beam_engraver : public Engraver
38 {
39 public:
40   DECLARE_ACKNOWLEDGER (stem);
41   DECLARE_ACKNOWLEDGER (rest);
42
43 protected:
44   Stream_event *start_ev_;
45
46   Spanner *finished_beam_;
47   Spanner *beam_;
48   Stream_event *prev_start_ev_;
49
50   Stream_event *stop_ev_;
51
52   Direction forced_direction_;
53
54   Beaming_pattern *beam_info_;
55   Beaming_pattern *finished_beam_info_;
56
57   /// location  within measure where beam started.
58   Moment beam_start_location_;
59
60   /// moment (global time) where beam started.
61   Moment beam_start_mom_;
62
63   Beaming_options beaming_options_;
64   Beaming_options finished_beaming_options_;
65
66   void typeset_beam ();
67   void set_melisma (bool);
68
69   Moment last_stem_added_at_;
70   void stop_translation_timestep ();
71   void start_translation_timestep ();
72   virtual void finalize ();
73
74   void process_music ();
75
76   virtual bool valid_start_point ();
77   virtual bool valid_end_point ();
78
79   DECLARE_TRANSLATOR_LISTENER (beam);
80 public:
81   TRANSLATOR_DECLARATIONS (Beam_engraver);
82 };
83
84 /*
85   Hmm. this isn't necessary, since grace beams and normal beams are
86   always nested.
87 */
88 bool
89 Beam_engraver::valid_start_point ()
90 {
91   Moment n = now_mom ();
92
93   return n.grace_part_ == Rational (0);
94 }
95
96 bool
97 Beam_engraver::valid_end_point ()
98 {
99   return valid_start_point ();
100 }
101
102 Beam_engraver::Beam_engraver ()
103 {
104   beam_ = 0;
105   finished_beam_ = 0;
106   finished_beam_info_ = 0;
107   beam_info_ = 0;
108   forced_direction_ = CENTER;
109   stop_ev_ = 0;
110   start_ev_ = 0;
111   prev_start_ev_ = 0;
112 }
113
114 IMPLEMENT_TRANSLATOR_LISTENER (Beam_engraver, beam);
115 void
116 Beam_engraver::listen_beam (Stream_event *ev)
117 {
118   Direction d = to_dir (ev->get_property ("span-direction"));
119
120   if (d == START && valid_start_point ())
121     {
122       ASSIGN_EVENT_ONCE (start_ev_, ev);
123
124       Direction updown = to_dir (ev->get_property ("direction"));
125       if (updown)
126         forced_direction_ = updown;
127     }
128   else if (d == STOP && valid_end_point ())
129     ASSIGN_EVENT_ONCE (stop_ev_, ev);
130 }
131
132 void
133 Beam_engraver::set_melisma (bool ml)
134 {
135   SCM b = get_property ("autoBeaming");
136   if (!to_boolean (b))
137     context ()->set_property ("beamMelismaBusy", ml ? SCM_BOOL_T : SCM_BOOL_F);
138 }
139
140 void
141 Beam_engraver::process_music ()
142 {
143   if (start_ev_)
144     {
145       if (beam_)
146         {
147           start_ev_->origin ()->warning (_ ("already have a beam"));
148           return;
149         }
150
151       set_melisma (true);
152       prev_start_ev_ = start_ev_;
153       beam_ = make_spanner ("Beam", start_ev_->self_scm ());
154
155       Moment mp (robust_scm2moment (get_property ("measurePosition"),
156                                     Moment (0)));
157
158       beam_start_location_ = mp;
159       beam_start_mom_ = now_mom ();
160
161       beaming_options_.from_context (context ());
162       beam_info_ = new Beaming_pattern;
163       /* urg, must copy to Auto_beam_engraver too */
164     }
165
166   typeset_beam ();
167   if (stop_ev_ && beam_)
168     {
169       announce_end_grob (beam_, stop_ev_->self_scm ());
170
171     }
172 }
173
174 void
175 Beam_engraver::typeset_beam ()
176 {
177   if (finished_beam_)
178     {
179       if (!finished_beam_->get_bound (RIGHT))
180         finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
181       if (forced_direction_)
182         {
183           Grob *stem = finished_beam_->get_bound (RIGHT);
184           set_grob_direction (stem, forced_direction_);
185           forced_direction_ = CENTER;
186         }
187       finished_beam_info_->beamify (finished_beaming_options_);
188
189       Beam::set_beaming (finished_beam_, finished_beam_info_);
190
191       delete finished_beam_info_;
192       finished_beam_info_ = 0;
193       finished_beam_ = 0;
194
195     }
196 }
197
198 void
199 Beam_engraver::start_translation_timestep ()
200 {
201   start_ev_ = 0;
202
203   if (beam_)
204     set_melisma (true);
205 }
206
207 void
208 Beam_engraver::stop_translation_timestep ()
209 {
210   if (stop_ev_)
211     {
212       finished_beam_ = beam_;
213       finished_beam_info_ = beam_info_;
214       finished_beaming_options_ = beaming_options_;
215
216       stop_ev_ = 0;
217       beam_ = 0;
218       beam_info_ = 0;
219       typeset_beam ();
220       set_melisma (false);
221     }
222 }
223
224 void
225 Beam_engraver::finalize ()
226 {
227   typeset_beam ();
228   if (beam_)
229     {
230       prev_start_ev_->origin ()->warning (_ ("unterminated beam"));
231
232       /*
233         we don't typeset it, (we used to, but it was commented
234         out. Reason unknown) */
235       beam_->suicide ();
236       delete beam_info_;
237     }
238 }
239
240 void
241 Beam_engraver::acknowledge_rest (Grob_info info)
242 {
243   if (beam_
244       && !scm_is_number (info.grob ()->get_property_data ("staff-position")))
245     chain_offset_callback (info.grob (),
246                            Beam::rest_collision_callback_proc, Y_AXIS);
247 }
248
249 void
250 Beam_engraver::acknowledge_stem (Grob_info info)
251 {
252   if (!beam_)
253     return;
254
255   Moment now = now_mom ();
256   if (!valid_start_point ())
257     return;
258
259   Item *stem = dynamic_cast<Item *> (info.grob ());
260   if (Stem::get_beam (stem))
261     return;
262
263   Stream_event *ev = info.ultimate_event_cause ();
264   if (!ev->in_event_class ("rhythmic-event"))
265     {
266       info.grob ()->warning (_ ("stem must have Rhythmic structure"));
267       return;
268     }
269
270   last_stem_added_at_ = now;
271
272   Duration *stem_duration = unsmob_duration (ev->get_property ("duration"));
273   int durlog = stem_duration->duration_log ();
274   //int durlog = unsmob_duration (ev->get_property ("duration"))->duration_log ();
275   if (durlog <= 2)
276     {
277       ev->origin ()->warning (_ ("stem does not fit in beam"));
278       prev_start_ev_->origin ()->warning (_ ("beam was started here"));
279       /*
280         don't return, since
281
282         [r4 c8] can just as well be modern notation.
283       */
284     }
285
286   if (forced_direction_)
287     set_grob_direction (stem, forced_direction_);
288
289   stem->set_property ("duration-log", scm_from_int (durlog));
290   Moment stem_location = now - beam_start_mom_ + beam_start_location_;
291   beam_info_->add_stem (stem_location,
292                         max (durlog - 2, 0),
293                         Stem::is_invisible (stem),
294                         stem_duration->factor ());
295   Beam::add_stem (beam_, stem);
296 }
297
298 ADD_ACKNOWLEDGER (Beam_engraver, stem);
299 ADD_ACKNOWLEDGER (Beam_engraver, rest);
300
301 ADD_TRANSLATOR (Beam_engraver,
302                 /* doc */
303                 "Handle @code{Beam} events by engraving beams.  If omitted,"
304                 " then notes are printed with flags instead of beams.",
305
306                 /* create */
307                 "Beam ",
308
309                 /* read */
310                 "baseMoment "
311                 "beamMelismaBusy "
312                 "beatStructure "
313                 "subdivideBeams ",
314
315                 /* write */
316                 "forbidBreak"
317                );
318
319 class Grace_beam_engraver : public Beam_engraver
320 {
321 public:
322   TRANSLATOR_DECLARATIONS (Grace_beam_engraver);
323
324   DECLARE_TRANSLATOR_LISTENER (beam);
325
326 protected:
327   virtual bool valid_start_point ();
328   virtual bool valid_end_point ();
329 };
330
331 Grace_beam_engraver::Grace_beam_engraver ()
332 {
333 }
334
335 bool
336 Grace_beam_engraver::valid_start_point ()
337 {
338   Moment n = now_mom ();
339
340   return n.grace_part_ != Rational (0);
341 }
342
343 bool
344 Grace_beam_engraver::valid_end_point ()
345 {
346   return beam_ && valid_start_point ();
347 }
348
349 /*
350   Ugh, C&P code.
351  */
352 IMPLEMENT_TRANSLATOR_LISTENER (Grace_beam_engraver, beam);
353 void
354 Grace_beam_engraver::listen_beam (Stream_event *ev)
355 {
356   Direction d = to_dir (ev->get_property ("span-direction"));
357
358   if (d == START && valid_start_point ())
359     start_ev_ = ev;
360   else if (d == STOP && valid_end_point ())
361     stop_ev_ = ev;
362 }
363
364 ADD_ACKNOWLEDGER (Grace_beam_engraver, stem);
365 ADD_ACKNOWLEDGER (Grace_beam_engraver, rest);
366
367 ADD_TRANSLATOR (Grace_beam_engraver,
368                 /* doc */
369                 "Handle @code{Beam} events by engraving beams.  If omitted,"
370                 " then notes are printed with flags instead of beams.  Only"
371                 " engraves beams when we are at grace points in time.",
372
373                 /* create */
374                 "Beam ",
375
376                 /* read */
377                 "baseMoment "
378                 "beamMelismaBusy "
379                 "beatStructure "
380                 "subdivideBeams ",
381
382                 /* write */
383                 ""
384                );
385