2 duration-convert.cc -- implement Duration_convert
4 source file of the LilyPond music typesetter
6 (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "duration-convert.hh"
11 #include "duration-iter.hh"
14 // statics Duration_convert
15 bool Duration_convert::no_quantify_b_s
= false;
16 bool Duration_convert::no_double_dots_b_s
= false;
17 bool Duration_convert::no_tuplets_b_s
= false;
18 int Duration_convert::no_smaller_than_i_s
= 0;
19 Array
<Duration
> Duration_convert::dur_array_s
;
22 Duration_convert::dur2_str (Duration dur
)
25 return String ("[") + to_str (dur
.ticks_i_
) + "]";
28 if (dur
.durlog_i_
>= 0)
29 str
= to_str ( type2_i (dur
.durlog_i_
) );
30 else if (dur
.durlog_i_
== -1)
32 else if (dur
.durlog_i_
<= -2)
35 if (dur
.durlog_i_
< -2)
37 dur
.plet_
.iso_i_
*= 1 << (-2 - dur
.durlog_i_
);
40 str
+= to_str ('.', dur
.dots_i_
);
42 str
+= String ("*") + to_str (dur
.plet_
.iso_i_
)
43 + String ("/") + to_str (dur
.plet_
.type_i_
);
48 Duration_convert::dur2ticks_i (Duration dur
)
52 return dur2_mom (dur
) * Rational (Duration::division_1_i_s
);
56 Duration_convert::i2_type (int i
)
59 while (i
&& !(i
& 1)) {
67 Duration_convert::type2_i (int type
)
76 Duration_convert::dur2_mom (Duration dur
)
79 return Rational (dur
.ticks_i_
, Duration::division_1_i_s
);
82 if (dur
.durlog_i_
<-10)
86 mom
= Rational (type2_i (-dur
.durlog_i_
), 1);
88 mom
= Rational (1 , type2_i (dur
.durlog_i_
));
97 return mom
* plet_factor_mom (dur
);
101 Duration_convert::mom2_dur (Rational mom
)
110 return mom2standardised_dur (mom
);
114 Duration_convert::mom2standardised_dur (Rational mom
)
116 // if (!dur_array_s.length_i ())
117 if (!dur_array_s
.size ())
119 assert (dur_array_s
.size ());
120 for (int i
= 0; i
< dur_array_s
.size () - 1; i
++)
122 Rational lower_mom
= dur2_mom (dur_array_s
[ i
]);
123 if (mom
<= lower_mom
)
125 // all arbitrary, but 3/4 will get rid of the noise...
127 if (i
|| (mom
/ lower_mom
> Rational (3, 4)))
128 return dur_array_s
[ i
];
136 Rational upper_mom
= dur2_mom (dur_array_s
[ i
+ 1 ]);
137 if ((mom
< upper_mom
)
138 && ((mom
- lower_mom
) / lower_mom
139 < (upper_mom
- mom
) / upper_mom
))
140 return dur_array_s
[ i
];
142 return dur_array_s
[ dur_array_s
.size () - 1 ];
146 Duration_convert::set_array ()
148 dur_array_s
.clear ();
152 dur_array_s
.push (i
.forward_dur ());
157 Duration_convert::plet_factor_mom (Duration dur
)
159 return dur
.plet_
.mom ();
163 Duration_convert::sync_f (Duration dur
, Rational mom
)
165 return mom
/ dur2_mom (dur
);
169 Duration_convert::ticks2_dur (int ticks_i
)
171 Rational
mom (ticks_i
, Duration::division_1_i_s
);
172 return mom2standardised_dur (mom
);
176 Duration_convert::ticks2standardised_dur (int ticks_i
)
178 Rational
mom (ticks_i
, Duration::division_1_i_s
);
179 Duration dur
= mom2standardised_dur (mom
);