]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-translator.cc
release: 1.5.0
[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--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9
10 #include "debug.hh"
11 #include "timing-translator.hh"
12 #include "command-request.hh"
13 #include "translator-group.hh"
14 #include "global-translator.hh"
15 #include "multi-measure-rest.hh"
16
17 /*
18   TODO: change the rest of lily, so communication with
19   Timing_translator is only done through properties.  This means the
20   class declaration can go here.  */
21 bool
22 Timing_translator::try_music (Music*r)
23 {
24   if (dynamic_cast<Barcheck_req*> (r))
25     {
26       check_ = r;
27       return true;
28     }
29   return false;
30 }
31
32 void
33 Timing_translator::process_music ()
34 {
35   if (check_ && measure_position ().main_part_)
36     {
37       check_->origin ()->warning (_f ("barcheck failed at: %s", 
38                                       measure_position ().str ()));
39       Moment zero; 
40       
41       if (!to_boolean (get_property ("barCheckNoSynchronize")))
42         daddy_trans_l_->set_property ("measurePosition", zero.smobbed_copy ());
43     }
44 }
45
46 void
47 Timing_translator::stop_translation_timestep ()
48 {
49   check_ = 0;
50   
51   Translator *t = this;
52   Global_translator *global_l =0;
53   do
54     {
55       t = t->daddy_trans_l_ ;
56       global_l = dynamic_cast<Global_translator*> (t);
57     }
58   while (!global_l);
59
60   /* allbars == ! skipbars */
61   SCM sb = get_property ("skipBars");
62   bool allbars = !to_boolean (sb);
63
64   // urg: multi bar rests: should always process whole of first bar?
65   SCM tim = get_property ("timing");
66   bool timb = to_boolean (tim);
67   if (timb && allbars)
68     {
69       Moment barleft = (measure_length () - measure_position ());
70
71       if (barleft > Moment (0))
72         global_l->add_moment_to_process (now_mom () + barleft);
73     }
74 }
75
76
77 ADD_THIS_TRANSLATOR (Timing_translator);
78
79 void
80 Timing_translator::initialize ()
81 {
82   Moment m;
83   daddy_trans_l_->set_property ("timing" , SCM_BOOL_T);  
84   daddy_trans_l_->set_property ("currentBarNumber" , gh_int2scm (1));
85   daddy_trans_l_->set_property ("measurePosition", m.smobbed_copy ());
86   daddy_trans_l_->set_property ("timeSignatureFraction",
87                                 gh_cons (gh_int2scm (4), gh_int2scm (4)));
88
89   daddy_trans_l_->set_property ("measureLength", Moment (1).smobbed_copy ());
90   daddy_trans_l_->set_property ("beatLength", Moment (1,4).smobbed_copy ());
91 }
92
93 Rational
94 Timing_translator::measure_length () const
95 {
96   SCM l = get_property ("measureLength");
97   if (unsmob_moment (l))
98     return unsmob_moment (l)->main_part_;
99   else
100     return Rational (1);
101 }
102
103 Timing_translator::Timing_translator ()
104 {
105
106 }
107
108 Moment
109 Timing_translator::measure_position () const
110 {
111   SCM sm = get_property ("measurePosition");
112   
113   Moment m   =0;
114   if (unsmob_moment (sm))
115     {
116       m = *unsmob_moment (sm);
117       while (m.main_part_ < Rational (0))
118         m.main_part_ += measure_length ();
119     }
120   
121   return m;
122 }
123
124 void
125 Timing_translator::start_translation_timestep ()
126 {
127   check_ = 0;
128   Translator *t = this;
129   Global_translator *global_l =0;
130   do
131     {
132       t = t->daddy_trans_l_ ;
133       global_l = dynamic_cast<Global_translator*> (t);
134     }
135   while (!global_l);
136
137   Moment dt = global_l->now_mom_  - global_l -> prev_mom_;
138   if (dt < Moment (0))
139     {
140       programming_error ("Moving backwards in time");
141       dt = 0;
142     }
143   
144   if (!dt)
145     return;
146
147   Moment measposp;
148
149   SCM s = get_property ("measurePosition");
150   if (unsmob_moment (s))
151     {
152       measposp = *unsmob_moment (s);
153     }
154   else
155     {
156       daddy_trans_l_->set_property ("measurePosition", measposp.smobbed_copy ());
157     }
158   
159   measposp += dt;
160   
161   SCM barn = get_property ("currentBarNumber");
162   int b = 0;
163   if (gh_number_p (barn))
164     {
165       b = gh_scm2int (barn);
166     }
167
168   SCM cad = get_property ("timing");
169   bool c= to_boolean (cad);
170
171   Rational len = measure_length ();
172   while (c && measposp.main_part_ >= len)
173     {
174       measposp.main_part_ -= len;
175       b ++;
176     }
177
178   daddy_trans_l_->set_property ("currentBarNumber", gh_int2scm (b));
179   daddy_trans_l_->set_property ("measurePosition", measposp.smobbed_copy ());
180 }
181