lilypond-1.3.6
[lilypond.git] / src / duration.cc
bloba7a1c5dfcf5d74608acfb689beb3e856423ff228
1 //
2 // duration.cc -- implement Duration, Plet, Duration_convert, Duration_iterator
3 //
4 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
6 // split into 4?
8 #include "proto.hh" // ugh, these all for midi-main.hh
9 #include "plist.hh"
10 #include "string.hh"
11 #include "source-file.hh"
12 #include "source.hh"
13 #include "midi-main.hh" // *tors
15 #include "string.hh"
16 #include "moment.hh"
17 #include "duration.hh"
19 Duration::Duration( int type_i, int dots_i = 0, Plet* plet_l )
21 type_i_ = type_i;
22 dots_i_ = dots_i;
23 plet_p_ = 0;
24 set_plet( plet_l );
27 Duration::Duration( Duration const& dur_c_r )
29 type_i_ = 0;
30 dots_i_ = 0;
31 plet_p_ = 0;
32 *this = dur_c_r;
35 Duration::~Duration()
37 delete plet_p_;
40 Duration const&
41 Duration::operator =( Duration const& dur_c_r )
43 if ( &dur_c_r == this )
44 return *this;
46 type_i_ = dur_c_r.type_i_;
47 dots_i_ = dur_c_r.dots_i_;
48 set_plet( dur_c_r.plet_p_ );
50 return *this;
53 void
54 Duration::set_plet( Plet* plet_l )
56 delete plet_p_;
57 plet_p_ = 0;
58 if ( plet_l )
59 plet_p_ = new Plet( *plet_l );
62 Plet::Plet( int iso_i, int type_i )
64 iso_i_ = iso_i;
65 type_i_ = type_i;
68 Plet::Plet( Plet const& plet_c_r )
70 iso_i_ = plet_c_r.iso_i_;
71 type_i_ = plet_c_r.type_i_;
74 String
75 Duration_convert::dur2_str( Duration dur )
77 String str( dur.type_i_ );
78 str += String( '.', dur.dots_i_ );
79 if ( dur.plet_p_ )
80 str += String( "*" ) + String( dur.plet_p_->iso_i_ )
81 + String( "/" ) + String( dur.plet_p_->type_i_ );
82 return str;
85 int
86 Duration_convert::dur2_i( Duration dur, int division_1_i )
88 return dur2_mom( dur ) * Moment( division_1_i );
91 Moment
92 Duration_convert::dur2_mom( Duration dur )
94 if ( !dur.type_i_ )
95 return 0;
97 Moment mom = Moment( 1 , dur.type_i_ );
99 Moment delta = mom;
100 while ( dur.dots_i_-- ) {
101 delta /= 2.0;
102 mom += delta;
105 return mom * plet_factor_mom( dur );
108 Duration
109 Duration_convert::mom2_dur( Moment mom )
111 /* this is cute,
112 but filling an array using Duration_iterator
113 might speed things up, a little
115 Duration_iterator iter_dur;
116 assert( iter_dur );
117 while ( iter_dur ) {
118 Duration lower_dur = iter_dur++;
119 Duration upper_dur( 0 );
120 if ( iter_dur )
121 upper_dur = iter_dur();
122 Moment lower_mom = dur2_mom( lower_dur );
123 Moment upper_mom = dur2_mom( upper_dur );
124 if ( mom == lower_mom )
125 return lower_dur;
126 if ( mom == upper_mom ) // don-t miss last (sic)
127 return upper_dur;
128 if ( ( mom >= lower_mom ) && ( mom <= upper_mom ) ) {
129 warning( String( "duration not exact: " ) + String( (Real)mom ) , 0 );
130 if ( abs( mom - lower_mom ) < abs( mom - upper_mom ) )
131 return lower_dur;
132 else
133 return upper_dur;
135 lower_dur = upper_dur;
137 return Duration( 0 );
140 Moment
141 Duration_convert::plet_factor_mom( Duration dur )
143 if ( !dur.plet_p_ )
144 return 1;
145 return Moment( dur.plet_p_->iso_i_, dur.plet_p_->type_i_ );
148 Real
149 Duration_convert::sync_f( Duration dur, Moment mom )
151 return mom / dur2_mom( dur );
154 Moment
155 Duration_convert::i2_mom( int time_i, int division_1_i )
157 if ( !time_i )
158 return Moment( 0 );
160 if ( division_1_i > 0 )
161 return Moment( time_i, division_1_i );
162 else
163 return Moment( -division_1_i, time_i );
166 Duration_iterator::Duration_iterator()
168 cursor_dur_.type_i_ = 128;
169 cursor_dur_.set_plet( 0 );
172 Duration
173 Duration_iterator::operator ++(int)
175 return forward_dur();
178 Duration
179 Duration_iterator::operator ()()
181 return dur();
184 Duration_iterator::operator bool()
186 return ok();
189 Duration
190 Duration_iterator::dur()
192 return cursor_dur_;
195 Duration
196 Duration_iterator::forward_dur()
198 // should do smart table? guessing:
199 // duration wholes
200 // 16 0.0625
201 // 32.. 0.0703
202 // 8:2/3 0.0833
203 // 16. 0.0938
204 // 8 0.1250
205 // 16.. 0.1406
206 // 4:2/3 0.1667
207 // 8. 0.1875
209 assert( ok() );
211 Duration dur = cursor_dur_;
213 if ( !cursor_dur_.dots_i_ && !cursor_dur_.plet_p_ ) {
214 cursor_dur_.type_i_ *= 2;
215 cursor_dur_.dots_i_ = 2;
217 else if ( cursor_dur_.dots_i_ == 2 ) {
218 assert( !cursor_dur_.plet_p_ );
219 cursor_dur_.dots_i_ = 0;
220 cursor_dur_.type_i_ /= 4;
221 cursor_dur_.set_plet( &Plet( 2, 3 ) );
223 else if ( cursor_dur_.plet_p_
224 && ( cursor_dur_.plet_p_->iso_i_ == 2 )
225 && ( cursor_dur_.plet_p_->type_i_ == 3 ) ) {
226 assert( !cursor_dur_.dots_i_ );
227 cursor_dur_.set_plet( 0 );
228 cursor_dur_.type_i_ *= 2;
229 cursor_dur_.dots_i_ = 1;
231 else if ( cursor_dur_.dots_i_ == 1 ) {
232 assert( !cursor_dur_.plet_p_ );
233 cursor_dur_.dots_i_ = 0;
234 cursor_dur_.type_i_ /= 2;
237 // ugh
238 if ( no_triplets_bo_g && cursor_dur_.plet_p_ && ok() )
239 forward_dur();
241 return dur;
244 bool
245 Duration_iterator::ok()
247 return ( cursor_dur_.type_i_
248 && !( ( cursor_dur_.type_i_ == 1 ) && ( cursor_dur_.dots_i_ > 2 ) ) );