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