]> git.donarmstrong.com Git - lilypond.git/blob - lily/musical-request.cc
release: 0.1.11
[lilypond.git] / lily / musical-request.cc
1 /*
2   request.cc -- implement all musical requests.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include "musical-request.hh"
10 #include "misc.hh"
11 #include "debug.hh"
12 #include "script-def.hh"
13 #include "text-def.hh"
14 #include "music-list.hh"
15
16
17
18 IMPLEMENT_IS_TYPE_B1(Musical_req,Request);
19 void
20 Musical_req::do_print() const{}
21 void
22 Tie_req::do_print() const{}
23
24
25 /* *************** */
26    
27
28
29
30 IMPLEMENT_IS_TYPE_B1(Span_req,Musical_req);
31
32 void
33 Span_req::do_print() const    
34 {
35 #ifndef NPRINT
36   DOUT << spantype ;
37 #endif
38 }
39
40 Spacing_req::Spacing_req()
41 {
42   next = 0;
43   distance = 0;
44   strength = 0;
45 }
46
47 IMPLEMENT_IS_TYPE_B1(Spacing_req,Request);
48
49 void
50 Spacing_req::do_print() const
51 {
52 #ifndef NPRINT
53   DOUT << "next " << next << "dist " << distance << "strength\n";
54 #endif
55 }
56
57
58 IMPLEMENT_IS_TYPE_B2(Blank_req,Spacing_req,Rhythmic_req);
59
60 void
61 Blank_req::do_print() const
62 {
63   Spacing_req::do_print();
64 }
65 /* *************** */
66
67 Melodic_req::Melodic_req()
68 {
69   notename_i_ = 0;
70   octave_i_ = 0;
71   accidental_i_ = 0;
72 }
73
74 void
75 Melodic_req::transpose (Melodic_req const & delta)
76 {
77   int old_pitch = pitch();
78   int delta_pitch = delta.pitch();
79   octave_i_ += delta.octave_i_;
80   notename_i_ += delta.notename_i_;
81   while  (notename_i_ >= 7) 
82     {
83         notename_i_ -= 7;
84         octave_i_ ++;
85     }
86
87   int new_pitch = pitch();
88   int delta_acc = new_pitch - old_pitch - delta_pitch;
89   
90   accidental_i_ -= delta_acc;
91   if (abs (accidental_i_) > 2) 
92     {
93         delta.warning ("transposition makes accidental larger than 2");
94     }
95 }
96
97 IMPLEMENT_IS_TYPE_B1(Melodic_req,Musical_req);
98
99 bool
100 Melodic_req::do_equal_b (Request*r) const
101 {
102   Melodic_req* m= r->musical()->melodic ();
103   return !compare (*m, *this);
104 }
105
106 int
107 Melodic_req::compare (Melodic_req const &m1 , Melodic_req const&m2)
108 {
109   int o=  m1.octave_i_ - m2.octave_i_;
110   int n = m1.notename_i_ - m2.notename_i_;
111   int a = m1.accidental_i_ - m2.accidental_i_;
112   
113   if (o)
114         return o;
115   if (n)
116         return n;
117   if (a)
118         return a;
119   return 0;
120 }
121
122 void
123 Melodic_req::do_print() const
124 {
125 #ifndef NPRINT
126   DOUT << "notename: " << notename_i_ 
127          << " acc: " <<accidental_i_<<" oct: "<< octave_i_;
128 #endif
129 }
130
131 int
132 Melodic_req::height() const
133 {
134   return  notename_i_ + octave_i_*7;
135 }
136
137 /*
138   should be settable from input to allow "viola"-mode
139  */
140 static Byte pitch_byte_a[  ] = { 0, 2, 4, 5, 7, 9, 11 };        
141
142 int
143 Melodic_req::pitch() const
144 {
145   return  pitch_byte_a[ notename_i_ % 7 ] + accidental_i_ + octave_i_ * 12;
146 }
147
148 /* *************** */
149 int
150 Rhythmic_req::compare (Rhythmic_req const &r1, Rhythmic_req const &r2)
151 {
152   return (r1.duration() - r2.duration ());
153 }
154
155 bool
156 Rhythmic_req::do_equal_b (Request*r) const
157 {
158   Rhythmic_req* rh = r->musical()->rhythmic ();
159
160   return !compare (*this, *rh);
161 }
162
163 void
164 Rhythmic_req::set_duration (Duration d)
165 {
166   duration_ = d;
167 }
168
169 Rhythmic_req::Rhythmic_req()
170 {
171 }
172
173
174 IMPLEMENT_IS_TYPE_B1(Rhythmic_req,Musical_req);
175
176 void
177 Rhythmic_req::do_print() const
178 {
179 #ifndef NPRINT
180   DOUT << "duration { " <<duration_.str() << "}";
181 #endif
182 }
183
184
185 Moment
186 Rhythmic_req::duration() const {    
187   return duration_.length();
188 }
189 /* *************** */
190
191 Lyric_req::Lyric_req (Text_def* def_p)
192   :Text_req (0, def_p)
193 {
194   def_p->align_i_ = 0;  // centre
195   dir_ = DOWN;          // lyrics below (invisible) staff
196 }
197
198
199 IMPLEMENT_IS_TYPE_B2(Lyric_req,Musical_req,Rhythmic_req);
200
201 void
202 Lyric_req::do_print() const
203 {    
204   Rhythmic_req::do_print();
205   Text_req::do_print();
206 }
207
208 /* *************** */
209 bool
210 Note_req::do_equal_b (Request*r) const
211 {
212   return Rhythmic_req::do_equal_b (r) && Melodic_req::do_equal_b (r);
213 }
214
215
216 Note_req::Note_req()
217 {
218   forceacc_b_ = false;
219 }
220
221 IMPLEMENT_IS_TYPE_B2(Note_req,Melodic_req,Rhythmic_req);
222
223 void
224 Note_req::do_print() const
225 {
226 #ifndef NPRINT
227   Melodic_req::do_print();
228   if (forceacc_b_) 
229     {
230         DOUT << " force accidental\n";
231     }
232   Rhythmic_req::do_print();
233 #endif
234 }
235 /* *************** */
236
237 IMPLEMENT_IS_TYPE_B1(Rest_req,Rhythmic_req);
238
239 void
240 Rest_req::do_print() const
241 {
242       Rhythmic_req::do_print();
243 }
244
245 /* *************** */
246 Beam_req::Beam_req()
247 {
248   nplet = 0;
249 }
250
251 IMPLEMENT_IS_TYPE_B1(Beam_req,Span_req);
252 void
253 Beam_req::do_print() const{}
254 /* *************** */
255
256 IMPLEMENT_IS_TYPE_B1(Slur_req,Span_req);
257 void
258 Slur_req::do_print() const{}
259 /* *************** */
260
261
262 bool
263 Span_req:: do_equal_b (Request*r) const
264 {
265   Span_req * s = r->span();
266   return spantype - s->spantype;
267 }
268
269 Span_req::Span_req()
270 {
271   spantype = NOSPAN;
272 }
273
274 /* *************** */
275 Script_req::Script_req (Script_req const&s)
276 {
277   dir_ = s.dir_;
278   scriptdef_p_ = s.scriptdef_p_ ? s.scriptdef_p_->clone() : 0;
279 }
280
281 /*
282   don't check dirs?
283   
284   (d1.dir_ == d2.dir_)
285  */
286 bool
287 Script_req::do_equal_b (Request*r) const
288 {
289   Script_req * s = r->script();
290   
291   return  scriptdef_p_->equal_b (*s->scriptdef_p_);
292 }
293
294 Script_req::Script_req()
295 {
296   dir_ = CENTER;
297   scriptdef_p_ = 0;
298 }
299
300
301 IMPLEMENT_IS_TYPE_B1(Script_req,Request);
302
303 void
304 Script_req::do_print() const
305 {
306 #ifndef NPRINT
307   DOUT << " dir " << dir_ ;
308   scriptdef_p_->print();
309 #endif
310 }
311
312 void
313 Musical_script_req::do_print() const
314 {
315   Script_req::do_print();
316 }
317
318
319 IMPLEMENT_IS_TYPE_B2(Musical_script_req,Musical_req, Script_req);
320
321
322 Script_req::~Script_req()
323 {
324   delete scriptdef_p_;
325 }
326 /* *************** */
327
328
329 Text_req::~Text_req()
330 {
331   delete tdef_p_;
332   tdef_p_ = 0;
333 }
334
335 Text_req::Text_req (Text_req const& src)
336 {
337   tdef_p_ = new Text_def (*src.tdef_p_);
338   dir_ = src.dir_;
339 }
340
341 Text_req::Text_req (int dir_i, Text_def* tdef_p)        
342 {
343   dir_ = Direction(dir_i);
344   tdef_p_ = tdef_p;
345 }
346
347
348 IMPLEMENT_IS_TYPE_B1(Text_req,Musical_req);
349
350 void
351 Text_req::do_print() const
352 {
353 #ifndef NPRINT
354   DOUT << " dir " << dir_ ;
355   tdef_p_->print();
356 #endif
357 }
358
359 /* *************** */
360
361
362 IMPLEMENT_IS_TYPE_B1(Skip_req,Musical_req);
363
364 void
365 Skip_req::do_print() const
366 {
367 #ifndef NPRINT
368
369   DOUT << "duration: " << duration();
370 #endif
371 }
372
373
374
375 IMPLEMENT_IS_TYPE_B1(Dynamic_req,Musical_req);
376
377 void
378 Dynamic_req::do_print() const
379 {
380   Musical_req::do_print();
381 }
382
383
384 IMPLEMENT_IS_TYPE_B1(Absolute_dynamic_req,Musical_req);
385
386 void
387 Absolute_dynamic_req::do_print() const
388 {
389 #ifndef NPRINT
390   Dynamic_req::do_print();
391   DOUT << " loudness " <<loudness_str (loudness_);
392 #endif
393 }
394
395 String
396 Dynamic_req::loudness_str (Loudness l) 
397 {
398   switch (l) 
399     {
400   case FFF: return "fff";
401   case FF: return "ff";
402   case F: return "f";
403   case MF: return "mf";
404   case MP: return "mp";
405   case P: return "p";
406   case PP: return "pp";
407   case PPP: return "ppp";
408     }
409   assert (false);
410   return "";
411 }
412
413 Absolute_dynamic_req::Absolute_dynamic_req()
414 {
415   loudness_ = MF;
416 }
417
418
419 Span_dynamic_req::Span_dynamic_req()
420 {
421   dynamic_dir_  = CENTER;
422 }
423
424
425 IMPLEMENT_IS_TYPE_B1(Span_dynamic_req,Musical_req);
426
427 void
428 Span_dynamic_req::do_print() const
429 {
430 #ifndef NPRINT
431   Span_req::do_print();
432   DOUT << "softer/louder: " <<dynamic_dir_;
433 #endif
434 }
435
436
437 IMPLEMENT_IS_TYPE_B1(Tie_req,Musical_req);
438
439