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