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