]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-engraver.cc
(stop_translation_timestep): use
[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
111   spacing_ = make_spanner ("SpacingSpanner", SCM_EOL);
112   spacing_->set_bound (LEFT,
113                        unsmob_grob (get_property ("currentCommandColumn")));
114 }
115
116 void
117 Spacing_engraver::finalize ()
118 {
119   stop_spanner ();
120 }
121
122 void
123 Spacing_engraver::stop_spanner ()
124 {
125   if (spacing_)
126     {
127       Grob *p = unsmob_grob (get_property ("currentCommandColumn"));
128
129       spacing_->set_bound (RIGHT, p);
130       spacing_ = 0;
131     }
132 }
133
134 void
135 Spacing_engraver::acknowledge_note_spacing (Grob_info i)
136 {
137   Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
138 }
139
140 void
141 Spacing_engraver::acknowledge_staff_spacing (Grob_info i)
142 {
143   Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
144 }
145
146 void
147 Spacing_engraver::acknowledge_rhythmic_head (Grob_info i)
148 {
149   if (i.grob ()->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface"))
150       || i.grob ()->internal_has_interface (ly_symbol2scm ("multi-measure-interface")))
151     return;
152
153   /*
154     only pay attention to durations that are not grace notes.
155   */
156   if (!now_.grace_part_)
157     {
158       Stream_event *r = i.event_cause ();
159       if (r && r->in_event_class ("rhythmic-event"))
160         {
161           Moment len = get_event_length (r);
162           Rhythmic_tuple t (i, now_mom () + len);
163           now_durations_.push_back (t);
164         }
165     }
166 }
167
168 void
169 Spacing_engraver::stop_translation_timestep ()
170 {
171   Paper_column *musical_column
172     = dynamic_cast<Paper_column *> (unsmob_grob (get_property ("currentMusicalColumn")));
173
174
175   if (!spacing_)
176     start_spanner ();
177
178   musical_column->set_object ("spacing", spacing_->self_scm ());
179   unsmob_grob (get_property ("currentCommandColumn"))
180     ->set_object ("spacing", spacing_->self_scm ());
181   
182   SCM proportional = get_property ("proportionalNotationDuration");
183   if (unsmob_moment (proportional))
184     {
185       musical_column->set_property ("shortest-playing-duration", proportional);
186       musical_column->set_property ("shortest-starter-duration", proportional);
187       return;
188     }
189
190   Moment shortest_playing;
191   shortest_playing.set_infinite (1);
192   for (vsize i = 0; i < playing_durations_.size (); i++)
193     {
194       Stream_event *ev = playing_durations_[i].info_.event_cause ();
195       if (ev)
196         {
197           Moment m = get_event_length (ev);
198           shortest_playing = min (shortest_playing, m);
199         }
200     }
201   Moment starter;
202   starter.set_infinite (1);
203
204   for (vsize i = 0; i < now_durations_.size (); i++)
205     {
206       Moment m = get_event_length (now_durations_[i].info_.event_cause ());
207       if (m.to_bool ())
208         {
209           starter = min (starter, m);
210           playing_durations_.insert (now_durations_[i]);
211         }
212     }
213   now_durations_.clear ();
214
215   shortest_playing = min (shortest_playing, starter);
216
217   assert (starter.to_bool ());
218   SCM sh = shortest_playing.smobbed_copy ();
219   SCM st = starter.smobbed_copy ();
220
221   musical_column->set_property ("shortest-playing-duration", sh);
222   musical_column->set_property ("shortest-starter-duration", st);
223 }
224
225
226
227 void
228 Spacing_engraver::start_translation_timestep ()
229 {
230   start_section_ = 0;
231
232   now_ = now_mom ();
233   stopped_durations_.clear ();
234   
235   while (playing_durations_.size () && playing_durations_.front ().end_ < now_)
236     playing_durations_.delmin ();
237   while (playing_durations_.size () && playing_durations_.front ().end_ == now_)
238     stopped_durations_.push_back (playing_durations_.get ());
239 }
240
241 ADD_ACKNOWLEDGER (Spacing_engraver, staff_spacing);
242 ADD_ACKNOWLEDGER (Spacing_engraver, note_spacing);
243 ADD_ACKNOWLEDGER (Spacing_engraver, rhythmic_head);
244
245 ADD_TRANSLATOR (Spacing_engraver,
246                 "make a SpacingSpanner and do "
247                 "bookkeeping of shortest starting and playing notes  ",
248
249                 /* create */ "SpacingSpanner",
250                 /* accept */
251                 "spacing-section-event ",
252                 /* read */
253                 "currentMusicalColumn "
254                 "currentCommandColumn "
255                 "proportionalNotationDuration",
256                 
257                 /* write */ "");