]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-engraver.cc
(stop_translation_timestep): don't
[lilypond.git] / lily / spacing-engraver.cc
1 /*
2   spacing-engraver.cc -- implement Spacing_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1999--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "engraver.hh"
10 #include "note-spacing.hh"
11 #include "paper-column.hh"
12 #include "pointer-group-interface.hh"
13 #include "pqueue.hh"
14 #include "spanner.hh"
15 #include "staff-spacing.hh"
16 #include "stream-event.hh"
17
18 #include "translator.icc"
19
20 struct Rhythmic_tuple
21 {
22   Grob_info info_;
23   Moment end_;
24
25   Rhythmic_tuple ()
26   {
27   }
28   Rhythmic_tuple (Grob_info i, Moment m)
29   {
30     info_ = i;
31     end_ = m;
32   }
33   static int time_compare (Rhythmic_tuple const &, Rhythmic_tuple const &);
34 };
35
36 inline int
37 compare (Rhythmic_tuple const &a, Rhythmic_tuple const &b)
38 {
39   return Rhythmic_tuple::time_compare (a, b);
40 }
41
42 int
43 Rhythmic_tuple::time_compare (Rhythmic_tuple const &h1,
44                               Rhythmic_tuple const &h2)
45 {
46   return (h1.end_ - h2.end_).main_part_.sign ();
47 }
48
49 /****************************************************************/
50
51 /*
52   Acknowledge rhythmic elements, for initializing spacing fields in
53   the columns.
54 */
55 class Spacing_engraver : public Engraver
56 {
57   PQueue<Rhythmic_tuple> playing_durations_;
58   vector<Rhythmic_tuple> now_durations_;
59   vector<Rhythmic_tuple> stopped_durations_;
60   Moment now_;
61   Spanner *spacing_;
62   Stream_event *start_section_;
63   
64   TRANSLATOR_DECLARATIONS (Spacing_engraver);
65
66 protected:
67   DECLARE_ACKNOWLEDGER (staff_spacing);
68   DECLARE_ACKNOWLEDGER (note_spacing);
69   DECLARE_ACKNOWLEDGER (rhythmic_head);
70   DECLARE_TRANSLATOR_LISTENER (spacing_section);
71
72   void start_translation_timestep ();
73   void stop_translation_timestep ();
74   void process_music ();
75   
76   virtual void finalize ();
77
78   void start_spanner ();
79   void stop_spanner ();
80 };
81
82 Spacing_engraver::Spacing_engraver ()
83 {
84   spacing_ = 0;
85   start_section_ = 0;
86 }
87
88 IMPLEMENT_TRANSLATOR_LISTENER (Spacing_engraver, spacing_section);
89 void
90 Spacing_engraver::listen_spacing_section (Stream_event *ev)
91 {
92   start_section_ = ev;
93 }
94
95 void
96 Spacing_engraver::process_music ()
97 {
98   if (start_section_ && spacing_)
99     stop_spanner ();
100   
101   if (!spacing_)
102     start_spanner ();
103 }
104
105 void
106 Spacing_engraver::start_spanner ()
107 {
108   assert (!spacing_);
109
110   spacing_ = make_spanner ("SpacingSpanner", SCM_EOL);
111   spacing_->set_bound (LEFT,
112                        unsmob_grob (get_property ("currentCommandColumn")));
113 }
114
115 void
116 Spacing_engraver::finalize ()
117 {
118   stop_spanner ();
119 }
120
121 void
122 Spacing_engraver::stop_spanner ()
123 {
124   if (spacing_)
125     {
126       Grob *p = unsmob_grob (get_property ("currentCommandColumn"));
127
128       spacing_->set_bound (RIGHT, p);
129       spacing_ = 0;
130     }
131 }
132
133 void
134 Spacing_engraver::acknowledge_note_spacing (Grob_info i)
135 {
136   Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
137 }
138
139 void
140 Spacing_engraver::acknowledge_staff_spacing (Grob_info i)
141 {
142   Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
143 }
144
145 void
146 Spacing_engraver::acknowledge_rhythmic_head (Grob_info i)
147 {
148   if (i.grob ()->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface"))
149       || i.grob ()->internal_has_interface (ly_symbol2scm ("multi-measure-interface")))
150     return;
151
152   /*
153     only pay attention to durations that are not grace notes.
154   */
155   if (!now_.grace_part_)
156     {
157       Stream_event *r = i.event_cause ();
158       if (r && r->in_event_class ("rhythmic-event"))
159         {
160           Moment len = get_event_length (r);
161           Rhythmic_tuple t (i, now_mom () + len);
162           now_durations_.push_back (t);
163         }
164     }
165 }
166
167 void
168 Spacing_engraver::stop_translation_timestep ()
169 {
170   Paper_column *musical_column
171     = dynamic_cast<Paper_column *> (unsmob_grob (get_property ("currentMusicalColumn")));
172
173
174   if (spacing_)
175     {  
176       musical_column->set_object ("spacing", spacing_->self_scm ());
177       unsmob_grob (get_property ("currentCommandColumn"))
178         ->set_object ("spacing", spacing_->self_scm ());
179     }
180   
181   SCM proportional = get_property ("proportionalNotationDuration");
182   if (unsmob_moment (proportional))
183     {
184       musical_column->set_property ("shortest-playing-duration", proportional);
185       musical_column->set_property ("shortest-starter-duration", proportional);
186       return;
187     }
188
189   Moment shortest_playing;
190   shortest_playing.set_infinite (1);
191   for (vsize i = 0; i < playing_durations_.size (); i++)
192     {
193       Stream_event *ev = playing_durations_[i].info_.event_cause ();
194       if (ev)
195         {
196           Moment m = get_event_length (ev);
197           shortest_playing = min (shortest_playing, m);
198         }
199     }
200   Moment starter;
201   starter.set_infinite (1);
202
203   for (vsize i = 0; i < now_durations_.size (); i++)
204     {
205       Moment m = get_event_length (now_durations_[i].info_.event_cause ());
206       if (m.to_bool ())
207         {
208           starter = min (starter, m);
209           playing_durations_.insert (now_durations_[i]);
210         }
211     }
212   now_durations_.clear ();
213
214   shortest_playing = min (shortest_playing, starter);
215
216   assert (starter.to_bool ());
217   SCM sh = shortest_playing.smobbed_copy ();
218   SCM st = starter.smobbed_copy ();
219
220   musical_column->set_property ("shortest-playing-duration", sh);
221   musical_column->set_property ("shortest-starter-duration", st);
222 }
223
224
225
226 void
227 Spacing_engraver::start_translation_timestep ()
228 {
229   start_section_ = 0;
230
231   now_ = now_mom ();
232   stopped_durations_.clear ();
233   
234   while (playing_durations_.size () && playing_durations_.front ().end_ < now_)
235     playing_durations_.delmin ();
236   while (playing_durations_.size () && playing_durations_.front ().end_ == now_)
237     stopped_durations_.push_back (playing_durations_.get ());
238 }
239
240 ADD_ACKNOWLEDGER (Spacing_engraver, staff_spacing);
241 ADD_ACKNOWLEDGER (Spacing_engraver, note_spacing);
242 ADD_ACKNOWLEDGER (Spacing_engraver, rhythmic_head);
243
244 ADD_TRANSLATOR (Spacing_engraver,
245                 "make a SpacingSpanner and do "
246                 "bookkeeping of shortest starting and playing notes  ",
247
248                 /* create */ "SpacingSpanner",
249                 /* accept */
250                 "spacing-section-event ",
251                 /* read */
252                 "currentMusicalColumn "
253                 "currentCommandColumn "
254                 "proportionalNotationDuration",
255                 
256                 /* write */ "");