]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-pq-engraver.cc
Merge with master
[lilypond.git] / lily / grob-pq-engraver.cc
1 /*
2   grob-pq-engraver.cc -- implement Grob_pq_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2001--2007  Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "context.hh"
10 #include "engraver.hh"
11 #include "grob.hh"
12 #include "warn.hh"
13
14 struct Grob_pq_entry
15 {
16   Grob *grob_;
17   Moment end_;
18 };
19
20 bool
21 operator< (Grob_pq_entry const &a, Grob_pq_entry const &b)
22 {
23   return a.end_ < b.end_;
24 }
25
26 class Grob_pq_engraver : public Engraver
27 {
28 public:
29   TRANSLATOR_DECLARATIONS (Grob_pq_engraver);
30 protected:
31   virtual void initialize ();
32   DECLARE_ACKNOWLEDGER (grob);
33   void start_translation_timestep ();
34   void stop_translation_timestep ();
35   void process_acknowledged ();
36   
37   vector<Grob_pq_entry> started_now_;
38 };
39
40 Grob_pq_engraver::Grob_pq_engraver ()
41 {
42 }
43
44 void
45 Grob_pq_engraver::initialize ()
46 {
47   context ()->set_property ("busyGrobs", SCM_EOL);
48 }
49
50 LY_DEFINE (ly_grob_pq_less_p, "ly:grob-pq-less?",
51            2, 0, 0, (SCM a, SCM b),
52            "Compare 2 grob priority queue entries. Internal")
53 {
54   if (Moment::compare (*unsmob_moment (scm_car (a)),
55                        *unsmob_moment (scm_car (b))) < 0)
56     return SCM_BOOL_T;
57   else
58     return SCM_BOOL_F;
59 }
60
61 void
62 Grob_pq_engraver::acknowledge_grob (Grob_info gi)
63 {
64   Stream_event *ev = gi.event_cause ();
65
66   if (ev
67       && !gi.grob ()->internal_has_interface (ly_symbol2scm ("multi-measure-interface")))
68     {
69       Moment n = now_mom ();
70       Moment l = get_event_length (ev);
71
72       if (!l.to_bool ())
73         return;
74
75       if (n.grace_part_)
76         {
77           l.grace_part_ = l.main_part_;
78           l.main_part_ = 0;
79         }
80
81       Moment end = n + l;
82
83       Grob_pq_entry e;
84       e.grob_ = gi.grob ();
85       e.end_ = end;
86
87       started_now_.push_back (e);
88     }
89 }
90
91 void
92 Grob_pq_engraver::process_acknowledged ()
93 {
94   vector_sort (started_now_, less<Grob_pq_entry> ());
95   SCM lst = SCM_EOL;
96   SCM *tail = &lst;
97   for (vsize i = 0; i < started_now_.size (); i++)
98     {
99       *tail = scm_acons (started_now_[i].end_.smobbed_copy (),
100                          started_now_[i].grob_->self_scm (),
101                          SCM_EOL);
102       tail = SCM_CDRLOC (*tail);
103     }
104
105   SCM busy = get_property ("busyGrobs");
106   busy = scm_merge_x (lst, busy, ly_grob_pq_less_p_proc);
107   context ()->set_property ("busyGrobs", busy);
108
109   started_now_.clear ();
110 }
111
112 void
113 Grob_pq_engraver::stop_translation_timestep ()
114 {
115   Moment now = now_mom ();
116   SCM start_busy = get_property ("busyGrobs");
117   SCM busy = start_busy;
118   while (scm_is_pair (busy) && *unsmob_moment (scm_caar (busy)) == now)
119     busy = scm_cdr (busy);
120
121 }
122
123 void
124 Grob_pq_engraver::start_translation_timestep ()
125 {
126   Moment now = now_mom ();
127
128   SCM start_busy = get_property ("busyGrobs");
129   SCM busy = start_busy;
130   while (scm_is_pair (busy) && *unsmob_moment (scm_caar (busy)) < now)
131     {
132       /*
133         The grob-pq-engraver is not water tight, and stuff like
134         tupletSpannerDuration confuses it.
135       */
136       busy = scm_cdr (busy);
137     }
138
139   if (start_busy != busy)
140     context ()->set_property ("busyGrobs", busy);
141 }
142
143 #include "translator.icc"
144 ADD_ACKNOWLEDGER (Grob_pq_engraver, grob);
145 ADD_TRANSLATOR (Grob_pq_engraver,
146
147                 /* doc */ "Administrate when certain grobs (eg. note heads) stop playing",
148                 /* create */ "",
149                 /* read */ "busyGrobs",
150                 /* write */ "busyGrobs");