]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-engraver.cc
* lily/timing-engraver.cc (process_music): robustness fix.
[lilypond.git] / lily / timing-engraver.cc
1 /*
2   timing-engraver.cc -- implement Timing_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "timing-translator.hh"
10 #include "engraver.hh"
11
12 #include "context.hh"
13 #include "multi-measure-rest.hh"
14 #include "grob.hh"
15 #include "warn.hh"
16
17
18 class Timing_engraver : public Timing_translator, public Engraver
19 {
20 protected:
21   /* Need to know whether we're advancing in grace notes, or not. */
22   Moment last_moment_;
23
24   virtual void start_translation_timestep ();
25   virtual void process_music ();
26   virtual void stop_translation_timestep ();
27
28 public:
29   TRANSLATOR_DECLARATIONS (Timing_engraver);
30 };
31
32 ADD_TRANSLATOR (Timing_engraver,
33                 /* descr */ " Responsible for synchronizing timing information from staves.  "
34                 "Normally in @code{Score}.  In order to create polyrhythmic music, "
35                 "this engraver should be removed from @code{Score} and placed in "
36                 "@code{Staff}. "
37                 "\n\nThis engraver adds the alias @code{Timing} to its containing context.",
38                 /* creats*/ "",
39                 /* accepts */ "",
40                 /* acks  */ "",
41                 /* reads */ "automaticBars whichBar barAlways defaultBarType "
42                 "skipBars timing measureLength measurePosition currentBarNumber",
43                 /* write */ "");
44
45
46 Timing_engraver::Timing_engraver ()
47 {
48   last_moment_.main_part_ = Rational (-1);
49 }
50
51 void
52 Timing_engraver::process_music ()
53 {
54   Timing_translator::process_music ();
55
56   bool start_of_measure = (last_moment_.main_part_ != now_mom ().main_part_
57                            && !measure_position ().main_part_);
58
59   /*
60     We can't do this in start_translation_timestep(), since time sig
61     changes won't have happened by then.
62   */
63   if (start_of_measure)
64     {
65       Moment mlen = Moment (measure_length ());
66       Grob * column = unsmob_grob (get_property ("currentCommandColumn"));
67       if (column)
68         column->set_property ("measure-length", mlen.smobbed_copy ());
69       else
70         programming_error("No command column?");
71     }
72 }
73
74 void
75 Timing_engraver::start_translation_timestep ()
76 {
77   Timing_translator::start_translation_timestep ();
78
79   SCM automatic_bars = get_property ("automaticBars");
80   Moment now = now_mom ();
81   SCM which = get_property ("whichBar");
82
83   /* Set the first bar of the score? */
84   if (!scm_is_string (which))
85     which = SCM_EOL;
86
87   Moment mp = measure_position ();
88   bool start_of_measure = (last_moment_.main_part_ != now.main_part_
89                            && !mp.main_part_);
90
91   if (!scm_is_string (which) && to_boolean (automatic_bars))
92     {
93       SCM always = get_property ("barAlways");
94
95       if ((start_of_measure && last_moment_.main_part_ >= Moment (0))
96           || to_boolean (always))
97         {
98           /* should this work, or be junked?  See input/bugs/no-bars.ly */
99           which = get_property ("defaultBarType");
100         }
101     }
102
103   context ()->set_property ("whichBar", which);
104 }
105
106 void
107 Timing_engraver::stop_translation_timestep ()
108 {
109   Timing_translator::stop_translation_timestep ();
110   context ()->set_property ("whichBar", SCM_EOL);
111   last_moment_ = now_mom ();
112 }