]> git.donarmstrong.com Git - lilypond.git/blob - lib/duration-convert.cc
4a46bf9e64b69caacb6abbd9141add9a9e26e828
[lilypond.git] / lib / duration-convert.cc
1 /*
2   duration-convert.cc -- implement Duration_convert
3
4   source file of the LilyPond music typesetter
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8 #include <assert.h>
9 #include "duration-convert.hh"
10 #include "warn.hh"
11
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;
19         
20 String 
21 Duration_convert::dur2_str( Duration dur )
22 {
23         if ( dur.ticks_i_ )
24                 return String( "["  ) + String( dur.ticks_i_ ) + "]";
25
26         String str( dur.type_i_ );
27         str += String( '.', dur.dots_i_ );
28         if ( dur.plet_b())
29                 str += String( "*" ) + String( dur.plet_.iso_i_ )
30                         + String( "/" ) + String( dur.plet_.type_i_ );
31         return str;
32 }
33
34 #if 0
35 int
36 Duration_convert::dur2_i( Duration dur, int division_1_i )
37 {
38         return dur2_mom( dur ) * Moment( division_1_i );
39 }
40 #endif
41
42 int
43 Duration_convert::dur2ticks_i( Duration dur )
44 {
45         if ( dur.ticks_i_ )
46                 return dur.ticks_i_;
47         return dur2_mom( dur ) * Moment( Duration::division_1_i_s );
48 }
49
50 Moment
51 Duration_convert::dur2_mom( Duration dur )
52 {
53         if ( dur.ticks_i_ )
54                 return Moment( dur.ticks_i_, Duration::division_1_i_s );        
55
56         // or simply assert?
57         if ( !dur.type_i_ )
58                 return Moment( 0 );
59
60         Moment mom = Moment( 1 , dur.type_i_ );
61
62         Moment delta = mom;
63         while ( dur.dots_i_-- ) {
64                 delta /= 2.0;
65                 mom += delta;
66         }
67
68         return mom * plet_factor_mom( dur );    
69 }
70
71 #if 0
72 Moment
73 Duration_convert::i2_mom( int time_i, int division_1_i )
74 {
75         if ( !time_i )
76                 return Moment( 0 );
77
78         if ( division_1_i > 0 )
79                 return Moment( time_i, division_1_i );
80         else 
81                 return Moment( -division_1_i, time_i );
82 }
83 #endif
84
85 Duration
86 Duration_convert::mom2_dur( Moment mom )
87 {
88     if (!mom) {
89         Duration dur;
90         dur.set_plet(0,1);
91         return dur;
92     }
93         
94
95         Duration dur = mom2standardised_dur( mom );
96 //      if ( !dur.mom() || ( dur.mom() == mom ) )
97         if ( !dur.length() || ( dur.length() == mom ) )
98                 return dur;
99         assert( midi_as_plet_b_s );
100
101 //      dur.set_plet( type_mom, Duration::division_1_i_s / 4 ); 
102
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 );
109         return dur;
110 }
111
112 Duration
113 Duration_convert::mom2standardised_dur( Moment mom )
114 {
115 //      if ( !dur_array_s.length_i() )
116         if ( !dur_array_s.size() )
117                 set_array();
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                         // all arbitrary, but 3/4 will get rid of the noise...
125                         // kinda ok
126                         if ( i || ( mom / lower_mom > Moment( 3, 4 ) ) )
127                                 return dur_array_s[ i ];
128                         else
129                                 return Duration( 0 );
130                 }
131                 Moment upper_mom = dur2_mom( dur_array_s[ i + 1 ] );
132 #if 0  // % deviation of upper/lower from mom, perhaps it's better to use
133                 if ( ( mom < upper_mom )
134                          && ( ( mom - lower_mom ) / mom
135                                 < ( upper_mom - mom ) / mom ) )
136 #else // % deviation of mom from lower/upper?
137                 if ( ( mom < upper_mom )
138                          && ( ( mom - lower_mom ) / lower_mom
139                                 < ( upper_mom - mom ) / upper_mom ) )
140 #endif
141                         return dur_array_s[ i ];
142         }
143 //      return dur_array_s[ dur_array_s.length_i() ];
144         return dur_array_s[ dur_array_s.size() - 1 ];
145 }
146
147 void
148 Duration_convert::set_array()
149 {
150         dur_array_s.clear();
151
152         Duration_iterator iter_dur;
153         assert( iter_dur );
154         while ( iter_dur )
155                 dur_array_s.push( iter_dur++ );
156 }
157
158
159 Moment
160 Duration_convert::plet_factor_mom( Duration dur )
161 {
162         return dur.plet_.mom();
163 }
164
165 Real
166 Duration_convert::sync_f( Duration dur, Moment mom )
167 {
168         return mom / dur2_mom( dur );
169 }
170
171 Duration
172 Duration_convert::ticks2_dur( int ticks_i )
173 {
174 //              Duration dur( 4, 0 );
175 //              dur.set_plet( ticks_i, Duration::division_1_i_s / 4 ); 
176
177         Moment mom( ticks_i, Duration::division_1_i_s );
178         if ( midi_as_plet_b_s )
179                 return mom2_dur( mom );
180
181         Duration dur = mom2standardised_dur( mom );
182
183 //      if ( dur.mom() == mom )
184         if ( dur.length() == mom )
185                 return dur;
186                 
187 // huh?
188 #if 0
189         dur.type_i_ = 0;
190         dur.dots_i_ = 0;
191         dur.set_ticks( ticks_i );
192         return dur;
193 #else
194         return mom2_dur( mom );
195 #endif
196 }
197
198 Duration
199 Duration_convert::ticks2standardised_dur( int ticks_i )
200 {
201         Moment mom( ticks_i, Duration::division_1_i_s );
202         Duration dur = mom2standardised_dur( mom );
203         return dur;
204 }
205
206 Duration_iterator::Duration_iterator()
207 {
208         cursor_dur_.type_i_ = 128;
209         if ( Duration_convert::no_smaller_than_i_s )
210                 cursor_dur_.type_i_ = Duration_convert::no_smaller_than_i_s;
211 //      cursor_dur_.set_plet( 1, 1 );
212 }
213
214 Duration 
215 Duration_iterator::operator ++(int)
216 {
217         return forward_dur();
218 }
219
220 Duration
221 Duration_iterator::operator ()()
222 {
223         return dur();
224 }
225
226 Duration_iterator::operator bool()
227 {
228         return ok();
229 }
230
231 Duration
232 Duration_iterator::dur()
233 {
234         return cursor_dur_;
235 }
236
237 Duration
238 Duration_iterator::forward_dur()
239 {
240         /* should do smart table? guessing: 
241                 duration wholes
242                 16      0.0625
243                 32..    0.0703
244                 8:2/3   0.0833
245                 16.     0.0938
246                 8       0.1250
247                 16..    0.1406
248                 4:2/3   0.1667
249                 8.      0.1875
250                 
251         */
252         assert( ok() );
253
254         Duration dur = cursor_dur_;
255
256         if ( !cursor_dur_.dots_i_ && !cursor_dur_.plet_b() ) {
257                 cursor_dur_.type_i_ *= 2;
258                 cursor_dur_.dots_i_ = 2;
259         }
260         else if ( cursor_dur_.dots_i_ == 2 ) {
261                 assert( !cursor_dur_.plet_b() );
262                 cursor_dur_.dots_i_ = 0;
263                 cursor_dur_.type_i_ /= 4;
264                 cursor_dur_.set_plet( 2, 3 );
265         }
266         else if ( cursor_dur_.plet_b() 
267                 && ( cursor_dur_.plet_.iso_i_ == 2 )
268                 && ( cursor_dur_.plet_.type_i_ == 3 ) ) {
269                 assert( !cursor_dur_.dots_i_ );
270                 cursor_dur_.set_plet( 1, 1 );
271                 cursor_dur_.type_i_ *= 2;
272                 cursor_dur_.dots_i_ = 1;
273         }
274         else if ( cursor_dur_.dots_i_ == 1 ) {
275                 assert( !cursor_dur_.plet_b() );
276                 cursor_dur_.dots_i_ = 0;
277                 cursor_dur_.type_i_ /= 2;
278         }
279                 
280         if ( Duration_convert::no_triplets_b_s
281              && cursor_dur_.plet_b() && ok() )
282             forward_dur();
283         if ( Duration_convert::no_double_dots_b_s 
284              && ( cursor_dur_.dots_i_ == 2 ) && ok() )
285                 forward_dur();
286         if ( Duration_convert::no_smaller_than_i_s
287              && ( cursor_dur_.type_i_ > Duration_convert::no_smaller_than_i_s ) && ok() )
288                 forward_dur();
289         if ( Duration_convert::no_smaller_than_i_s
290              && cursor_dur_.dots_i_
291              && ( cursor_dur_.type_i_ >= Duration_convert::no_smaller_than_i_s )
292              && ok() )
293                 forward_dur();
294         if ( Duration_convert::no_smaller_than_i_s
295              && ( cursor_dur_.dots_i_ == 2 )
296              && ( cursor_dur_.type_i_ >= Duration_convert::no_smaller_than_i_s / 2 )
297              && ok() )
298                 forward_dur();
299
300         return dur;
301 }
302
303 bool
304 Duration_iterator::ok()
305 {
306         return ( cursor_dur_.type_i_ 
307                 && !( ( cursor_dur_.type_i_ == 1 ) && ( cursor_dur_.dots_i_ > 2 ) ) );
308 }