]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-pq-engraver.cc
6ad7ccf0fa89f9f913b38f04f6bfdb7c538a6657
[lilypond.git] / lily / grob-pq-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2001--2012  Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "context.hh"
21 #include "engraver.hh"
22 #include "grob.hh"
23 #include "warn.hh"
24
25 struct Grob_pq_entry
26 {
27   Grob *grob_;
28   Moment end_;
29 };
30
31 bool
32 operator < (Grob_pq_entry const &a, Grob_pq_entry const &b)
33 {
34   return a.end_ < b.end_;
35 }
36
37 class Grob_pq_engraver : public Engraver
38 {
39 public:
40   TRANSLATOR_DECLARATIONS (Grob_pq_engraver);
41 protected:
42   virtual void initialize ();
43   DECLARE_ACKNOWLEDGER (grob);
44   void start_translation_timestep ();
45   void stop_translation_timestep ();
46   void process_acknowledged ();
47
48   vector<Grob_pq_entry> started_now_;
49 };
50
51 Grob_pq_engraver::Grob_pq_engraver ()
52 {
53 }
54
55 void
56 Grob_pq_engraver::initialize ()
57 {
58   context ()->set_property ("busyGrobs", SCM_EOL);
59 }
60
61 LY_DEFINE (ly_grob_pq_less_p, "ly:grob-pq<?",
62            2, 0, 0, (SCM a, SCM b),
63            "Compare two grob priority queue entries."
64            "  This is an internal function.")
65 {
66   if (Moment::compare (*unsmob_moment (scm_car (a)),
67                        *unsmob_moment (scm_car (b))) < 0)
68     return SCM_BOOL_T;
69   else
70     return SCM_BOOL_F;
71 }
72
73 void
74 Grob_pq_engraver::acknowledge_grob (Grob_info gi)
75 {
76   Stream_event *ev = gi.event_cause ();
77
78   if (ev
79       && !gi.grob ()->internal_has_interface (ly_symbol2scm ("multi-measure-interface")))
80     {
81       Moment n = now_mom ();
82       Moment l = get_event_length (ev, n);
83
84       if (!l.to_bool ())
85         return;
86
87       Moment end = n + l;
88
89       Grob_pq_entry e;
90       e.grob_ = gi.grob ();
91       e.end_ = end;
92
93       started_now_.push_back (e);
94     }
95 }
96
97 void
98 Grob_pq_engraver::process_acknowledged ()
99 {
100   vector_sort (started_now_, less<Grob_pq_entry> ());
101   SCM lst = SCM_EOL;
102   SCM *tail = &lst;
103   for (vsize i = 0; i < started_now_.size (); i++)
104     {
105       *tail = scm_acons (started_now_[i].end_.smobbed_copy (),
106                          started_now_[i].grob_->self_scm (),
107                          SCM_EOL);
108       tail = SCM_CDRLOC (*tail);
109     }
110
111   SCM busy = get_property ("busyGrobs");
112   busy = scm_merge_x (lst, busy, ly_grob_pq_less_p_proc);
113   context ()->set_property ("busyGrobs", busy);
114
115   started_now_.clear ();
116 }
117
118 void
119 Grob_pq_engraver::stop_translation_timestep ()
120 {
121   Moment now = now_mom ();
122   SCM start_busy = get_property ("busyGrobs");
123   SCM busy = start_busy;
124   while (scm_is_pair (busy) && *unsmob_moment (scm_caar (busy)) == now)
125     busy = scm_cdr (busy);
126
127 }
128
129 void
130 Grob_pq_engraver::start_translation_timestep ()
131 {
132   Moment now = now_mom ();
133
134   SCM start_busy = get_property ("busyGrobs");
135   SCM busy = start_busy;
136   while (scm_is_pair (busy) && *unsmob_moment (scm_caar (busy)) < now)
137     {
138       /*
139         The grob-pq-engraver is not water tight, and stuff like
140         tupletSpannerDuration confuses it.
141       */
142       busy = scm_cdr (busy);
143     }
144
145   if (start_busy != busy)
146     context ()->set_property ("busyGrobs", busy);
147 }
148
149 #include "translator.icc"
150 ADD_ACKNOWLEDGER (Grob_pq_engraver, grob);
151 ADD_TRANSLATOR (Grob_pq_engraver,
152                 /* doc */
153                 "Administrate when certain grobs (e.g., note heads) stop"
154                 " playing.",
155
156                 /* create */
157                 "",
158
159                 /* read */
160                 "busyGrobs ",
161
162                 /* write */
163                 "busyGrobs "
164                );