]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-engraver.cc
release: 1.3.94
[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--2000 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   Spanner *finished_beam_p_;
27   Spanner *beam_p_;
28   Span_req * prev_start_req_;
29
30   Beaming_info_list * beam_info_p_;
31   Beaming_info_list * finished_beam_info_p_;  
32
33   /// location  within measure where beam started.
34   Moment beam_start_location_;
35
36   /// moment (global time) where beam started.
37   Moment beam_start_mom_;
38   
39   void typeset_beam ();
40   void set_melisma (bool);
41 protected:
42   virtual void do_pre_move_processing ();
43   virtual void do_post_move_processing ();
44   virtual void do_removal_processing ();
45   virtual void acknowledge_element (Score_element_info);
46   virtual bool do_try_music (Music*);
47   virtual void do_process_music ();
48 public:
49   Beam_engraver ();
50   VIRTUAL_COPY_CONS (Translator);
51 };
52
53
54 Beam_engraver::Beam_engraver ()
55 {
56   beam_p_ = 0;
57   finished_beam_p_ =0;
58   finished_beam_info_p_=0;
59   beam_info_p_ =0;
60   reqs_drul_[LEFT] = reqs_drul_[RIGHT] =0;
61   prev_start_req_ =0;
62 }
63
64 bool
65 Beam_engraver::do_try_music (Music *m)
66 {
67   if (Span_req * c = dynamic_cast<Span_req*>(m))
68     {
69       if (c->span_type_str_ == "abort")
70         {
71           reqs_drul_[START] = 0;
72           reqs_drul_[STOP] = 0;
73           if (beam_p_)
74             beam_p_->suicide ();
75           beam_p_ = 0;
76         }
77       else if (c->span_type_str_ == "beam")
78         {
79       
80           Direction d =c->span_dir_;
81
82           if (d == STOP && !beam_p_)
83             {
84               m->origin ()->warning  (_ ("can't find start of beam"));
85               return false;
86             }
87
88           if(d == STOP)
89             {
90               SCM m = get_property ("automaticMelismata");
91               SCM b = get_property("noAutoBeaming");
92               if (to_boolean (m) && to_boolean(b))
93                 {
94                   set_melisma (false);
95                 }
96             }
97
98           reqs_drul_[d ] = c;
99           return true;
100         }
101     }
102   return false;
103 }
104
105 void
106 Beam_engraver::set_melisma (bool m)
107 {
108   daddy_trans_l_->set_property ("beamMelismaBusy", m ? SCM_BOOL_T :SCM_BOOL_F);
109 }
110
111
112 void
113 Beam_engraver::do_process_music ()
114 {
115   if (reqs_drul_[STOP])
116     {
117       if (!beam_p_)
118         reqs_drul_[STOP]->origin ()->warning (_("can't find start of beam"));
119       prev_start_req_ =0;
120       finished_beam_p_ = beam_p_;
121       finished_beam_info_p_ = beam_info_p_;
122
123       beam_info_p_ =0;
124       beam_p_ = 0;
125     }
126
127
128   if (beam_p_  &&  !to_boolean (get_property ("weAreGraceContext")))
129     {
130       Score_engraver * e = 0;
131       Translator * t  =  daddy_grav_l ();
132       for (; !e && t;  t = t->daddy_trans_l_)
133         {
134           e = dynamic_cast<Score_engraver*> (t);
135         }
136       
137       if (!e)
138         programming_error ("No score engraver!");
139       else
140         e->forbid_breaks ();
141     }
142   
143   if (reqs_drul_[START])
144     {
145       if (beam_p_)
146         {
147           reqs_drul_[START]->origin ()->warning (_ ("already have a beam"));
148           return;
149         }
150
151       prev_start_req_ = reqs_drul_[START];
152       beam_p_ = new Spanner (get_property ("Beam"));
153       Beam::set_interface (beam_p_);
154       
155       SCM smp = get_property ("measurePosition");
156       Moment mp =  (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
157
158       beam_start_location_ = mp;
159       beam_start_mom_ = now_mom();
160       beam_info_p_ = new Beaming_info_list;
161       
162       
163       /* urg, must copy to Auto_beam_engraver too */
164  
165       announce_element (beam_p_, reqs_drul_[START]);
166     }
167 }
168
169 void
170 Beam_engraver::typeset_beam ()
171 {
172   if (finished_beam_p_)
173     {
174       finished_beam_info_p_->beamify ();
175       
176       Beam::set_beaming (finished_beam_p_, finished_beam_info_p_);
177       typeset_element (finished_beam_p_);
178       delete finished_beam_info_p_;
179       finished_beam_info_p_ =0;
180       finished_beam_p_ = 0;
181     
182       reqs_drul_[STOP] = 0;
183     }
184 }
185
186 void
187 Beam_engraver::do_post_move_processing ()
188 {
189   reqs_drul_ [START] =0;
190   if(beam_p_) {
191     SCM m = get_property ("automaticMelismata");
192     SCM b = get_property("noAutoBeaming");
193     if (to_boolean (m) && to_boolean(b)) {
194       set_melisma (true);
195     }
196   }
197 }
198
199 void
200 Beam_engraver::do_pre_move_processing ()
201 {
202   typeset_beam ();
203 }
204
205 void
206 Beam_engraver::do_removal_processing ()
207 {
208   typeset_beam ();
209   if (beam_p_)
210     {
211       prev_start_req_->origin ()->warning (_ ("unterminated beam"));
212 #if 0
213       finished_beam_p_ = beam_p_;
214       finished_beam_info_p_ = beam_info_p_;
215       typeset_beam ();
216 #else
217       beam_p_->suicide ();
218       delete beam_info_p_;
219 #endif
220     }
221 }
222
223 void
224 Beam_engraver::acknowledge_element (Score_element_info info)
225 {
226   if (beam_p_)
227     {
228       if (Rest::has_interface (info.elem_l_))
229         {
230           info.elem_l_->add_offset_callback (Beam::rest_collision_callback_proc, Y_AXIS);
231         }
232       else if (Stem::has_interface (info.elem_l_))
233         {
234           Item *stem_l = dynamic_cast<Item*> (info.elem_l_);
235           if (Stem::beam_l (stem_l))
236             return;
237
238           bool stem_grace = stem_l->get_elt_property ("grace") == SCM_BOOL_T;
239
240           SCM wg =get_property ("weAreGraceContext");
241           bool wgb= to_boolean (wg);
242
243           if (wgb!= stem_grace)
244             return;
245
246           Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_);
247           if (!rhythmic_req)
248             {
249               String s = _ ("stem must have Rhythmic structure");
250               if (info.req_l_)
251                 info.req_l_->origin ()->warning (s);
252               else
253                 ::warning (s);
254           
255               return;
256             }
257
258           if (rhythmic_req->duration_.durlog_i_<= 2)
259             {
260               rhythmic_req->origin ()->warning (_ ("stem doesn't fit in beam"));
261               prev_start_req_->origin ()->warning (_ ("beam was started here"));
262               /*
263                 don't return, since
264
265                 [r4 c8] can just as well be modern notation.
266               */
267             }
268
269           stem_l->set_elt_property ("duration-log",
270                                     gh_int2scm (rhythmic_req->duration_.durlog_i_));
271           Moment stem_location = now_mom () - beam_start_mom_ + beam_start_location_;
272           beam_info_p_->add_stem (stem_location,
273                                   (rhythmic_req->duration_.durlog_i_ - 2) >? 1);
274           Beam::add_stem (beam_p_, stem_l);
275         }
276     }
277 }
278
279
280
281 ADD_THIS_TRANSLATOR(Beam_engraver);
282