2 duration-convert.cc -- implement
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 Duration_convert::be_blonde_b_s = false;
14 bool Duration_convert::no_double_dots_b_s = false;
15 bool Duration_convert::no_triplets_b_s = false;
16 int Duration_convert::no_smaller_than_i_s = 0;
19 Duration_convert::dur2_str( Duration dur )
22 return String( "[" ) + String( dur.ticks_i_ ) + "]";
24 String str( dur.type_i_ );
25 str += String( '.', dur.dots_i_ );
27 str += String( "*" ) + String( dur.plet_.iso_i_ )
28 + String( "/" ) + String( dur.plet_.type_i_ );
34 Duration_convert::dur2_i( Duration dur, int division_1_i )
36 return dur2_mom( dur ) * Moment( division_1_i );
41 Duration_convert::dur2ticks_i( Duration dur )
45 return dur2_mom( dur ) * Moment( Duration::division_1_i_s );
49 Duration_convert::dur2_mom( Duration dur )
52 return Moment( dur.ticks_i_, Duration::division_1_i_s );
58 Moment mom = Moment( 1 , dur.type_i_ );
61 while ( dur.dots_i_-- ) {
66 return mom * plet_factor_mom( dur );
71 Duration_convert::i2_mom( int time_i, int division_1_i )
76 if ( division_1_i > 0 )
77 return Moment( time_i, division_1_i );
79 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 lower_dur = iter_dur++;
95 Duration upper_dur( 0 );
97 upper_dur = iter_dur();
98 Moment lower_mom = dur2_mom( lower_dur );
99 Moment upper_mom = dur2_mom( upper_dur );
100 if ( mom == lower_mom )
102 if ( mom == upper_mom ) // don-t miss last (sic)
104 if ( ( mom >= lower_mom ) && ( mom <= upper_mom ) ) {
105 warning( String( "duration not exact: " ) + String( (Real)mom ) , 0 );
106 if ( abs( mom - lower_mom ) < abs( mom - upper_mom ) )
111 lower_dur = upper_dur;
113 return Duration( 0 );
118 Duration_convert::plet_factor_mom( Duration dur )
120 return dur.plet_.mom();
124 Duration_convert::sync_f( Duration dur, Moment mom )
126 return mom / dur2_mom( dur );
130 Duration_convert::ticks2_dur( int ticks_i )
133 but filling an array using Duration_iterator
134 might speed things up, a little
136 Moment mom( ticks_i, Duration::division_1_i_s );
137 Duration_iterator iter_dur;
140 Duration dur = iter_dur++;
141 if ( mom == dur2_mom( dur ) )
145 dur.set_ticks( ticks_i );
150 Duration_convert::ticks2standardised_dur( int ticks_i )
153 but filling an array using Duration_iterator
154 might speed things up, a little
156 Moment mom( ticks_i, Duration::division_1_i_s );
157 Duration_iterator iter_dur;
160 Duration lower_dur = iter_dur++;
161 // Duration upper_dur( 0 );
162 Duration upper_dur( 1, 1 );
164 upper_dur = iter_dur();
165 Moment lower_mom = dur2_mom( lower_dur );
166 Moment upper_mom = dur2_mom( upper_dur );
167 if ( mom < lower_mom )
169 if ( mom == lower_mom )
171 if ( mom == upper_mom ) // don-t miss last (sic)
173 if ( ( mom >= lower_mom ) && ( mom <= upper_mom ) ) {
174 warning( String( "duration not exact: " ) + String( (Real)mom ) , 0 );
175 if ( abs( mom - lower_mom ) < abs( mom - upper_mom ) )
180 lower_dur = upper_dur;
185 Duration_iterator::Duration_iterator()
187 cursor_dur_.type_i_ = 128;
188 if ( Duration_convert::no_smaller_than_i_s )
189 cursor_dur_.type_i_ = Duration_convert::no_smaller_than_i_s;
190 cursor_dur_.set_plet( 0,1 ); // ugh?
194 Duration_iterator::operator ++(int)
196 return forward_dur();
200 Duration_iterator::operator ()()
205 Duration_iterator::operator bool()
211 Duration_iterator::dur()
217 Duration_iterator::forward_dur()
219 /* should do smart table? guessing:
233 Duration dur = cursor_dur_;
235 if ( !cursor_dur_.dots_i_ && !cursor_dur_.plet_b() ) {
236 cursor_dur_.type_i_ *= 2;
237 cursor_dur_.dots_i_ = 2;
239 else if ( cursor_dur_.dots_i_ == 2 ) {
240 assert( !cursor_dur_.plet_b() );
241 cursor_dur_.dots_i_ = 0;
242 cursor_dur_.type_i_ /= 4;
243 cursor_dur_.set_plet( 2, 3 );
245 else if ( cursor_dur_.plet_b()
246 && ( cursor_dur_.plet_.iso_i_ == 2 )
247 && ( cursor_dur_.plet_.type_i_ == 3 ) ) {
248 assert( !cursor_dur_.dots_i_ );
249 cursor_dur_.set_plet( 0,1 );
250 cursor_dur_.type_i_ *= 2;
251 cursor_dur_.dots_i_ = 1;
253 else if ( cursor_dur_.dots_i_ == 1 ) {
254 assert( !cursor_dur_.plet_b() );
255 cursor_dur_.dots_i_ = 0;
256 cursor_dur_.type_i_ /= 2;
259 if ( Duration_convert::no_triplets_b_s
260 && cursor_dur_.plet_b() && ok() )
262 if ( Duration_convert::no_double_dots_b_s
263 && ( cursor_dur_.dots_i_ == 2 ) && ok() )
265 if ( Duration_convert::no_smaller_than_i_s
266 && ( cursor_dur_.type_i_ > Duration_convert::no_smaller_than_i_s ) && ok() )
268 if ( Duration_convert::no_smaller_than_i_s
269 && cursor_dur_.dots_i_
270 && ( cursor_dur_.type_i_ >= Duration_convert::no_smaller_than_i_s )
273 if ( Duration_convert::no_smaller_than_i_s
274 && ( cursor_dur_.dots_i_ == 2 )
275 && ( cursor_dur_.type_i_ >= Duration_convert::no_smaller_than_i_s / 2 )
283 Duration_iterator::ok()
285 return ( cursor_dur_.type_i_
286 && !( ( cursor_dur_.type_i_ == 1 ) && ( cursor_dur_.dots_i_ > 2 ) ) );