]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-translator.cc
aa8e7e272374780361bab1d86964bf1af1a1dbad
[lilypond.git] / lily / timing-translator.cc
1 /*
2   timing-translator.cc -- implement Timing_translator
3
4
5   source file of the GNU LilyPond music typesetter
6
7   (c)  1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9
10 #include "warn.hh"
11 #include "timing-translator.hh"
12
13 #include "translator-group.hh"
14 #include "global-translator.hh"
15 #include "multi-measure-rest.hh"
16
17
18 void
19 Timing_translator::stop_translation_timestep ()
20 {
21   Translator *t = this;
22   Global_translator *global =0;
23   do
24     {
25       t = t->daddy_trans_ ;
26       global = dynamic_cast<Global_translator*> (t);
27     }
28   while (!global);
29
30   /* allbars == ! skipbars */
31   SCM sb = get_property ("skipBars");
32   bool allbars = !to_boolean (sb);
33
34   // urg: multi bar rests: should always process whole of first bar?
35   SCM tim = get_property ("timing");
36   bool timb = to_boolean (tim);
37   if (timb && allbars)
38     {
39       Moment barleft = (measure_length () - measure_position ());
40       Moment now = now_mom ();
41
42       if (barleft > Moment (0)
43           /*
44             Hmm. We insert the bar moment every time we process a
45             moment.  A waste of cpu?
46            */
47           && !now.grace_part_)
48         global->add_moment_to_process (now + barleft);
49     }
50 }
51
52 void
53 Timing_translator::initialize ()
54 {
55
56   /*
57     move this to engraver-init.ly? 
58    */
59  
60   daddy_trans_->set_property ("timing" , SCM_BOOL_T);  
61   daddy_trans_->set_property ("currentBarNumber" , gh_int2scm (1));
62
63   daddy_trans_->set_property ("timeSignatureFraction",
64                                 gh_cons (gh_int2scm (4), gh_int2scm (4)));
65   daddy_trans_->set_property ("measurePosition", Moment (Rational (0)).smobbed_copy ());
66   daddy_trans_->set_property ("measureLength", Moment (Rational (1)).smobbed_copy ());
67   daddy_trans_->set_property ("beatLength", Moment (Rational (1,4)).smobbed_copy ());
68 }
69
70 Rational
71 Timing_translator::measure_length () const
72 {
73   SCM l = get_property ("measureLength");
74   if (unsmob_moment (l))
75     return unsmob_moment (l)->main_part_;
76   else
77     return Rational (1);
78 }
79
80 Timing_translator::Timing_translator ()
81 {
82
83 }
84
85 Moment
86 Timing_translator::measure_position () const
87 {
88   SCM sm = get_property ("measurePosition");
89   
90   Moment m   =0;
91   if (unsmob_moment (sm))
92     {
93       m = *unsmob_moment (sm);
94       while (m.main_part_ < Rational (0))
95         m.main_part_ += measure_length ();
96     }
97   
98   return m;
99 }
100
101 void
102 Timing_translator::start_translation_timestep ()
103 {
104   Translator *t = this;
105   Global_translator *global =0;
106   do
107     {
108       t = t->daddy_trans_ ;
109       global = dynamic_cast<Global_translator*> (t);
110     }
111   while (!global);
112
113   Moment now = global->now_mom_;
114   Moment dt = now  - global -> prev_mom_;
115   if (dt < Moment (0))
116     {
117       programming_error ("Moving backwards in time");
118       dt = 0;
119     }
120   else if (dt.main_part_.is_infinity ())
121     {
122       programming_error ("Moving infinitely to future");
123       dt = 0;
124     }
125   
126   if (!dt.to_bool ())
127     return;
128
129   Moment measposp;
130
131   SCM s = get_property ("measurePosition");
132   if (unsmob_moment (s))
133     {
134       measposp = *unsmob_moment (s);
135     }
136   else
137     {
138       measposp = now;
139       daddy_trans_->set_property ("measurePosition", measposp.smobbed_copy ());
140     }
141   
142   measposp += dt;
143   
144   SCM barn = get_property ("currentBarNumber");
145   int b = 0;
146   if (gh_number_p (barn))
147     {
148       b = gh_scm2int (barn);
149     }
150
151   SCM cad = get_property ("timing");
152   bool c= to_boolean (cad);
153
154   Rational len = measure_length ();
155   while (c && measposp.main_part_ >= len)
156     {
157       measposp.main_part_ -= len;
158       b ++;
159     }
160
161   daddy_trans_->set_property ("currentBarNumber", gh_int2scm (b));
162   daddy_trans_->set_property ("measurePosition", measposp.smobbed_copy ());
163 }
164
165 ENTER_DESCRIPTION(Timing_translator,"","","","","","");