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;
18 Array<Duration> Duration_convert::dur_array_s;
21 Duration_convert::dur2_str( Duration dur )
24 return String( "[" ) + String( dur.ticks_i_ ) + "]";
26 String str( dur.type_i_ );
27 str += String( '.', dur.dots_i_ );
29 str += String( "*" ) + String( dur.plet_.iso_i_ )
30 + String( "/" ) + String( dur.plet_.type_i_ );
36 Duration_convert::dur2_i( Duration dur, int division_1_i )
38 return dur2_mom( dur ) * Moment( division_1_i );
43 Duration_convert::dur2ticks_i( Duration dur )
47 return dur2_mom( dur ) * Moment( Duration::division_1_i_s );
51 Duration_convert::dur2_mom( Duration dur )
54 return Moment( dur.ticks_i_, Duration::division_1_i_s );
60 Moment mom = Moment( 1 , dur.type_i_ );
63 while ( dur.dots_i_-- ) {
68 return mom * plet_factor_mom( dur );
73 Duration_convert::i2_mom( int time_i, int division_1_i )
78 if ( division_1_i > 0 )
79 return Moment( time_i, division_1_i );
81 return Moment( -division_1_i, time_i );
86 Duration_convert::mom2_dur( Moment mom )
95 Duration dur = mom2standardised_dur( mom );
96 // if ( dur.mom() == mom )
97 if ( dur.length() == mom )
99 assert( midi_as_plet_b_s );
101 // dur.set_plet( type_mom, Duration::division_1_i_s / 4 );
103 // Moment as_plet_mom = mom / dur.mom();
104 Moment as_plet_mom = mom / dur.length();
105 as_plet_mom *= dur.plet_.mom();
106 long num = as_plet_mom.numerator().as_long();
107 long den = as_plet_mom.denominator().as_long();
108 dur.set_plet( num, den );
113 Duration_convert::mom2standardised_dur( Moment mom )
115 // if ( !dur_array_s.length_i() )
116 if ( !dur_array_s.size() )
118 // assert( dur_array_s.length_i() );
119 assert( dur_array_s.size() );
120 // for ( int i = 0; i < dur_array_s.length_i() - 1; i++ ) {
121 for ( int i = 0; i < dur_array_s.size() - 1; i++ ) {
122 Moment lower_mom = dur2_mom( dur_array_s[ i ] );
123 if ( mom <= lower_mom ) {
124 if ( i || ( mom / lower_mom > Moment( 3, 4 ) ) )
125 return dur_array_s[ i ];
127 return Duration( 0 );
129 Moment upper_mom = dur2_mom( dur_array_s[ i + 1 ] );
130 if ( ( mom < upper_mom )
131 && ( ( mom - lower_mom ) / mom
132 < ( upper_mom - mom ) / mom ) )
133 return dur_array_s[ i ];
135 // return dur_array_s[ dur_array_s.length_i() ];
136 return dur_array_s[ dur_array_s.size() - 1 ];
140 Duration_convert::set_array()
144 Duration_iterator iter_dur;
147 dur_array_s.push( iter_dur++ );
152 Duration_convert::plet_factor_mom( Duration dur )
154 return dur.plet_.mom();
158 Duration_convert::sync_f( Duration dur, Moment mom )
160 return mom / dur2_mom( dur );
164 Duration_convert::ticks2_dur( int ticks_i )
166 // Duration dur( 4, 0 );
167 // dur.set_plet( ticks_i, Duration::division_1_i_s / 4 );
169 Moment mom( ticks_i, Duration::division_1_i_s );
170 if ( midi_as_plet_b_s )
171 return mom2_dur( mom );
173 Duration dur = mom2standardised_dur( mom );
175 // if ( dur.mom() == mom )
176 if ( dur.length() == mom )
183 dur.set_ticks( ticks_i );
186 return mom2_dur( mom );
191 Duration_convert::ticks2standardised_dur( int ticks_i )
193 Moment mom( ticks_i, Duration::division_1_i_s );
194 Duration dur = mom2standardised_dur( mom );
196 // if ( dur.mom() != mom )
197 if ( dur.length() != mom )
198 warning( String( "duration not exact: " ) + String( (Real)mom ) );
202 Duration_iterator::Duration_iterator()
204 cursor_dur_.type_i_ = 128;
205 if ( Duration_convert::no_smaller_than_i_s )
206 cursor_dur_.type_i_ = Duration_convert::no_smaller_than_i_s;
207 // cursor_dur_.set_plet( 1, 1 );
211 Duration_iterator::operator ++(int)
213 return forward_dur();
217 Duration_iterator::operator ()()
222 Duration_iterator::operator bool()
228 Duration_iterator::dur()
234 Duration_iterator::forward_dur()
236 /* should do smart table? guessing:
250 Duration dur = cursor_dur_;
252 if ( !cursor_dur_.dots_i_ && !cursor_dur_.plet_b() ) {
253 cursor_dur_.type_i_ *= 2;
254 cursor_dur_.dots_i_ = 2;
256 else if ( cursor_dur_.dots_i_ == 2 ) {
257 assert( !cursor_dur_.plet_b() );
258 cursor_dur_.dots_i_ = 0;
259 cursor_dur_.type_i_ /= 4;
260 cursor_dur_.set_plet( 2, 3 );
262 else if ( cursor_dur_.plet_b()
263 && ( cursor_dur_.plet_.iso_i_ == 2 )
264 && ( cursor_dur_.plet_.type_i_ == 3 ) ) {
265 assert( !cursor_dur_.dots_i_ );
266 cursor_dur_.set_plet( 1, 1 );
267 cursor_dur_.type_i_ *= 2;
268 cursor_dur_.dots_i_ = 1;
270 else if ( cursor_dur_.dots_i_ == 1 ) {
271 assert( !cursor_dur_.plet_b() );
272 cursor_dur_.dots_i_ = 0;
273 cursor_dur_.type_i_ /= 2;
276 if ( Duration_convert::no_triplets_b_s
277 && cursor_dur_.plet_b() && ok() )
279 if ( Duration_convert::no_double_dots_b_s
280 && ( cursor_dur_.dots_i_ == 2 ) && ok() )
282 if ( Duration_convert::no_smaller_than_i_s
283 && ( cursor_dur_.type_i_ > Duration_convert::no_smaller_than_i_s ) && ok() )
285 if ( Duration_convert::no_smaller_than_i_s
286 && cursor_dur_.dots_i_
287 && ( cursor_dur_.type_i_ >= Duration_convert::no_smaller_than_i_s )
290 if ( Duration_convert::no_smaller_than_i_s
291 && ( cursor_dur_.dots_i_ == 2 )
292 && ( cursor_dur_.type_i_ >= Duration_convert::no_smaller_than_i_s / 2 )
300 Duration_iterator::ok()
302 return ( cursor_dur_.type_i_
303 && !( ( cursor_dur_.type_i_ == 1 ) && ( cursor_dur_.dots_i_ > 2 ) ) );