]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-engraver.cc
Issue 4997/2: Use Preinit class in Scheme_engraver
[lilypond.git] / lily / beam-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2015 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 "unpure-pure-container.hh"
34 #include "warn.hh"
35
36 #include "translator.icc"
37
38 class Beam_engraver : public Engraver
39 {
40 public:
41   void acknowledge_stem (Grob_info);
42   void acknowledge_rest (Grob_info);
43   void listen_beam (Stream_event *);
44
45 protected:
46   Stream_event *start_ev_;
47
48   Spanner *finished_beam_;
49   Spanner *beam_;
50   Stream_event *prev_start_ev_;
51
52   Stream_event *stop_ev_;
53
54   Direction forced_direction_;
55
56   Beaming_pattern *beam_info_;
57   Beaming_pattern *finished_beam_info_;
58
59   /// location  within measure where beam started.
60   Moment beam_start_location_;
61
62   /// moment (global time) where beam started.
63   Moment beam_start_mom_;
64
65   Beaming_options beaming_options_;
66   Beaming_options finished_beaming_options_;
67
68   void typeset_beam ();
69   void set_melisma (bool);
70
71   Moment last_stem_added_at_;
72   void stop_translation_timestep ();
73   void start_translation_timestep ();
74   virtual void finalize ();
75
76   void process_music ();
77
78   virtual bool valid_start_point ();
79   virtual bool valid_end_point ();
80
81 public:
82   TRANSLATOR_DECLARATIONS (Beam_engraver);
83 };
84
85 /*
86   Hmm. this isn't necessary, since grace beams and normal beams are
87   always nested.
88 */
89 bool
90 Beam_engraver::valid_start_point ()
91 {
92   Moment n = now_mom ();
93
94   return n.grace_part_ == Rational (0);
95 }
96
97 bool
98 Beam_engraver::valid_end_point ()
99 {
100   return valid_start_point ();
101 }
102
103 Beam_engraver::Beam_engraver ()
104 {
105   beam_ = 0;
106   finished_beam_ = 0;
107   finished_beam_info_ = 0;
108   beam_info_ = 0;
109   forced_direction_ = CENTER;
110   stop_ev_ = 0;
111   start_ev_ = 0;
112   prev_start_ev_ = 0;
113 }
114
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       Grob *stem = finished_beam_->get_bound (RIGHT);
180       if (!stem)
181         {
182           stem = finished_beam_->get_bound (LEFT);
183           if (stem)
184             finished_beam_->set_bound (RIGHT, stem);
185         }
186
187       if (stem && forced_direction_)
188         set_grob_direction (stem, forced_direction_);
189
190       forced_direction_ = CENTER;
191       finished_beam_info_->beamify (finished_beaming_options_);
192
193       Beam::set_beaming (finished_beam_, finished_beam_info_);
194
195       delete finished_beam_info_;
196       finished_beam_info_ = 0;
197       finished_beam_ = 0;
198
199     }
200 }
201
202 void
203 Beam_engraver::start_translation_timestep ()
204 {
205   start_ev_ = 0;
206
207   if (beam_)
208     set_melisma (true);
209 }
210
211 void
212 Beam_engraver::stop_translation_timestep ()
213 {
214   if (stop_ev_)
215     {
216       finished_beam_ = beam_;
217       finished_beam_info_ = beam_info_;
218       finished_beaming_options_ = beaming_options_;
219
220       stop_ev_ = 0;
221       beam_ = 0;
222       beam_info_ = 0;
223       typeset_beam ();
224       set_melisma (false);
225     }
226 }
227
228 void
229 Beam_engraver::finalize ()
230 {
231   typeset_beam ();
232   if (beam_)
233     {
234       prev_start_ev_->origin ()->warning (_ ("unterminated beam"));
235
236       /*
237         we don't typeset it, (we used to, but it was commented
238         out. Reason unknown) */
239       beam_->suicide ();
240       delete beam_info_;
241     }
242 }
243
244 void
245 Beam_engraver::acknowledge_rest (Grob_info info)
246 {
247   if (beam_
248       && !scm_is_number (info.grob ()->get_property_data ("staff-position")))
249     chain_offset_callback (info.grob (),
250                            Unpure_pure_container::make_smob
251                              (Beam::rest_collision_callback_proc,
252                               Beam::pure_rest_collision_callback_proc),
253                            Y_AXIS);
254 }
255
256 void
257 Beam_engraver::acknowledge_stem (Grob_info info)
258 {
259   if (!beam_)
260     return;
261
262   Moment now = now_mom ();
263   if (!valid_start_point ())
264     return;
265
266   // It's suboptimal that we don't support callbacks returning ##f,
267   // but this makes beams have no effect on "stems" reliably in
268   // TabStaff when \tabFullNotation is switched off: the real stencil
269   // callback for beams is called quite late in the process, and we
270   // don't want to trigger it early.
271   if (scm_is_false (beam_->get_property_data ("stencil")))
272     return;
273
274   Item *stem = dynamic_cast<Item *> (info.grob ());
275   if (Stem::get_beam (stem))
276     return;
277
278   Stream_event *ev = info.ultimate_event_cause ();
279   if (!ev->in_event_class ("rhythmic-event"))
280     {
281       info.grob ()->warning (_ ("stem must have Rhythmic structure"));
282       return;
283     }
284
285   last_stem_added_at_ = now;
286
287   Duration *stem_duration = unsmob<Duration> (ev->get_property ("duration"));
288   int durlog = stem_duration->duration_log ();
289   //int durlog = unsmob<Duration> (ev->get_property ("duration"))->duration_log ();
290   if (durlog <= 2)
291     {
292       ev->origin ()->warning (_ ("stem does not fit in beam"));
293       prev_start_ev_->origin ()->warning (_ ("beam was started here"));
294       /*
295         don't return, since
296
297         [r4 c8] can just as well be modern notation.
298       */
299     }
300
301   if (forced_direction_)
302     set_grob_direction (stem, forced_direction_);
303
304   stem->set_property ("duration-log", scm_from_int (durlog));
305   Moment stem_location = now - beam_start_mom_ + beam_start_location_;
306   beam_info_->add_stem (stem_location,
307                         max (durlog - 2, 0),
308                         Stem::is_invisible (stem),
309                         stem_duration->factor (),
310                         (to_boolean (stem->get_property ("tuplet-start"))));
311   Beam::add_stem (beam_, stem);
312 }
313
314
315 void
316 Beam_engraver::boot ()
317 {
318   ADD_LISTENER (Beam_engraver, beam);
319   ADD_ACKNOWLEDGER (Beam_engraver, stem);
320   ADD_ACKNOWLEDGER (Beam_engraver, rest);
321 }
322
323 ADD_TRANSLATOR (Beam_engraver,
324                 /* doc */
325                 "Handle @code{Beam} events by engraving beams.  If omitted,"
326                 " then notes are printed with flags instead of beams.",
327
328                 /* create */
329                 "Beam ",
330
331                 /* read */
332                 "baseMoment "
333                 "beamMelismaBusy "
334                 "beatStructure "
335                 "subdivideBeams ",
336
337                 /* write */
338                 "forbidBreak"
339                );
340
341 class Grace_beam_engraver : public Beam_engraver
342 {
343 public:
344   TRANSLATOR_DECLARATIONS (Grace_beam_engraver);
345   TRANSLATOR_INHERIT (Beam_engraver);
346
347 protected:
348   virtual bool valid_start_point ();
349   virtual bool valid_end_point ();
350 };
351
352 Grace_beam_engraver::Grace_beam_engraver ()
353 {
354 }
355
356 bool
357 Grace_beam_engraver::valid_start_point ()
358 {
359   Moment n = now_mom ();
360
361   return n.grace_part_ != Rational (0);
362 }
363
364 bool
365 Grace_beam_engraver::valid_end_point ()
366 {
367   return beam_ && valid_start_point ();
368 }
369
370 void
371 Grace_beam_engraver::boot ()
372 {
373   ADD_LISTENER (Grace_beam_engraver, beam);
374   ADD_ACKNOWLEDGER (Grace_beam_engraver, stem);
375   ADD_ACKNOWLEDGER (Grace_beam_engraver, rest);
376 }
377
378 ADD_TRANSLATOR (Grace_beam_engraver,
379                 /* doc */
380                 "Handle @code{Beam} events by engraving beams.  If omitted,"
381                 " then notes are printed with flags instead of beams.  Only"
382                 " engraves beams when we are at grace points in time.",
383
384                 /* create */
385                 "Beam ",
386
387                 /* read */
388                 "baseMoment "
389                 "beamMelismaBusy "
390                 "beatStructure "
391                 "subdivideBeams ",
392
393                 /* write */
394                 ""
395                );
396