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