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