]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental-engraver.cc
Merge commit 'origin' into includes
[lilypond.git] / lily / accidental-engraver.cc
1 /*
2   accidental-engraver.cc -- implement Accidental_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   Modified 2001--2002 by Rune Zedeler <rz@daimi.au.dk>
8 */
9
10 #include "accidental-placement.hh"
11 #include "arpeggio.hh"
12 #include "context.hh"
13 #include "duration.hh"
14 #include "engraver.hh"
15 #include "international.hh"
16 #include "item.hh"
17 #include "pitch.hh"
18 #include "protected-scm.hh"
19 #include "rhythmic-head.hh"
20 #include "separation-item.hh"
21 #include "side-position-interface.hh"
22 #include "spanner.hh"
23 #include "stream-event.hh"
24 #include "tie.hh"
25 #include "warn.hh"
26
27 #include "translator.icc"
28
29 class Accidental_entry
30 {
31 public:
32   bool done_;
33   Stream_event *melodic_;
34   Grob *accidental_;
35   Context *origin_;
36   Engraver *origin_engraver_;
37   Grob *head_;
38   bool tied_;
39
40   Accidental_entry ();
41 };
42
43 Accidental_entry::Accidental_entry ()
44 {
45   tied_ = false;
46   done_ = false;
47   melodic_ = 0;
48   accidental_ = 0;
49   origin_ = 0;
50   head_ = 0;
51 }
52
53 class Accidental_engraver : public Engraver
54 {
55   void update_local_key_signature (SCM new_signature);
56   void create_accidental (Accidental_entry *entry, bool, bool);
57   Grob *make_standard_accidental (Stream_event *note, Grob *note_head, Engraver *trans, bool);
58   Grob *make_suggested_accidental (Stream_event *note, Grob *note_head, Engraver *trans);
59
60 protected:
61   TRANSLATOR_DECLARATIONS (Accidental_engraver);
62   void process_music ();
63
64   void acknowledge_tie (Grob_info);
65   void acknowledge_arpeggio (Grob_info);
66   void acknowledge_rhythmic_head (Grob_info);
67   void acknowledge_finger (Grob_info);
68   void acknowledge_note_column (Grob_info);
69
70   void stop_translation_timestep ();
71   void process_acknowledged ();
72   
73   virtual void finalize ();
74   virtual void derived_mark () const;
75
76 public:
77   SCM last_keysig_;
78
79   vector<Grob*> left_objects_;
80   vector<Grob*> right_objects_;
81
82   Grob *accidental_placement_;
83
84   vector<Accidental_entry> accidentals_;
85   vector<Spanner*> ties_;
86   vector<Grob*> note_columns_;
87 };
88
89 /*
90   localKeySignature is changed at runtime, which means that references
91   in grobs should always store ly_deep_copy ()s of those.
92 */
93
94
95 Accidental_engraver::Accidental_engraver ()
96 {
97   accidental_placement_ = 0;
98   last_keysig_ = SCM_EOL;
99 }
100
101 void
102 Accidental_engraver::derived_mark () const
103 {
104   scm_gc_mark (last_keysig_);
105 }
106
107 void
108 Accidental_engraver::update_local_key_signature (SCM new_sig)
109 {
110   last_keysig_ = new_sig;
111   set_context_property_on_children (context (),
112                                     ly_symbol2scm ("localKeySignature"),
113                                     new_sig);
114
115   Context *trans = context ()->get_parent_context ();
116
117   /* Reset parent contexts so that e.g. piano-accidentals won't remember old
118      cross-staff accidentals after key-sig-changes */
119
120   SCM val;
121   while (trans && trans->where_defined (ly_symbol2scm ("localKeySignature"), &val)==trans)
122     {
123       trans->set_property ("localKeySignature", ly_deep_copy (last_keysig_));
124       trans = trans->get_parent_context ();
125     }
126 }
127
128
129 /** Calculate the number of accidentals on basis of the current local key
130     sig (passed as argument)
131
132     * First check step+octave (taking into account barnumbers if necessary).
133
134     * Then check the global signature (only step).
135
136     Return number of accidentals (0, 1 or 2).  */
137
138 static bool
139 recent_enough (int bar_number, SCM alteration_def, SCM laziness)
140 {
141   if (scm_is_number (alteration_def)
142       || laziness == SCM_BOOL_T)
143     return true;
144
145   return (bar_number <= scm_to_int (scm_cadr (alteration_def)) + scm_to_int (laziness));
146 }
147
148 static Rational
149 extract_alteration (SCM alteration_def)
150 {
151   if (scm_is_number (alteration_def))
152     return ly_scm2rational (alteration_def);
153   else if (scm_is_pair (alteration_def))
154     return ly_scm2rational (scm_car (alteration_def));
155   else if (alteration_def == SCM_BOOL_F)
156     return Rational (0);
157   else
158     assert (0);
159   return Rational (0);
160 }
161
162 bool
163 is_tied (SCM alteration_def)
164 {
165   SCM tied = ly_symbol2scm ("tied");
166   return (alteration_def == tied
167           || (scm_is_pair (alteration_def) && scm_car (alteration_def) == tied));
168 }
169
170 struct Accidental_result
171 {
172   bool need_acc;
173   bool need_restore;
174
175   Accidental_result ()
176   {
177     need_restore = need_acc = false;
178   }
179
180   Accidental_result (bool restore, bool acc)
181   {
182     need_restore = restore;
183     need_acc = acc;
184   }
185
186   Accidental_result (SCM scm)
187   {
188     need_restore = to_boolean (scm_car (scm));
189     need_acc = to_boolean (scm_cdr (scm));
190   }
191
192   int score () const
193   {
194     return need_acc ? 1 : 0
195       + need_restore ? 1 : 0;
196   }
197 };
198
199 Accidental_result
200 check_pitch_against_signature (SCM key_signature, Pitch const &pitch,
201                                int bar_number, SCM laziness, bool ignore_octave)
202 {
203   Accidental_result result;
204   int n = pitch.get_notename ();
205   int o = pitch.get_octave ();
206
207   SCM previous_alteration = SCM_BOOL_F;
208
209   SCM from_same_octave = ly_assoc_get (scm_cons (scm_from_int (o),
210                                                  scm_from_int (n)), key_signature, SCM_BOOL_F);
211   SCM from_key_signature = ly_assoc_get (scm_from_int (n), key_signature, SCM_BOOL_F);
212   SCM from_other_octaves = SCM_BOOL_F;
213   for (SCM s = key_signature; scm_is_pair (s); s = scm_cdr (s))
214     {
215       SCM entry = scm_car (s);
216       if (scm_is_pair (scm_car (entry))
217           && scm_cdar (entry) == scm_from_int (n))
218         {
219           from_other_octaves = scm_cdr (entry);
220           break;
221         }
222     }
223
224   if (!ignore_octave
225       && from_same_octave != SCM_BOOL_F
226       && recent_enough (bar_number, from_same_octave, laziness))
227     previous_alteration = from_same_octave;
228   else if (ignore_octave
229            && from_other_octaves != SCM_BOOL_F
230            && recent_enough (bar_number, from_other_octaves, laziness))
231     previous_alteration = from_other_octaves;
232   else if (from_key_signature != SCM_BOOL_F)
233     previous_alteration = from_key_signature;
234
235   if (is_tied (previous_alteration))
236     {
237       result.need_acc = true;
238     }
239   else
240     {
241       Rational prev = extract_alteration (previous_alteration);
242       Rational alter = pitch.get_alteration ();
243
244       if (alter != prev)
245         {
246           result.need_acc = true;
247           if (alter.sign ()
248               && (alter.abs () < prev.abs ()
249                   || (prev * alter).sign () < 0))
250             result.need_restore = true;
251         }
252     }
253
254   return result;
255 }
256
257 // TODO: consider moving check_pitch_against_signature to SCM (in which case
258 // we can delete this function).
259 LY_DEFINE (ly_find_accidentals_simple, "ly:find-accidentals-simple", 5, 0, 0,
260            (SCM keysig, SCM pitch_scm, SCM barnum, SCM laziness, SCM octaveness ),
261            "Checks the need for an accidental and a @q{restore} accidental against a"
262            " key signature.  The @var{laziness} is the number of bars for which reminder"
263            " accidentals are used (ie. if @var{laziness} is zero, we only cancel accidentals"
264            " in the same bar; if @var{laziness} is three, we cancel accidentals up to three"
265            " bars after they first appear.  @var{octaveness} is either"
266            " @code{'same-octave} or @code{'any-octave} and it specifies whether"
267            " accidentals should be canceled in different octaves.")
268 {
269   LY_ASSERT_TYPE (unsmob_pitch, pitch_scm, 2);
270   LY_ASSERT_TYPE (scm_is_integer, barnum, 3);
271   LY_ASSERT_TYPE (ly_is_symbol, octaveness, 5);
272
273   bool symbol_ok = octaveness == ly_symbol2scm ("any-octave") ||
274     octaveness == ly_symbol2scm ("same-octave");
275
276   SCM_ASSERT_TYPE (symbol_ok, octaveness, SCM_ARG5, __FUNCTION__, "'any-octave or 'same-octave");
277
278   Pitch *pitch = unsmob_pitch (pitch_scm);
279
280   int bar_number = scm_to_int (barnum);
281   bool ignore_octave = ly_symbol2scm ("any-octave") == octaveness; 
282   Accidental_result result = check_pitch_against_signature (keysig, *pitch, bar_number,
283                                                             laziness, ignore_octave);
284
285   return scm_cons (scm_from_bool (result.need_restore), scm_from_bool (result.need_acc));
286 }
287
288 static
289 Accidental_result
290 check_pitch_against_rules (Pitch const &pitch, Context *origin,
291                            SCM rules, int bar_number, SCM measurepos)
292 {
293   Accidental_result result;
294   SCM pitch_scm = pitch.smobbed_copy ();
295   SCM barnum_scm = scm_from_int (bar_number);
296
297   if (scm_is_pair (rules) && !scm_is_symbol (scm_car (rules)))
298     warning (_f ("accidental typesetting list must begin with context-name: %s",
299                  ly_scm2string (scm_car (rules)).c_str ()));
300
301   for (; scm_is_pair (rules) && origin;
302        rules = scm_cdr (rules))
303     {
304       SCM rule = scm_car (rules);
305       if (ly_is_procedure (rule))
306         {
307           SCM rule_result_scm = scm_call_4 (rule, origin->self_scm (),
308                                             pitch_scm, barnum_scm, measurepos);
309
310           Accidental_result rule_result (rule_result_scm);
311
312           result.need_acc |= rule_result.need_acc;
313           result.need_restore |= rule_result.need_restore;
314         }
315
316       /* if symbol then it is a context name.  Scan parent contexts to
317          find it. */
318       else if (scm_is_symbol (rule))
319         {
320           Context *dad = origin;
321           while (dad && !dad->is_alias (rule))
322             dad = dad->get_parent_context ();
323
324           if (dad)
325             origin = dad;
326         }
327       else
328         warning (_f ("procedure or context-name expected for accidental rule, found %s",
329                      print_scm_val (rule).c_str ()));
330     }
331
332   return result;
333 }
334
335 void
336 Accidental_engraver::process_acknowledged ()
337 {
338   if (accidentals_.size () && !accidentals_.back ().done_)
339     {
340       SCM accidental_rules = get_property ("autoAccidentals");
341       SCM cautionary_rules = get_property ("autoCautionaries");
342       SCM measure_position = get_property ("measurePosition");
343       int barnum = measure_number (context());
344
345       for (vsize i = 0; i < accidentals_.size (); i++)
346         {
347           if (accidentals_[i].done_)
348             continue;
349           accidentals_[i].done_ = true;
350
351           Stream_event *note = accidentals_[i].melodic_;
352           Context *origin = accidentals_[i].origin_;
353
354           Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
355           if (!pitch)
356             continue;
357
358           Accidental_result acc = check_pitch_against_rules (*pitch, origin, accidental_rules,
359                                                              barnum, measure_position);
360           Accidental_result caut = check_pitch_against_rules (*pitch, origin, cautionary_rules,
361                                                               barnum, measure_position);
362
363           bool cautionary = to_boolean (note->get_property ("cautionary"));
364           if (caut.score () > acc.score ())
365             {
366               acc.need_acc |= caut.need_acc; 
367               acc.need_restore |= caut.need_restore; 
368
369               cautionary = true;
370             }
371
372           bool forced = to_boolean (note->get_property ("force-accidental"));
373           if (!acc.need_acc && forced)
374             acc.need_acc = true;
375
376           /* Cannot look for ties: it's not guaranteed that they reach
377              us before the notes. */
378           if (!note->in_event_class ("trill-span-event"))
379             {
380               if (acc.need_acc)       
381                 create_accidental (&accidentals_[i], acc.need_restore, cautionary);
382
383               if (forced || cautionary)
384                 accidentals_[i].accidental_->set_property ("forced", SCM_BOOL_T);
385             }
386         }
387     }
388 }
389
390 void
391 Accidental_engraver::create_accidental (Accidental_entry *entry,
392                                         bool restore_natural,
393                                         bool cautionary)
394 {
395   Stream_event *note = entry->melodic_;
396   Grob *support = entry->head_;
397   bool as_suggestion = to_boolean (entry->origin_->get_property ("suggestAccidentals"));
398   Grob *a = 0;
399   if (as_suggestion)
400     a = make_suggested_accidental (note, support, entry->origin_engraver_);
401   else
402     a = make_standard_accidental (note, support, entry->origin_engraver_, cautionary);
403
404   if (restore_natural)
405     {
406       if (to_boolean (get_property ("extraNatural")))
407         a->set_property ("restore-first", SCM_BOOL_T);
408     }
409
410   entry->accidental_ = a;
411 }
412
413 Grob *
414 Accidental_engraver::make_standard_accidental (Stream_event * /* note */,
415                                                Grob *note_head,
416                                                Engraver *trans,
417                                                bool cautionary)
418 {
419   /*
420     We construct the accidentals at the originating Voice
421     level, so that we get the property settings for
422     Accidental from the respective Voice.
423   */
424   Grob *a = 0;
425   if (cautionary)
426     a = trans->make_item ("AccidentalCautionary", note_head->self_scm ());
427   else
428     a = trans->make_item ("Accidental", note_head->self_scm ());
429
430   /*
431     We add the accidentals to the support of the arpeggio,
432     so it is put left of the accidentals.
433   */
434   for (vsize i = 0; i < left_objects_.size (); i++)
435     {
436       if (left_objects_[i]->get_property ("side-axis") == scm_from_int (X_AXIS))
437         Side_position_interface::add_support (left_objects_[i], a);
438     }
439
440   for (vsize i = 0; i < right_objects_.size (); i++)
441     Side_position_interface::add_support (a, right_objects_[i]);
442
443   a->set_parent (note_head, Y_AXIS);
444
445   if (!accidental_placement_)
446     accidental_placement_ = make_item ("AccidentalPlacement",
447                                        a->self_scm ());
448   Accidental_placement::add_accidental (accidental_placement_, a);
449
450   note_head->set_object ("accidental-grob", a->self_scm ());
451   
452   return a;
453 }
454
455 Grob *
456 Accidental_engraver::make_suggested_accidental (Stream_event * /* note */,
457                                                 Grob *note_head,
458                                                 Engraver *trans)
459 {
460   Grob *a = trans->make_item ("AccidentalSuggestion", note_head->self_scm ());
461
462   Side_position_interface::add_support (a, note_head);
463   if (Grob *stem = unsmob_grob (a->get_object ("stem")))
464     Side_position_interface::add_support (a, stem);
465
466   a->set_parent (note_head, X_AXIS);
467   return a;
468 }
469
470 void
471 Accidental_engraver::finalize ()
472 {
473   last_keysig_ = SCM_EOL;
474 }
475
476 void
477 Accidental_engraver::stop_translation_timestep ()
478 {
479   for (vsize j = ties_.size (); j--;)
480     {
481       Grob *r = Tie::head (ties_[j], RIGHT);
482       for (vsize i = accidentals_.size (); i--;)
483         if (accidentals_[i].head_ == r)
484           {
485             if (Grob *g = accidentals_[i].accidental_)
486               {
487                 g->set_object ("tie", ties_[j]->self_scm ());
488                 accidentals_[i].tied_ = true;
489               }
490             ties_.erase (ties_.begin () + j);
491             break;
492           }
493     }
494
495   for (vsize i = accidentals_.size (); i--;)
496     {      
497       Stream_event *note = accidentals_[i].melodic_;
498       Context *origin = accidentals_[i].origin_;
499
500       int barnum = measure_number (origin);
501
502       Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
503       if (!pitch)
504         continue;
505
506       int n = pitch->get_notename ();
507       int o = pitch->get_octave ();
508       Rational a = pitch->get_alteration ();
509       SCM key = scm_cons (scm_from_int (o), scm_from_int (n));
510
511       Moment end_mp = measure_position (context (),
512                                         unsmob_duration (note->get_property ("duration")));
513       SCM position = scm_cons (scm_from_int (barnum), end_mp.smobbed_copy ());
514
515       SCM localsig = SCM_EOL;
516       while (origin
517              && origin->where_defined (ly_symbol2scm ("localKeySignature"), &localsig))
518         {
519           bool change = false;
520           if (accidentals_[i].tied_
521               && !(to_boolean (accidentals_[i].accidental_->get_property ("forced"))))
522             {
523               /*
524                 Remember an alteration that is different both from
525                 that of the tied note and of the key signature.
526               */
527               localsig = ly_assoc_prepend_x (localsig, key,scm_cons (ly_symbol2scm ("tied"),
528                                                                      position));
529
530               change = true;
531             }
532           else
533             {
534               /*
535                 not really really correct if there are more than one
536                 noteheads with the same notename.
537               */
538               localsig = ly_assoc_prepend_x (localsig, key,
539                                              scm_cons (ly_rational2scm (a),
540                                                        position));
541               change = true;
542             }
543
544           if (change)
545             origin->set_property ("localKeySignature", localsig);
546
547           origin = origin->get_parent_context ();
548         }
549     }
550
551   if (accidental_placement_)
552     for (vsize i = 0; i < note_columns_.size (); i++)
553       Separation_item::add_conditional_item (note_columns_[i], accidental_placement_);
554
555   accidental_placement_ = 0;
556   accidentals_.clear ();
557   left_objects_.clear ();
558   right_objects_.clear ();
559 }
560
561 void
562 Accidental_engraver::acknowledge_rhythmic_head (Grob_info info)
563 {
564   Stream_event *note = info.event_cause ();
565   if (note
566       && (note->in_event_class ("note-event")
567           || note->in_event_class ("trill-span-event")))
568     {
569       /*
570         string harmonics usually don't have accidentals.
571       */
572       if (info.grob ()->get_property ("style") != ly_symbol2scm ("harmonic")
573           || to_boolean (get_property ("harmonicAccidentals")))
574         {
575           Accidental_entry entry;
576           entry.head_ = info.grob ();
577           entry.origin_engraver_ = dynamic_cast<Engraver *> (info.origin_translator ());
578           entry.origin_ = entry.origin_engraver_->context ();
579           entry.melodic_ = note;
580
581           accidentals_.push_back (entry);
582         }
583     }
584 }
585
586 void
587 Accidental_engraver::acknowledge_tie (Grob_info info)
588 {
589   ties_.push_back (dynamic_cast<Spanner *> (info.grob ()));
590 }
591
592 void
593 Accidental_engraver::acknowledge_note_column (Grob_info info)
594 {
595   note_columns_.push_back (info.grob ());
596 }
597
598 void
599 Accidental_engraver::acknowledge_arpeggio (Grob_info info)
600 {
601   left_objects_.push_back (info.grob ());
602 }
603
604 void
605 Accidental_engraver::acknowledge_finger (Grob_info info)
606 {
607   left_objects_.push_back (info.grob ());
608 }
609
610 void
611 Accidental_engraver::process_music ()
612 {
613   SCM sig = get_property ("keySignature");
614   if (last_keysig_ != sig)
615     update_local_key_signature (sig);
616 }
617
618 ADD_ACKNOWLEDGER (Accidental_engraver, arpeggio);
619 ADD_ACKNOWLEDGER (Accidental_engraver, finger);
620 ADD_ACKNOWLEDGER (Accidental_engraver, rhythmic_head);
621 ADD_ACKNOWLEDGER (Accidental_engraver, tie);
622 ADD_ACKNOWLEDGER (Accidental_engraver, note_column);
623
624 ADD_TRANSLATOR (Accidental_engraver,
625                 /* doc */
626                 "Make accidentals."
627                 "  Catch note heads, ties and notices key-change events."
628                 "  This engraver usually lives at Staff level, but"
629                 " reads the settings for Accidental at @code{Voice} level,"
630                 " so you can @code{\\override} them at @code{Voice}.",
631
632                 /* create */
633                 "Accidental "
634                 "AccidentalCautionary "
635                 "AccidentalPlacement "
636                 "AccidentalSuggestion ",
637
638                 /* read */
639                 "autoAccidentals "
640                 "autoCautionaries "
641                 "internalBarNumber "
642                 "extraNatural "
643                 "harmonicAccidentals "
644                 "keySignature "
645                 "localKeySignature ",
646
647                 /* write */
648                 "localKeySignature "
649                 );