]> git.donarmstrong.com Git - lilypond.git/blob - lily/command-request.cc
release: 1.1.14
[lilypond.git] / lily / command-request.cc
1 /*
2   commandrequest.cc -- implement Nonmusical reqs
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "command-request.hh"
10 #include "debug.hh"
11 #include "musical-request.hh"
12
13 void
14 Command_script_req::do_print() const
15 {}
16 Command_script_req::Command_script_req(){}
17 Command_script_req::~Command_script_req(){}
18
19
20
21
22
23
24 void
25 Cadenza_req::do_print() const
26 {
27 #ifndef NPRINT
28   DOUT << on_b_;
29 #endif
30 }
31
32 bool
33 Cadenza_req::do_equal_b (Request*r) const
34 {
35   Cadenza_req*cad =  dynamic_cast <Cadenza_req *> (r);
36   return cad && cad->on_b_ == on_b_;
37 }
38
39 Cadenza_req::Cadenza_req (bool b)
40 {
41   on_b_ =b;
42 }
43
44
45
46 bool
47 Bar_req::do_equal_b (Request*r) const
48 {
49   Bar_req * b = dynamic_cast <Bar_req *> (r);
50   return b && type_str_ == b->type_str_;
51 }
52
53 void
54 Command_tie_req::do_print () const
55 {}
56
57
58
59
60
61 void
62 Bar_req::do_print() const
63 {
64   DOUT << type_str_;
65 }
66
67 void
68 Bracket_req::do_print () const
69 {
70   Span_req::do_print ();
71 }
72
73 Bar_req::Bar_req (String s)
74 {
75   type_str_ = s;
76 }
77
78 Partial_measure_req::Partial_measure_req (Moment m)
79 {
80   duration_ =m;
81 }
82
83 bool
84 Partial_measure_req::do_equal_b (Request* r) const
85 {
86   Partial_measure_req *p = dynamic_cast <Partial_measure_req *> (r);
87
88   return p&& p->duration_ == duration_;
89 }
90
91
92
93
94 void
95 Timing_req::do_print() const{}
96
97
98
99
100 void
101 Command_req::do_print() const{}
102
103
104
105 void
106 Barcheck_req::do_print() const{}
107
108 bool
109 Barcheck_req::do_equal_b (Request*r) const
110 {
111   Barcheck_req *b = dynamic_cast<Barcheck_req*> (r);
112   return b;
113 }
114
115
116
117
118 void
119 Clef_change_req::do_print() const
120 {
121 #ifndef NPRINT
122   DOUT << clef_str_ ;
123 #endif
124 }
125
126 Clef_change_req::Clef_change_req (String s)
127 {
128   clef_str_ = s;
129 }
130
131
132
133
134
135
136 void
137 Partial_measure_req::do_print() const
138 {
139   DOUT << duration_;
140 }
141
142
143
144
145 void
146 Time_signature_change_req::do_print() const
147 {
148 #ifndef NPRINT
149   DOUT << beats_i_ << "/" << one_beat_i_;
150 #endif
151 }
152
153 bool
154 Time_signature_change_req::do_equal_b (Request * r) const
155 {
156   Time_signature_change_req * m = dynamic_cast <Time_signature_change_req *> (r);
157
158   return m && m->beats_i_ == beats_i_
159     && one_beat_i_ == m->one_beat_i_;
160 }
161
162 Time_signature_change_req::Time_signature_change_req()
163 {
164   beats_i_ = 0;
165   one_beat_i_ =0;
166 }
167
168
169 Tempo_req::Tempo_req()
170 {
171   metronome_i_ = 60;
172   dur_. durlog_i_ = 2;
173 }
174
175 void
176 Tempo_req::do_print() const
177 {
178   DOUT << dur_.str() << " = " << metronome_i_;
179 }
180
181
182 bool
183 Tempo_req::do_equal_b (Request *r) const
184 {
185   Tempo_req *t = dynamic_cast <Tempo_req *> (r);
186
187   return t&& t->dur_.length()== dur_.length () && metronome_i_ == t->metronome_i_;
188 }
189
190
191
192
193
194
195 void
196 Measure_grouping_req::do_print() const
197 {
198   for (int i=0; i < elt_length_arr_.size(); i++)
199     {
200       DOUT << beat_i_arr_[i] << "*" << elt_length_arr_[i].str () << " ";
201     }
202 }
203
204
205 bool
206 Measure_grouping_req::do_equal_b (Request*) const
207 {
208   return false;         // todo
209 }
210
211 void
212 Key_change_req::transpose (Musical_pitch d) 
213 {
214   if (ordinary_key_b_ ) 
215     { 
216       if (pitch_arr_.size () > 0) 
217         pitch_arr_[0].transpose(d);
218       else
219         {
220           warning(_ ("don't know how handle empty keys")); // TODO 
221         }
222     }
223   else
224     {
225       Array<Musical_pitch> old_pitch_arr_;
226       for (int i = 0; i < pitch_arr_.size(); i++)
227         {
228           old_pitch_arr_.push(pitch_arr_[i]);
229         }
230       // set accidentals for \key d (as in Key_engraver::read_req)
231       // (later called "new accidentals")
232       int p = d.semitone_pitch ();
233       /* Solve the equation 7*no_of_acc mod 12 = p, -6 <= no_of_acc <= 5 */
234       int no_of_acc = (7*p) % 12;
235       no_of_acc = (no_of_acc + 18) % 12 -6;
236
237       /* Correct from flats to sharps or vice versa */
238       if (no_of_acc * d.accidental_i_ < 0)
239       no_of_acc += 12 * sign (d.accidental_i_);
240     
241       pitch_arr_.clear ();
242       if (no_of_acc < 0) 
243         {
244           int accidental = 6 ; // First accidental: bes
245           for ( ; no_of_acc < 0 ; no_of_acc++ ) 
246             {
247               Musical_pitch m;
248               m.accidental_i_ = -1;
249               m.notename_i_ = accidental;
250               pitch_arr_.push(m);
251      
252               accidental = (accidental + 3) % 7 ;
253             }
254         }
255       else 
256         { 
257           int accidental = 3 ; // First accidental: fis
258           for ( ; no_of_acc > 0 ; no_of_acc-- ) 
259             {
260               Musical_pitch m;
261               m.accidental_i_ = 1;
262               m.notename_i_ = accidental;
263               pitch_arr_.push(m);
264    
265               accidental = (accidental + 4) % 7 ;
266             }
267         }
268       // Check if transposed old accidentals and the new ones coincide
269       no_of_acc = pitch_arr_.size();
270       int acc_found;
271       Musical_pitch mm;
272       for (int i=0; i < old_pitch_arr_.size(); i++)
273         {
274           acc_found = 0;
275           mm = old_pitch_arr_[i];
276           mm.transpose(d);
277           for (int j=0; ((j < no_of_acc) && (acc_found == 0)); j++)
278             {
279               if (pitch_arr_[j].notename_i_ == mm.notename_i_)
280                 {
281                   if (mm.accidental_i_ == 0)
282                     {
283                       // remove new accidental 
284                       pitch_arr_.del(j);
285                       no_of_acc--;
286                       acc_found = 1;
287                     }
288                   else
289                     {
290                       // change new accidental 
291                       pitch_arr_[j].accidental_i_ = mm.accidental_i_;
292                       acc_found = 1;
293                     }
294                 }
295             }
296           if (acc_found == 0)
297             {
298               // add transposed old accidental 
299               pitch_arr_.push(mm);
300             }
301         }
302     }
303 }
304
305
306 void
307 Key_change_req::squash_octaves()
308 {
309   for (int i=0; i < pitch_arr_.size(); i++)
310     {
311       pitch_arr_[i].octave_i_ = 0;
312     }
313 }
314
315 void
316 Key_change_req::do_print() const
317 {
318 #ifndef NPRINT
319   for (int i=0; i < pitch_arr_.size(); i++)
320     {
321       pitch_arr_[i].print();
322     }
323 #endif
324 }
325
326 Key_change_req::Key_change_req()
327 {
328   modality_i_ = 0;
329   ordinary_key_b_= false;
330 }
331
332
333
334 Break_req::Break_req ()
335 {
336   penalty_i_ = 0;
337 }
338
339 void
340 Break_req::do_print () const
341 {
342 }
343
344
345
346 Mark_req::Mark_req (String s)
347 {
348   str_ = s;
349 }
350
351 void
352 Mark_req::do_print () const
353 {
354   DOUT << str_;
355 }
356
357 int
358 Key_change_req::flats_i()
359 {
360   int flats_i = 0;
361   for (int i = 0; i < pitch_arr_.size(); i++)
362     {
363       if (pitch_arr_[i].accidental_i_ < 0)
364         flats_i -= pitch_arr_[i].accidental_i_;
365     }
366   return flats_i;
367 }
368
369 bool
370 Key_change_req::minor_b() const
371 {
372   return modality_i_ == 3;
373 }
374
375 int
376 Key_change_req::sharps_i()
377 {
378   int sharps_i = 0;
379   for (int i = 0; i < pitch_arr_.size(); i++)
380     {
381       if (pitch_arr_[i].accidental_i_ > 0)
382         sharps_i += pitch_arr_[i].accidental_i_;
383     }
384   return sharps_i;
385 }