]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-engraver.cc
b9e7eb63fec08cb961c5221795ca05b9f49d9196
[lilypond.git] / lily / beam-engraver.cc
1 /*   
2   beam-engraver.cc --  implement Beam_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include "engraver-group-engraver.hh"
10 #include "engraver.hh"
11 #include "musical-request.hh"
12 #include "beam.hh"
13 #include "stem.hh"
14 #include "warn.hh"
15 #include "beaming.hh"
16 #include "score-engraver.hh"
17 #include "rest.hh"
18 #include "drul-array.hh"
19 #include "item.hh"
20 #include "spanner.hh"
21
22 class Beam_engraver : public Engraver
23 {
24   Drul_array<Span_req*> reqs_drul_;
25
26   Link_array<Stem> stems_;
27   
28   
29   Spanner *finished_beam_p_;
30   Spanner *beam_p_;
31   Span_req * prev_start_req_;
32
33   Beaming_info_list * beam_info_p_;
34   Beaming_info_list * finished_beam_info_p_;  
35
36   /// location  within measure where beam started.
37   Moment beam_start_location_;
38
39   /// moment (global time) where beam started.
40   Moment beam_start_mom_;
41   
42   void typeset_beam ();
43   void set_melisma (bool);
44 protected:
45   virtual void stop_translation_timestep ();
46   virtual void start_translation_timestep ();
47   virtual void finalize ();
48   virtual void create_grobs ();
49   virtual void acknowledge_grob (Grob_info);
50   virtual bool try_music (Music*);
51   virtual void process_music ();
52
53 public:
54   TRANSLATOR_DECLARATIONS(  Beam_engraver );
55 };
56
57
58 Beam_engraver::Beam_engraver ()
59 {
60   beam_p_ = 0;
61   finished_beam_p_ =0;
62   finished_beam_info_p_=0;
63   beam_info_p_ =0;
64   reqs_drul_[LEFT] = reqs_drul_[RIGHT] =0;
65   prev_start_req_ =0;
66 }
67
68 bool
69 Beam_engraver::try_music (Music *m)
70 {
71   if (Span_req * c = dynamic_cast<Span_req*> (m))
72     {
73       if (scm_equal_p (c->get_mus_property ("span-type"),
74                        ly_str02scm ("abort")) == SCM_BOOL_T)
75         {
76           reqs_drul_[START] = 0;
77           reqs_drul_[STOP] = 0;
78           if (beam_p_)
79             beam_p_->suicide ();
80           beam_p_ = 0;
81         }
82       else if (scm_equal_p (c->get_mus_property ("span-type"),
83                        ly_str02scm ("beam")) == SCM_BOOL_T)
84         {
85       
86           Direction d =c->get_span_dir ();
87
88           if (d == STOP && !beam_p_)
89             {
90               m->origin ()->warning (_ ("can't find start of beam"));
91               return false;
92             }
93
94           if (d == STOP)
95             {
96               SCM m = get_property ("automaticMelismata");
97               SCM b = get_property ("noAutoBeaming");
98               if (to_boolean (m) && to_boolean (b))
99                 {
100                   set_melisma (false);
101                 }
102             }
103
104           reqs_drul_[d ] = c;
105           return true;
106         }
107     }
108   return false;
109 }
110
111 void
112 Beam_engraver::set_melisma (bool m)
113 {
114   daddy_trans_l_->set_property ("beamMelismaBusy", m ? SCM_BOOL_T :SCM_BOOL_F);
115 }
116
117 void
118 Beam_engraver::process_music ()
119 {
120   if (reqs_drul_[STOP])
121     {
122       if (!beam_p_)
123         reqs_drul_[STOP]->origin ()->warning (_ ("can't find start of beam"));
124       prev_start_req_ =0;
125       finished_beam_p_ = beam_p_;
126       finished_beam_info_p_ = beam_info_p_;
127
128       beam_info_p_ =0;
129       beam_p_ = 0;
130     }
131
132
133   if (beam_p_)
134     {
135       Score_engraver * e = 0;
136       Translator * t  =  daddy_grav_l ();
137       for (; !e && t;  t = t->daddy_trans_l_)
138         {
139           e = dynamic_cast<Score_engraver*> (t);
140         }
141       
142       if (!e)
143         programming_error ("No score engraver!");
144       else
145         e->forbid_breaks ();
146     }
147 }
148
149
150 void
151 Beam_engraver::create_grobs ()
152 {
153   if (reqs_drul_[START])
154     {
155       if (beam_p_)
156         {
157           reqs_drul_[START]->origin ()->warning (_ ("already have a beam"));
158           return;
159         }
160
161       prev_start_req_ = reqs_drul_[START];
162       beam_p_ = new Spanner (get_property ("Beam"));
163       SCM smp = get_property ("measurePosition");
164       Moment mp = (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
165
166       beam_start_location_ = mp;
167       beam_start_mom_ = now_mom ();
168       
169       beam_info_p_ = new Beaming_info_list;
170       
171       /* urg, must copy to Auto_beam_engraver too */
172  
173       announce_grob (beam_p_, reqs_drul_[START]);
174     }
175   reqs_drul_[STOP] = 0;
176   reqs_drul_[START] = 0;
177 }
178
179 void
180 Beam_engraver::typeset_beam ()
181 {
182   if (finished_beam_p_)
183     {
184       finished_beam_info_p_->beamify(*unsmob_moment (get_property ("beatLength")),
185                                      (bool)gh_scm2bool(get_property("subdivideBeams")));
186
187       Beam::set_beaming (finished_beam_p_, finished_beam_info_p_);
188       typeset_grob (finished_beam_p_);
189       delete finished_beam_info_p_;
190       finished_beam_info_p_ =0;
191       finished_beam_p_ = 0;
192     
193       reqs_drul_[STOP] = 0;
194     }
195 }
196
197 void
198 Beam_engraver::start_translation_timestep ()
199 {
200   reqs_drul_ [START] =0;
201   if (beam_p_) {
202     SCM m = get_property ("automaticMelismata");
203     SCM b = get_property ("noAutoBeaming");
204     if (to_boolean (m) && to_boolean (b)) {
205       set_melisma (true);
206     }
207   }
208 }
209
210 void
211 Beam_engraver::stop_translation_timestep ()
212 {
213   typeset_beam ();
214 }
215
216 void
217 Beam_engraver::finalize ()
218 {
219   typeset_beam ();
220   if (beam_p_)
221     {
222       prev_start_req_->origin ()->warning (_ ("unterminated beam"));
223
224       /*
225         we don't typeset it, (we used to, but it was commented
226         out. Reason unknown) */
227       beam_p_->suicide ();
228       delete beam_info_p_;
229     }
230 }
231
232 void
233 Beam_engraver::acknowledge_grob (Grob_info info)
234 {
235   if (beam_p_)
236     {
237       if (Rest::has_interface (info.grob_l_))
238         {
239           info.grob_l_->add_offset_callback (Beam::rest_collision_callback_proc, Y_AXIS);
240         }
241       else if (Stem::has_interface (info.grob_l_))
242         {
243           Moment now = now_mom();
244
245           if(bool (now.grace_part_ ) != bool (beam_start_mom_.grace_part_))
246             return ;
247           
248           Item *stem_l = dynamic_cast<Item*> (info.grob_l_);
249           if (Stem::beam_l (stem_l))
250             return;
251
252           Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_);
253           if (!rhythmic_req)
254             {
255               String s = _ ("stem must have Rhythmic structure");
256               if (info.req_l_)
257                 info.req_l_->origin ()->warning (s);
258               else
259                 ::warning (s);
260           
261               return;
262             }
263
264       int durlog  = unsmob_duration (rhythmic_req->get_mus_property ("duration"))-> duration_log ();
265           if (durlog <= 2)
266             {
267               rhythmic_req->origin ()->warning (_ ("stem doesn't fit in beam"));
268               prev_start_req_->origin ()->warning (_ ("beam was started here"));
269               /*
270                 don't return, since
271
272                 [r4 c8] can just as well be modern notation.
273               */
274             }
275
276           stem_l->set_grob_property ("duration-log",
277                                     gh_int2scm (durlog));
278           Moment stem_location = now - beam_start_mom_ + beam_start_location_;
279           beam_info_p_->add_stem (stem_location,
280  (durlog- 2) >? 1);
281           Beam::add_stem (beam_p_, stem_l);
282         }
283     }
284 }
285
286
287
288
289
290 ENTER_DESCRIPTION(Beam_engraver,
291 /* descr */       "Handles Beam_requests by engraving Beams.    If omitted, then notes will be
292 printed with flags instead of beams.",
293 /* creats*/       "Beam",
294 /* acks  */       "stem-interface rest-interface",
295 /* reads */       "beamMelismaBusy",
296 /* write */       "");