2 accidental-engraver.cc -- implement accidental_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Modified 2001--2002 by Rune Zedeler <rz@daimi.au.dk>
10 #include "accidental-placement.hh"
11 #include "arpeggio.hh"
15 #include "engraver.hh"
16 #include "international.hh"
18 #include "protected-scm.hh"
19 #include "rhythmic-head.hh"
20 #include "side-position-interface.hh"
21 #include "stream-event.hh"
25 #include "translator.icc"
27 class Accidental_entry
31 Stream_event *melodic_;
34 Engraver *origin_engraver_;
41 Accidental_entry::Accidental_entry ()
51 class Accidental_engraver : public Engraver
53 int get_bar_number ();
54 void update_local_key_signature (SCM new_signature);
55 void create_accidental (Accidental_entry *entry, bool, bool);
56 Grob *make_standard_accidental (Stream_event *note, Grob *note_head, Engraver *trans, bool);
57 Grob *make_suggested_accidental (Stream_event *note, Grob *note_head, Engraver *trans);
60 TRANSLATOR_DECLARATIONS (Accidental_engraver);
61 void process_music ();
63 void acknowledge_tie (Grob_info);
64 void acknowledge_arpeggio (Grob_info);
65 void acknowledge_rhythmic_head (Grob_info);
66 void acknowledge_finger (Grob_info);
68 void stop_translation_timestep ();
69 void process_acknowledged ();
71 virtual void finalize ();
72 virtual void derived_mark () const;
77 vector<Grob*> left_objects_;
78 vector<Grob*> right_objects_;
80 Grob *accidental_placement_;
82 vector<Accidental_entry> accidentals_;
83 vector<Spanner*> ties_;
87 localKeySignature is changed at runtime, which means that references
88 in grobs should always store ly_deep_copy ()s of those.
92 Accidental_engraver::Accidental_engraver ()
94 accidental_placement_ = 0;
95 last_keysig_ = SCM_EOL;
99 Accidental_engraver::derived_mark () const
101 scm_gc_mark (last_keysig_);
105 Accidental_engraver::update_local_key_signature (SCM new_sig)
107 last_keysig_ = new_sig;
108 set_context_property_on_children (context (),
109 ly_symbol2scm ("localKeySignature"),
112 Context *trans = context ()->get_parent_context ();
114 /* Reset parent contexts so that e.g. piano-accidentals won't remember old
115 cross-staff accidentals after key-sig-changes */
118 while (trans && trans->where_defined (ly_symbol2scm ("localKeySignature"), &val)==trans)
120 trans->set_property ("localKeySignature", ly_deep_copy (last_keysig_));
121 trans = trans->get_parent_context ();
126 /** Calculate the number of accidentals on basis of the current local key
127 sig (passed as argument)
129 * First check step+octave (taking into account barnumbers if necessary).
131 * Then check the global signature (only step).
133 Return number of accidentals (0, 1 or 2). */
136 recent_enough (int bar_number, SCM alteration_def, SCM laziness)
138 if (scm_is_number (alteration_def)
139 || laziness == SCM_BOOL_T)
142 return (bar_number <= scm_to_int (scm_cdr (alteration_def)) + scm_to_int (laziness));
146 extract_alteration (SCM alteration_def)
148 if (scm_is_number (alteration_def))
149 return ly_scm2rational (alteration_def);
150 else if (scm_is_pair (alteration_def))
151 return ly_scm2rational (scm_car (alteration_def));
152 else if (alteration_def == SCM_BOOL_F)
160 is_tied (SCM alteration_def)
162 SCM tied = ly_symbol2scm ("tied");
163 return (alteration_def == tied
164 || (scm_is_pair (alteration_def) && scm_car (alteration_def) == tied));
167 struct Accidental_result
172 Accidental_result () {
173 need_restore = need_acc = false;
177 return need_acc ? 1 : 0
178 + need_restore ? 1 : 0;
183 check_pitch_against_signature (SCM key_signature, Pitch const &pitch,
184 int bar_number, SCM laziness, bool ignore_octave)
186 Accidental_result result;
187 int n = pitch.get_notename ();
188 int o = pitch.get_octave ();
190 SCM previous_alteration = SCM_BOOL_F;
192 SCM from_same_octave = ly_assoc_get (scm_cons (scm_from_int (o),
193 scm_from_int (n)), key_signature, SCM_BOOL_F);
194 SCM from_key_signature = ly_assoc_get (scm_from_int (n), key_signature, SCM_BOOL_F);
195 SCM from_other_octaves = SCM_BOOL_F;
196 for (SCM s = key_signature; scm_is_pair (s); s = scm_cdr (s))
198 SCM entry = scm_car (s);
199 if (scm_is_pair (scm_car (entry))
200 && scm_cdar (entry) == scm_from_int (n))
202 from_other_octaves = scm_cdr (entry);
208 && from_same_octave != SCM_BOOL_F
209 && recent_enough (bar_number, from_same_octave, laziness))
210 previous_alteration = from_same_octave;
211 else if (ignore_octave
212 && from_other_octaves != SCM_BOOL_F
213 && recent_enough (bar_number, from_other_octaves, laziness))
214 previous_alteration = from_other_octaves;
215 else if (from_key_signature != SCM_BOOL_F)
216 previous_alteration = from_key_signature;
218 if (is_tied (previous_alteration))
220 result.need_acc = true;
224 Rational prev = extract_alteration (previous_alteration);
225 Rational alter = pitch.get_alteration ();
229 result.need_acc = true;
231 && (alter.abs () < prev.abs ()
232 || (prev * alter).sign () < 0))
233 result.need_restore = true;
242 check_pitch_against_rules (Pitch const &pitch, Context *origin,
243 SCM rules, int bar_number)
245 Accidental_result result;
246 if (scm_is_pair (rules) && !scm_is_symbol (scm_car (rules)))
247 warning (_f ("accidental typesetting list must begin with context-name: %s",
248 ly_scm2string (scm_car (rules)).c_str ()));
250 for (; scm_is_pair (rules) && origin;
251 rules = scm_cdr (rules))
253 SCM rule = scm_car (rules);
254 if (scm_is_pair (rule))
256 SCM type = scm_car (rule);
257 SCM laziness = scm_cdr (rule);
258 SCM localsig = origin->get_property ("localKeySignature");
261 = (ly_symbol2scm ("same-octave") == type);
263 = (ly_symbol2scm ("any-octave") == type);
265 if (same_octave || any_octave)
267 Accidental_result rule_result = check_pitch_against_signature
268 (localsig, pitch, bar_number, laziness, any_octave);
270 result.need_acc |= rule_result.need_acc;
271 result.need_restore |= rule_result.need_restore;
274 warning (_f ("ignoring unknown accidental rule: %s",
275 ly_symbol2string (type).c_str ()));
278 /* if symbol then it is a context name. Scan parent contexts to
280 else if (scm_is_symbol (rule))
282 Context *dad = origin;
283 while (dad && !dad->is_alias (rule))
284 dad = dad->get_parent_context ();
290 warning (_f ("pair or context-name expected for accidental rule, found %s",
291 ly_scm2string (rule).c_str ()));
298 Accidental_engraver::get_bar_number ()
300 SCM barnum = get_property ("internalBarNumber");
301 SCM smp = get_property ("measurePosition");
303 int bn = robust_scm2int (barnum, 0);
305 Moment mp = robust_scm2moment (smp, Moment (0));
306 if (mp.main_part_ < Rational (0))
313 Accidental_engraver::process_acknowledged ()
315 if (accidentals_.size () && !accidentals_.back ().done_)
317 SCM accidental_rules = get_property ("autoAccidentals");
318 SCM cautionary_rules = get_property ("autoCautionaries");
319 int barnum = get_bar_number ();
321 for (vsize i = 0; i < accidentals_.size (); i++)
323 if (accidentals_[i].done_)
325 accidentals_[i].done_ = true;
327 Stream_event *note = accidentals_[i].melodic_;
328 Context *origin = accidentals_[i].origin_;
330 Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
334 Accidental_result acc = check_pitch_against_rules (*pitch, origin,
335 accidental_rules, barnum);
336 Accidental_result caut = check_pitch_against_rules (*pitch, origin,
337 cautionary_rules, barnum);
339 bool cautionary = to_boolean (note->get_property ("cautionary"));
340 if (caut.score () > acc.score ())
342 acc.need_acc |= caut.need_acc;
343 acc.need_restore |= caut.need_restore;
348 bool forced = to_boolean (note->get_property ("force-accidental"));
349 if (!acc.need_acc && forced)
352 /* Cannot look for ties: it's not guaranteed that they reach
353 us before the notes. */
355 && !note->in_event_class ("trill-span-event"))
356 create_accidental (&accidentals_[i], acc.need_restore, cautionary);
358 if (forced || cautionary)
359 accidentals_[i].accidental_->set_property ("forced", SCM_BOOL_T);
365 Accidental_engraver::create_accidental (Accidental_entry *entry,
366 bool restore_natural,
369 Stream_event *note = entry->melodic_;
370 Grob *support = entry->head_;
371 bool as_suggestion = to_boolean (entry->origin_->get_property ("suggestAccidentals"));
374 a = make_suggested_accidental (note, support, entry->origin_engraver_);
376 a = make_standard_accidental (note, support, entry->origin_engraver_, cautionary);
380 if (to_boolean (get_property ("extraNatural")))
381 a->set_property ("restore-first", SCM_BOOL_T);
384 entry->accidental_ = a;
388 Accidental_engraver::make_standard_accidental (Stream_event *note,
396 We construct the accidentals at the originating Voice
397 level, so that we get the property settings for
398 Accidental from the respective Voice.
402 a = trans->make_item ("AccidentalCautionary", note_head->self_scm ());
404 a = trans->make_item ("Accidental", note_head->self_scm ());
407 We add the accidentals to the support of the arpeggio,
408 so it is put left of the accidentals.
410 for (vsize i = 0; i < left_objects_.size (); i++)
412 if (left_objects_[i]->get_property ("side-axis") == scm_from_int (X_AXIS))
413 Side_position_interface::add_support (left_objects_[i], a);
416 for (vsize i = 0; i < right_objects_.size (); i++)
417 Side_position_interface::add_support (a, right_objects_[i]);
419 a->set_parent (note_head, Y_AXIS);
421 if (!accidental_placement_)
422 accidental_placement_ = make_item ("AccidentalPlacement",
424 Accidental_placement::add_accidental (accidental_placement_, a);
426 note_head->set_object ("accidental-grob", a->self_scm ());
432 Accidental_engraver::make_suggested_accidental (Stream_event *note,
438 Grob *a = trans->make_item ("AccidentalSuggestion", note_head->self_scm ());
440 Side_position_interface::add_support (a, note_head);
441 if (Grob *stem = unsmob_grob (a->get_object ("stem")))
442 Side_position_interface::add_support (a, stem);
444 a->set_parent (note_head, X_AXIS);
449 Accidental_engraver::finalize ()
451 last_keysig_ = SCM_EOL;
455 Accidental_engraver::stop_translation_timestep ()
457 for (vsize j = ties_.size (); j--;)
459 Grob *r = Tie::head (ties_[j], RIGHT);
460 for (vsize i = accidentals_.size (); i--;)
461 if (accidentals_[i].head_ == r)
463 if (Grob *g = accidentals_[i].accidental_)
465 g->set_object ("tie", ties_[j]->self_scm ());
466 accidentals_[i].tied_ = true;
468 ties_.erase (ties_.begin () + j);
473 for (vsize i = accidentals_.size (); i--;)
475 int barnum = get_bar_number ();
477 Stream_event *note = accidentals_[i].melodic_;
478 Context *origin = accidentals_[i].origin_;
480 Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
484 int n = pitch->get_notename ();
485 int o = pitch->get_octave ();
486 Rational a = pitch->get_alteration ();
487 SCM key = scm_cons (scm_from_int (o), scm_from_int (n));
489 SCM localsig = SCM_EOL;
491 && origin->where_defined (ly_symbol2scm ("localKeySignature"), &localsig))
494 if (accidentals_[i].tied_)
497 Remember an alteration that is different both from
498 that of the tied note and of the key signature.
500 localsig = ly_assoc_prepend_x (localsig, key, scm_cons (ly_symbol2scm ("tied"),
501 scm_from_int (barnum)));
508 not really really correct if there are more than one
509 noteheads with the same notename.
511 localsig = ly_assoc_prepend_x (localsig, key,
512 scm_cons (ly_rational2scm (a),
513 scm_from_int (barnum)));
518 origin->set_property ("localKeySignature", localsig);
520 origin = origin->get_parent_context ();
524 accidental_placement_ = 0;
525 accidentals_.clear ();
526 left_objects_.clear ();
527 right_objects_.clear ();
531 Accidental_engraver::acknowledge_rhythmic_head (Grob_info info)
533 Stream_event *note = info.event_cause ();
535 && (note->in_event_class ("note-event")
536 || note->in_event_class ("trill-span-event")))
539 string harmonics usually don't have accidentals.
541 if (info.grob ()->get_property ("style") != ly_symbol2scm ("harmonic")
542 || to_boolean (get_property ("harmonicAccidentals")))
544 Accidental_entry entry;
545 entry.head_ = info.grob ();
546 entry.origin_engraver_ = dynamic_cast<Engraver *> (info.origin_translator ());
547 entry.origin_ = entry.origin_engraver_->context ();
548 entry.melodic_ = note;
550 accidentals_.push_back (entry);
556 Accidental_engraver::acknowledge_tie (Grob_info info)
558 ties_.push_back (dynamic_cast<Spanner *> (info.grob ()));
562 Accidental_engraver::acknowledge_arpeggio (Grob_info info)
564 left_objects_.push_back (info.grob ());
568 Accidental_engraver::acknowledge_finger (Grob_info info)
570 left_objects_.push_back (info.grob ());
574 Accidental_engraver::process_music ()
576 SCM sig = get_property ("keySignature");
577 if (last_keysig_ != sig)
578 update_local_key_signature (sig);
581 ADD_ACKNOWLEDGER (Accidental_engraver, arpeggio);
582 ADD_ACKNOWLEDGER (Accidental_engraver, finger);
583 ADD_ACKNOWLEDGER (Accidental_engraver, rhythmic_head);
584 ADD_ACKNOWLEDGER (Accidental_engraver, tie);
586 ADD_TRANSLATOR (Accidental_engraver,
589 "Catch note heads, ties and notices key-change events. "
590 "This engraver usually lives at Staff level, but "
591 "reads the settings for Accidental at @code{Voice} level, "
592 "so you can @code{\\override} them at @code{Voice}. ",
596 "AccidentalCautionary "
597 "AccidentalSuggestion ",
604 "harmonicAccidentals "
605 "localKeySignature ",