]> git.donarmstrong.com Git - lilypond.git/blob - lily/musical-request.cc
release: 0.1.62
[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--1998 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 IMPLEMENT_IS_TYPE_B1 (Spacing_req,Request);
41
42 Spacing_req::Spacing_req ()
43 {
44   next = 0;
45   distance = 0;
46   strength = 0;
47 }
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 IMPLEMENT_IS_TYPE_B1 (Abbreviation_req, Musical_req);
58
59 Abbreviation_req::Abbreviation_req ()
60 {
61   type_i_ = 0;
62 }
63
64 void
65 Abbreviation_req::do_print () const
66 {
67 #ifndef NPRINT
68   DOUT << "type " << type_i_ << "\n";
69 #endif
70 }
71
72
73 IMPLEMENT_IS_TYPE_B2 (Blank_req,Spacing_req,Rhythmic_req);
74
75 void
76 Blank_req::do_print () const
77 {
78   Spacing_req::do_print ();
79 }
80 /* *************** */
81
82 Melodic_req::Melodic_req ()
83 {
84   notename_i_ = 0;
85   octave_i_ = 0;
86   accidental_i_ = 0;
87 }
88
89 void
90 Melodic_req::transpose (Melodic_req const * delta)
91 {
92   int old_pitch = pitch ();
93   int delta_pitch = delta->pitch ();
94   octave_i_ += delta->octave_i_;
95   notename_i_ += delta->notename_i_;
96   while  (notename_i_ >= 7)
97     {
98         notename_i_ -= 7;
99         octave_i_ ++;
100     }
101
102   int new_pitch = pitch ();
103   int delta_acc = new_pitch - old_pitch - delta_pitch;
104
105   accidental_i_ -= delta_acc;
106   if (abs (accidental_i_) > 2)
107     {
108         delta->warning (_ ("transposition makes accidental larger than 2"));
109     }
110 }
111
112 IMPLEMENT_IS_TYPE_B1 (Melodic_req,Musical_req);
113
114 bool
115 Melodic_req::do_equal_b (Request*r) const
116 {
117   Melodic_req* m= r->musical ()->melodic ();
118   return !compare (*m, *this);
119 }
120
121 int
122 Melodic_req::compare (Melodic_req const &m1 , Melodic_req const&m2)
123 {
124   int o=  m1.octave_i_ - m2.octave_i_;
125   int n = m1.notename_i_ - m2.notename_i_;
126   int a = m1.accidental_i_ - m2.accidental_i_;
127
128   if (o)
129         return o;
130   if (n)
131         return n;
132   if (a)
133         return a;
134   return 0;
135 }
136
137 void
138 Melodic_req::do_print () const
139 {
140 #ifndef NPRINT
141   DOUT << "notename: " << notename_i_
142          << " acc: " <<accidental_i_<<" oct: "<< octave_i_;
143 #endif
144 }
145
146 int
147 Melodic_req::height () const
148 {
149   return  notename_i_ + octave_i_*7;
150 }
151
152 /*
153   should be settable from input to allow "viola"-mode
154  */
155 static Byte pitch_byte_a[  ] = { 0, 2, 4, 5, 7, 9, 11 };
156
157 int
158 Melodic_req::pitch () const
159 {
160   return  pitch_byte_a[ notename_i_ % 7 ] + accidental_i_ + octave_i_ * 12;
161 }
162
163 /* *************** */
164 int
165 Rhythmic_req::compare (Rhythmic_req const &r1, Rhythmic_req const &r2)
166 {
167   return (r1.duration () - r2.duration ());
168 }
169
170 bool
171 Rhythmic_req::do_equal_b (Request*r) const
172 {
173   Rhythmic_req* rh = r->musical ()->rhythmic ();
174
175   return !compare (*this, *rh);
176 }
177
178 void
179 Rhythmic_req::set_duration (Duration d)
180 {
181   duration_ = d;
182 }
183
184 Rhythmic_req::Rhythmic_req ()
185 {
186 }
187
188
189 IMPLEMENT_IS_TYPE_B1 (Rhythmic_req,Musical_req);
190
191 void
192 Rhythmic_req::do_print () const
193 {
194 #ifndef NPRINT
195   DOUT << "duration { " <<duration_.str () << "}";
196 #endif
197 }
198
199
200 Moment
201 Rhythmic_req::duration () const
202 {
203   return duration_.length ();
204 }
205 /* *************** */
206
207 Lyric_req::Lyric_req (Text_def* def_p)
208   :Text_req (0, def_p)
209 {
210   def_p->align_i_ = CENTER;     // centre
211   dir_ = DOWN;          // lyrics below (invisible) staff
212 }
213
214
215 IMPLEMENT_IS_TYPE_B2 (Lyric_req,Musical_req,Rhythmic_req);
216
217 void
218 Lyric_req::do_print () const
219 {
220   Rhythmic_req::do_print ();
221   Text_req::do_print ();
222 }
223
224 /* *************** */
225 bool
226 Note_req::do_equal_b (Request*r) const
227 {
228   return Rhythmic_req::do_equal_b (r) && Melodic_req::do_equal_b (r);
229 }
230
231
232 Note_req::Note_req ()
233 {
234   forceacc_b_ = false;
235 }
236
237 IMPLEMENT_IS_TYPE_B2 (Note_req,Melodic_req,Rhythmic_req);
238
239 void
240 Note_req::do_print () const
241 {
242 #ifndef NPRINT
243   Melodic_req::do_print ();
244   if (forceacc_b_)
245     {
246         DOUT << " force accidental\n";
247     }
248   Rhythmic_req::do_print ();
249 #endif
250 }
251 /* *************** */
252
253 IMPLEMENT_IS_TYPE_B1 (Rest_req, Rhythmic_req);
254
255 void
256 Rest_req::do_print () const
257 {
258       Rhythmic_req::do_print ();
259 }
260
261 /* *************** */
262
263
264
265 IMPLEMENT_IS_TYPE_B1 (Multi_measure_rest_req, Rhythmic_req);
266
267 void
268 Multi_measure_rest_req::do_print () const
269 {
270       Rhythmic_req::do_print ();
271 }
272
273
274 /* *************** */
275
276 IMPLEMENT_IS_TYPE_B1 (Beam_req,Span_req);
277
278 Beam_req::Beam_req ()
279 {
280 }
281
282 void
283 Beam_req::do_print () const
284 {
285 }
286
287 /* *************** */
288
289 IMPLEMENT_IS_TYPE_B1 (Abbreviation_beam_req, Span_req);
290
291 Abbreviation_beam_req::Abbreviation_beam_req ()
292 {
293   type_i_ = 0;
294 }
295
296 void
297 Abbreviation_beam_req::do_print () const
298 {
299 }
300
301 IMPLEMENT_IS_TYPE_B1 (Slur_req,Span_req);
302 void
303 Slur_req::do_print () const
304 {
305 }
306
307 IMPLEMENT_IS_TYPE_B1 (Plet_req,Span_req);
308
309 Plet_req::Plet_req ()
310 {
311   plet_i_ = 0;
312 }
313
314 void
315 Plet_req::do_print () const
316 {
317 }
318
319 /* *************** */
320
321 bool
322 Span_req:: do_equal_b (Request*r) const
323 {
324   Span_req * s = r->span ();
325   return spantype == s->spantype;
326 }
327
328 Span_req::Span_req ()
329 {
330   spantype = NOSPAN;
331 }
332
333 /* *************** */
334 Script_req::Script_req (Script_req const&s)
335 {
336   dir_ = s.dir_;
337   scriptdef_p_ = s.scriptdef_p_ ? s.scriptdef_p_->clone () : 0;
338 }
339
340 /*
341   don't check dirs?
342
343   (d1.dir_ == d2.dir_)
344  */
345 bool
346 Script_req::do_equal_b (Request*r) const
347 {
348   Script_req * s = r->script ();
349
350   return  scriptdef_p_->equal_b (*s->scriptdef_p_);
351 }
352
353 Script_req::Script_req ()
354 {
355   dir_ = CENTER;
356   scriptdef_p_ = 0;
357 }
358
359
360 IMPLEMENT_IS_TYPE_B1 (Script_req,Request);
361
362 void
363 Script_req::do_print () const
364 {
365 #ifndef NPRINT
366   DOUT << " dir " << dir_ ;
367   scriptdef_p_->print ();
368 #endif
369 }
370
371 void
372 Musical_script_req::do_print () const
373 {
374   Script_req::do_print ();
375 }
376
377
378 IMPLEMENT_IS_TYPE_B2 (Musical_script_req,Musical_req, Script_req);
379
380
381 Script_req::~Script_req ()
382 {
383   delete scriptdef_p_;
384 }
385 /* *************** */
386
387
388 Text_req::~Text_req ()
389 {
390   delete tdef_p_;
391   tdef_p_ = 0;
392 }
393
394 Text_req::Text_req (Text_req const& src)
395 {
396   tdef_p_ = new Text_def (*src.tdef_p_);
397   dir_ = src.dir_;
398 }
399
400 Text_req::Text_req (int dir_i, Text_def* tdef_p)
401 {
402   dir_ = Direction (dir_i);
403   tdef_p_ = tdef_p;
404 }
405
406
407 IMPLEMENT_IS_TYPE_B1 (Text_req,Musical_req);
408
409 void
410 Text_req::do_print () const
411 {
412 #ifndef NPRINT
413   DOUT << " dir " << dir_ ;
414   tdef_p_->print ();
415 #endif
416 }
417
418 /* *************** */
419
420
421 IMPLEMENT_IS_TYPE_B1 (Skip_req,Musical_req);
422
423 void
424 Skip_req::do_print () const
425 {
426 #ifndef NPRINT
427
428   DOUT << "duration: " << duration ();
429 #endif
430 }
431
432
433
434 IMPLEMENT_IS_TYPE_B1 (Dynamic_req,Musical_req);
435
436 void
437 Dynamic_req::do_print () const
438 {
439   Musical_req::do_print ();
440 }
441
442
443 IMPLEMENT_IS_TYPE_B1 (Absolute_dynamic_req,Musical_req);
444
445 void
446 Absolute_dynamic_req::do_print () const
447 {
448 #ifndef NPRINT
449   Dynamic_req::do_print ();
450   DOUT << " loudness " <<loudness_str ();
451 #endif
452 }
453
454
455 bool
456 Absolute_dynamic_req::do_equal_b (Request *r) const
457 {
458   Absolute_dynamic_req *a = r->musical ()->dynamic ()->absdynamic ();
459   return loudness_ == a->loudness_;
460 }
461
462 String
463 Dynamic_req::loudness_static_str (Loudness l)
464 {
465   switch (l)
466     {
467     case FFF: return "fff";
468     case FF: return "ff";
469     case F: return "f";
470     case MF: return "mf";
471     case MP: return "mp";
472     case P: return "p";
473     case PP: return "pp";
474     case PPP: return "ppp";
475     case FP: return "fp";
476     case SF: return "sf";
477     case SFZ: return "sfz";
478     }
479   return "";
480 }
481
482 String
483 Absolute_dynamic_req::loudness_str () const
484 {
485   String s = loudness_static_str (loudness_);
486   if (s.empty_b ())
487     {
488       s = "mf";
489       warning (String (_ ("Never heard of dynamic scale "))
490                + loudness_ + _ (" assuming mf"));
491     }
492   return s;
493 }
494
495
496 Absolute_dynamic_req::Absolute_dynamic_req ()
497 {
498   loudness_ = MF;
499 }
500
501
502 Span_dynamic_req::Span_dynamic_req ()
503 {
504   dynamic_dir_  = CENTER;
505 }
506
507
508 IMPLEMENT_IS_TYPE_B1 (Span_dynamic_req,Musical_req);
509
510 void
511 Span_dynamic_req::do_print () const
512 {
513 #ifndef NPRINT
514   Span_req::do_print ();
515   DOUT << "softer/louder: " <<dynamic_dir_;
516 #endif
517 }
518
519
520 IMPLEMENT_IS_TYPE_B1 (Tie_req,Musical_req);