]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-engraver.cc
0e2fb868dc3c70a61454b485d3298a96b45b75db
[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 "context.hh"
10 #include "multi-measure-rest.hh"
11 #include "timing-translator.hh"
12 #include "engraver.hh"
13 #include "grob.hh"
14
15 /**
16   Do time bookkeeping
17  */
18 class Timing_engraver : public Timing_translator, public Engraver
19 {
20 protected:
21   /* Needed 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 initialize ();
26   virtual void process_music();
27   virtual void stop_translation_timestep ();
28
29 public:
30   TRANSLATOR_DECLARATIONS (Timing_engraver);
31 };
32
33
34 Timing_engraver::Timing_engraver ()
35 {
36   last_moment_.main_part_ = Rational (-1);
37 }
38
39
40 void
41 Timing_engraver::initialize ()
42 {
43   Timing_translator::initialize ();
44   
45   SCM which = get_property ("whichBar");
46   Moment now = now_mom ();
47   
48   /* Set the first bar of the score? */
49   if (!scm_is_string (which))
50     which = (now.main_part_ || now.main_part_ == last_moment_.main_part_)
51       ? SCM_EOL : scm_makfrom0str ("|");
52
53   context ()->set_property ("whichBar", which);
54 }
55
56 void
57 Timing_engraver::process_music()
58 {
59   Timing_translator::process_music ();
60
61   bool start_of_measure = (last_moment_.main_part_ != now_mom().main_part_
62                            && !measure_position ().main_part_);
63
64   /*
65     We can't do this in start_translation_timestep(), since time sig
66     changes won't have happened by then.
67    */
68   if (start_of_measure)
69     {
70       Moment mlen = Moment (measure_length ());
71       unsmob_grob (get_property ("currentCommandColumn"))
72         ->set_property ("measure-length", mlen.smobbed_copy ()); 
73     }
74 }
75   
76
77 void
78 Timing_engraver::start_translation_timestep ()
79 {
80   Timing_translator::start_translation_timestep ();
81
82   SCM automatic_bars = get_property ("automaticBars");
83   Moment now = now_mom ();
84   SCM which = get_property ("whichBar");
85
86   /* Set the first bar of the score? */
87   if (!scm_is_string (which))
88     which = SCM_EOL;
89
90   Moment mp = measure_position ();
91   bool start_of_measure = (last_moment_.main_part_ != now.main_part_
92                            && !mp.main_part_);
93
94   if (!scm_is_string (which) && to_boolean (automatic_bars))
95     {
96       SCM always = get_property ("barAlways");
97
98       if ( start_of_measure || (to_boolean (always)))
99         {
100           /* should this work, or be junked?  See input/bugs/no-bars.ly */
101           which = get_property ("defaultBarType");
102         }
103     }
104
105   context ()->set_property ("whichBar", which);
106 }
107
108 void
109 Timing_engraver::stop_translation_timestep ()
110 {
111   Timing_translator::stop_translation_timestep ();
112   context ()->set_property ("whichBar", SCM_EOL);
113   last_moment_ = now_mom ();
114 }
115
116
117 ADD_TRANSLATOR (Timing_engraver,
118 /* descr */       " Responsible for synchronizing timing information from staves.  "
119 "Normally in @code{Score}.  In order to create polyrhythmic music, "
120 "this engraver should be removed from @code{Score} and placed in "
121 "@code{Staff}. "
122 "\n\nThis engraver adds the alias @code{Timing} to its containing context."
123
124                    ,
125 /* creats*/       "",
126 /* accepts */     "",
127 /* acks  */      "",
128 /* reads */       "automaticBars whichBar barAlways defaultBarType skipBars timing measureLength measurePosition currentBarNumber",
129 /* write */       "");