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