2 duration-convert.cc -- implement Duration_convert
4 source file of the LilyPond music typesetter
6 (c) 1997--1998 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 const Duration_convert::midi_as_plet_b_s
= true;
16 bool Duration_convert::no_quantify_b_s
= false;
17 bool Duration_convert::no_double_dots_b_s
= false;
18 bool Duration_convert::no_triplets_b_s
= false;
19 int Duration_convert::no_smaller_than_i_s
= 0;
20 Array
<Duration
> Duration_convert::dur_array_s
;
23 Duration_convert::dur2_str (Duration dur
)
26 return String ("[") + to_str (dur
.ticks_i_
) + "]";
29 if (dur
.durlog_i_
>= 0)
30 str
= to_str ( type2_i (dur
.durlog_i_
) );
31 else if (dur
.durlog_i_
== -1)
33 else if (dur
.durlog_i_
== -2)
35 str
+= to_str ('.', dur
.dots_i_
);
37 str
+= String ("*") + to_str (dur
.plet_
.iso_i_
)
38 + String ("/") + to_str (dur
.plet_
.type_i_
);
43 Duration_convert::dur2ticks_i (Duration dur
)
47 return dur2_mom (dur
) * Moment (Duration::division_1_i_s
);
51 Duration_convert::i2_type (int i
)
54 while (i
&& !(i
& 1)) {
62 Duration_convert::type2_i (int type
)
71 Duration_convert::dur2_mom (Duration dur
)
74 return Moment (dur
.ticks_i_
, Duration::division_1_i_s
);
77 if (dur
.durlog_i_
<-10)
81 mom
= Moment (type2_i (-dur
.durlog_i_
), 1);
83 mom
= Moment (1 , type2_i (dur
.durlog_i_
));
92 return mom
* plet_factor_mom (dur
);
96 Duration_convert::mom2_dur (Moment mom
)
106 Duration dur
= mom2standardised_dur (mom
);
107 // if (!dur.mom () || (dur.mom () == mom))
108 if (!dur
.length () || (dur
.length () == mom
))
110 assert (midi_as_plet_b_s
);
112 // dur.set_plet (type_mom, Duration::division_1_i_s / 4);
114 // Moment as_plet_mom = mom / dur.mom ();
115 Moment as_plet_mom
= mom
/ dur
.length ();
116 as_plet_mom
*= dur
.plet_
.mom ();
117 long num
= as_plet_mom
.num ();
118 long den
= as_plet_mom
.den ();
119 dur
.set_plet (num
, den
);
124 Duration_convert::mom2standardised_dur (Moment mom
)
126 // if (!dur_array_s.length_i ())
127 if (!dur_array_s
.size ())
129 assert (dur_array_s
.size ());
130 for (int i
= 0; i
< dur_array_s
.size () - 1; i
++)
132 Moment lower_mom
= dur2_mom (dur_array_s
[ i
]);
133 if (mom
<= lower_mom
)
135 // all arbitrary, but 3/4 will get rid of the noise...
137 if (i
|| (mom
/ lower_mom
> Moment (3, 4)))
138 return dur_array_s
[ i
];
146 Moment upper_mom
= dur2_mom (dur_array_s
[ i
+ 1 ]);
147 if ((mom
< upper_mom
)
148 && ((mom
- lower_mom
) / lower_mom
149 < (upper_mom
- mom
) / upper_mom
))
150 return dur_array_s
[ i
];
152 return dur_array_s
[ dur_array_s
.size () - 1 ];
156 Duration_convert::set_array ()
158 dur_array_s
.clear ();
160 Duration_iterator iter_dur
;
163 dur_array_s
.push (iter_dur
++);
168 Duration_convert::plet_factor_mom (Duration dur
)
170 return dur
.plet_
.mom ();
174 Duration_convert::sync_f (Duration dur
, Moment mom
)
176 return mom
/ dur2_mom (dur
);
180 Duration_convert::ticks2_dur (int ticks_i
)
182 Moment
mom (ticks_i
, Duration::division_1_i_s
);
183 if (midi_as_plet_b_s
)
184 return mom2_dur (mom
);
186 Duration dur
= mom2standardised_dur (mom
);
188 if (dur
.length () == mom
)
191 return mom2_dur (mom
);
195 Duration_convert::ticks2standardised_dur (int ticks_i
)
197 Moment
mom (ticks_i
, Duration::division_1_i_s
);
198 Duration dur
= mom2standardised_dur (mom
);