2 duration-convert.cc -- implement Duration_convert
4 source file of the LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
9 #include "duration-convert.hh"
12 // statics Duration_convert
13 bool const Duration_convert::midi_as_plet_b_s
= true;
14 bool Duration_convert::no_quantify_b_s
= false;
15 bool Duration_convert::no_double_dots_b_s
= false;
16 bool Duration_convert::no_triplets_b_s
= false;
17 int Duration_convert::no_smaller_than_i_s
= 0;
20 Duration_convert::dur2_str( Duration dur
)
23 return String( "[" ) + String( dur
.ticks_i_
) + "]";
25 String
str( dur
.type_i_
);
26 str
+= String( '.', dur
.dots_i_
);
28 str
+= String( "*" ) + String( dur
.plet_
.iso_i_
)
29 + String( "/" ) + String( dur
.plet_
.type_i_
);
35 Duration_convert::dur2_i( Duration dur
, int division_1_i
)
37 return dur2_mom( dur
) * Moment( division_1_i
);
42 Duration_convert::dur2ticks_i( Duration dur
)
46 return dur2_mom( dur
) * Moment( Duration::division_1_i_s
);
50 Duration_convert::dur2_mom( Duration dur
)
53 return Moment( dur
.ticks_i_
, Duration::division_1_i_s
);
59 Moment mom
= Moment( 1 , dur
.type_i_
);
62 while ( dur
.dots_i_
-- ) {
67 return mom
* plet_factor_mom( dur
);
72 Duration_convert::i2_mom( int time_i
, int division_1_i
)
77 if ( division_1_i
> 0 )
78 return Moment( time_i
, division_1_i
);
80 return Moment( -division_1_i
, time_i
);
85 Duration_convert::mom2_dur( Moment mom
)
88 but filling an array using Duration_iterator
89 might speed things up, a little
91 Duration_iterator iter_dur
;
94 Duration dur
= iter_dur
++;
95 if ( mom
== dur2_mom( dur
) )
98 if ( midi_as_plet_b_s
) {
99 Moment mom_4
= mom
/ Moment( 4 );
100 long num
= mom_4
.numerator().as_long();
101 long den
= mom_4
.denominator().as_long();
102 Duration
dur( 4, 0 );
103 dur
.set_plet( num
, den
);
113 Duration_convert::mom2standardised_dur( Moment mom
)
116 but filling an array using Duration_iterator
117 might speed things up, a little
119 Duration_iterator iter_dur
;
122 Duration lower_dur
= iter_dur
++;
123 Duration
upper_dur( 0 );
125 upper_dur
= iter_dur();
126 Moment lower_mom
= dur2_mom( lower_dur
);
127 Moment upper_mom
= dur2_mom( upper_dur
);
128 if ( mom
< lower_mom
)
130 if ( mom
== lower_mom
)
138 Duration_convert::plet_factor_mom( Duration dur
)
140 return dur
.plet_
.mom();
144 Duration_convert::sync_f( Duration dur
, Moment mom
)
146 return mom
/ dur2_mom( dur
);
150 Duration_convert::ticks2_dur( int ticks_i
)
153 but filling an array using Duration_iterator
154 might speed things up, a little
156 // should use mom2_dur
157 Moment
mom( ticks_i
, Duration::division_1_i_s
);
158 Duration_iterator iter_dur
;
161 Duration dur
= iter_dur
++;
162 if ( mom
== dur2_mom( dur
) )
165 if ( midi_as_plet_b_s
) {
166 Duration
dur( 4, 0 );
167 dur
.set_plet( ticks_i
, Duration::division_1_i_s
/ 4 );
171 dur
.set_ticks( ticks_i
);
176 Duration_convert::ticks2standardised_dur( int ticks_i
)
179 but filling an array using Duration_iterator
180 might speed things up, a little
182 // should use mom2standardised_dur
183 Moment
mom( ticks_i
, Duration::division_1_i_s
);
184 Duration_iterator iter_dur
;
187 Duration lower_dur
= iter_dur
++;
188 // Duration upper_dur( 0 );
189 Duration
upper_dur( 1, 1 );
191 upper_dur
= iter_dur();
192 Moment lower_mom
= dur2_mom( lower_dur
);
193 Moment upper_mom
= dur2_mom( upper_dur
);
194 if ( mom
< lower_mom
)
196 if ( mom
== lower_mom
)
198 if ( mom
== upper_mom
) // don-t miss last (sic)
200 if ( ( mom
>= lower_mom
) && ( mom
<= upper_mom
) ) {
201 warning( String( "duration not exact: " ) + String( (Real
)mom
) );
202 if ( abs( mom
- lower_mom
) < abs( mom
- upper_mom
) )
207 lower_dur
= upper_dur
;
212 Duration_iterator::Duration_iterator()
214 cursor_dur_
.type_i_
= 128;
215 if ( Duration_convert::no_smaller_than_i_s
)
216 cursor_dur_
.type_i_
= Duration_convert::no_smaller_than_i_s
;
217 // cursor_dur_.set_plet( 1, 1 );
221 Duration_iterator::operator ++(int)
223 return forward_dur();
227 Duration_iterator::operator ()()
232 Duration_iterator::operator bool()
238 Duration_iterator::dur()
244 Duration_iterator::forward_dur()
246 /* should do smart table? guessing:
260 Duration dur
= cursor_dur_
;
262 if ( !cursor_dur_
.dots_i_
&& !cursor_dur_
.plet_b() ) {
263 cursor_dur_
.type_i_
*= 2;
264 cursor_dur_
.dots_i_
= 2;
266 else if ( cursor_dur_
.dots_i_
== 2 ) {
267 assert( !cursor_dur_
.plet_b() );
268 cursor_dur_
.dots_i_
= 0;
269 cursor_dur_
.type_i_
/= 4;
270 cursor_dur_
.set_plet( 2, 3 );
272 else if ( cursor_dur_
.plet_b()
273 && ( cursor_dur_
.plet_
.iso_i_
== 2 )
274 && ( cursor_dur_
.plet_
.type_i_
== 3 ) ) {
275 assert( !cursor_dur_
.dots_i_
);
276 cursor_dur_
.set_plet( 1, 1 );
277 cursor_dur_
.type_i_
*= 2;
278 cursor_dur_
.dots_i_
= 1;
280 else if ( cursor_dur_
.dots_i_
== 1 ) {
281 assert( !cursor_dur_
.plet_b() );
282 cursor_dur_
.dots_i_
= 0;
283 cursor_dur_
.type_i_
/= 2;
286 if ( Duration_convert::no_triplets_b_s
287 && cursor_dur_
.plet_b() && ok() )
289 if ( Duration_convert::no_double_dots_b_s
290 && ( cursor_dur_
.dots_i_
== 2 ) && ok() )
292 if ( Duration_convert::no_smaller_than_i_s
293 && ( cursor_dur_
.type_i_
> Duration_convert::no_smaller_than_i_s
) && ok() )
295 if ( Duration_convert::no_smaller_than_i_s
296 && cursor_dur_
.dots_i_
297 && ( cursor_dur_
.type_i_
>= Duration_convert::no_smaller_than_i_s
)
300 if ( Duration_convert::no_smaller_than_i_s
301 && ( cursor_dur_
.dots_i_
== 2 )
302 && ( cursor_dur_
.type_i_
>= Duration_convert::no_smaller_than_i_s
/ 2 )
310 Duration_iterator::ok()
312 return ( cursor_dur_
.type_i_
313 && !( ( cursor_dur_
.type_i_
== 1 ) && ( cursor_dur_
.dots_i_
> 2 ) ) );