]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-translator.cc
release: 1.5.30
[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--2002 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
19 void
20 Timing_translator::stop_translation_timestep ()
21 {
22   Translator *t = this;
23   Global_translator *global_l =0;
24   do
25     {
26       t = t->daddy_trans_l_ ;
27       global_l = dynamic_cast<Global_translator*> (t);
28     }
29   while (!global_l);
30
31   /* allbars == ! skipbars */
32   SCM sb = get_property ("skipBars");
33   bool allbars = !to_boolean (sb);
34
35   // urg: multi bar rests: should always process whole of first bar?
36   SCM tim = get_property ("timing");
37   bool timb = to_boolean (tim);
38   if (timb && allbars)
39     {
40       Moment barleft = (measure_length () - measure_position ());
41       Moment now = now_mom ();
42
43       if (barleft > Moment (0)
44           /*
45             Hmm. We insert the bar moment every time we process a
46             moment.  A waste of cpu?
47            */
48           && !now.grace_part_)
49         global_l->add_moment_to_process (now + barleft);
50     }
51 }
52
53
54 void
55 Timing_translator::initialize ()
56 {
57   daddy_trans_l_->set_property ("timing" , SCM_BOOL_T);  
58   daddy_trans_l_->set_property ("currentBarNumber" , gh_int2scm (1));
59
60   daddy_trans_l_->set_property ("timeSignatureFraction",
61                                 gh_cons (gh_int2scm (4), gh_int2scm (4)));
62   daddy_trans_l_->set_property ("measurePosition", Moment (Rational (0)).smobbed_copy ());
63   daddy_trans_l_->set_property ("measureLength", Moment (Rational (1)).smobbed_copy ());
64   daddy_trans_l_->set_property ("beatLength", Moment (Rational (1,4)).smobbed_copy ());
65 }
66
67 Rational
68 Timing_translator::measure_length () const
69 {
70   SCM l = get_property ("measureLength");
71   if (unsmob_moment (l))
72     return unsmob_moment (l)->main_part_;
73   else
74     return Rational (1);
75 }
76
77 Timing_translator::Timing_translator ()
78 {
79
80 }
81
82 Moment
83 Timing_translator::measure_position () const
84 {
85   SCM sm = get_property ("measurePosition");
86   
87   Moment m   =0;
88   if (unsmob_moment (sm))
89     {
90       m = *unsmob_moment (sm);
91       while (m.main_part_ < Rational (0))
92         m.main_part_ += measure_length ();
93     }
94   
95   return m;
96 }
97
98 void
99 Timing_translator::start_translation_timestep ()
100 {
101   Translator *t = this;
102   Global_translator *global_l =0;
103   do
104     {
105       t = t->daddy_trans_l_ ;
106       global_l = dynamic_cast<Global_translator*> (t);
107     }
108   while (!global_l);
109
110   Moment now = global_l->now_mom_;
111   Moment dt = now  - global_l -> prev_mom_;
112   if (dt < Moment (0))
113     {
114       programming_error ("Moving backwards in time");
115       dt = 0;
116     }
117   
118   if (!dt.to_bool ())
119     return;
120
121   Moment measposp;
122
123   SCM s = get_property ("measurePosition");
124   if (unsmob_moment (s))
125     {
126       measposp = *unsmob_moment (s);
127     }
128   else
129     {
130       measposp = now;
131       daddy_trans_l_->set_property ("measurePosition", measposp.smobbed_copy ());
132     }
133   
134   measposp += dt;
135   
136   SCM barn = get_property ("currentBarNumber");
137   int b = 0;
138   if (gh_number_p (barn))
139     {
140       b = gh_scm2int (barn);
141     }
142
143   SCM cad = get_property ("timing");
144   bool c= to_boolean (cad);
145
146   Rational len = measure_length ();
147   while (c && measposp.main_part_ >= len)
148     {
149       measposp.main_part_ -= len;
150       b ++;
151     }
152
153   daddy_trans_l_->set_property ("currentBarNumber", gh_int2scm (b));
154   daddy_trans_l_->set_property ("measurePosition", measposp.smobbed_copy ());
155 }
156
157 ENTER_DESCRIPTION (Timing_translator, "","","","","" );