2 accidental-engraver.cc -- implement accidental_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 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 "separation-item.hh"
21 #include "side-position-interface.hh"
22 #include "stream-event.hh"
26 #include "translator.icc"
28 class Accidental_entry
32 Stream_event *melodic_;
35 Engraver *origin_engraver_;
42 Accidental_entry::Accidental_entry ()
52 class Accidental_engraver : public Engraver
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);
67 void acknowledge_note_column (Grob_info);
69 void stop_translation_timestep ();
70 void process_acknowledged ();
72 virtual void finalize ();
73 virtual void derived_mark () const;
78 vector<Grob*> left_objects_;
79 vector<Grob*> right_objects_;
81 Grob *accidental_placement_;
83 vector<Accidental_entry> accidentals_;
84 vector<Spanner*> ties_;
85 vector<Grob*> note_columns_;
89 localKeySignature is changed at runtime, which means that references
90 in grobs should always store ly_deep_copy ()s of those.
94 Accidental_engraver::Accidental_engraver ()
96 accidental_placement_ = 0;
97 last_keysig_ = SCM_EOL;
101 Accidental_engraver::derived_mark () const
103 scm_gc_mark (last_keysig_);
107 Accidental_engraver::update_local_key_signature (SCM new_sig)
109 last_keysig_ = new_sig;
110 set_context_property_on_children (context (),
111 ly_symbol2scm ("localKeySignature"),
114 Context *trans = context ()->get_parent_context ();
116 /* Reset parent contexts so that e.g. piano-accidentals won't remember old
117 cross-staff accidentals after key-sig-changes */
120 while (trans && trans->where_defined (ly_symbol2scm ("localKeySignature"), &val)==trans)
122 trans->set_property ("localKeySignature", ly_deep_copy (last_keysig_));
123 trans = trans->get_parent_context ();
128 /** Calculate the number of accidentals on basis of the current local key
129 sig (passed as argument)
131 * First check step+octave (taking into account barnumbers if necessary).
133 * Then check the global signature (only step).
135 Return number of accidentals (0, 1 or 2). */
138 recent_enough (int bar_number, SCM alteration_def, SCM laziness)
140 if (scm_is_number (alteration_def)
141 || laziness == SCM_BOOL_T)
144 return (bar_number <= scm_to_int (scm_cdr (alteration_def)) + scm_to_int (laziness));
148 extract_alteration (SCM alteration_def)
150 if (scm_is_number (alteration_def))
151 return ly_scm2rational (alteration_def);
152 else if (scm_is_pair (alteration_def))
153 return ly_scm2rational (scm_car (alteration_def));
154 else if (alteration_def == SCM_BOOL_F)
162 is_tied (SCM alteration_def)
164 SCM tied = ly_symbol2scm ("tied");
165 return (alteration_def == tied
166 || (scm_is_pair (alteration_def) && scm_car (alteration_def) == tied));
169 struct Accidental_result
174 Accidental_result () {
175 need_restore = need_acc = false;
179 return need_acc ? 1 : 0
180 + need_restore ? 1 : 0;
185 check_pitch_against_signature (SCM key_signature, Pitch const &pitch,
186 int bar_number, SCM laziness, bool ignore_octave)
188 Accidental_result result;
189 int n = pitch.get_notename ();
190 int o = pitch.get_octave ();
192 SCM previous_alteration = SCM_BOOL_F;
194 SCM from_same_octave = ly_assoc_get (scm_cons (scm_from_int (o),
195 scm_from_int (n)), key_signature, SCM_BOOL_F);
196 SCM from_key_signature = ly_assoc_get (scm_from_int (n), key_signature, SCM_BOOL_F);
197 SCM from_other_octaves = SCM_BOOL_F;
198 for (SCM s = key_signature; scm_is_pair (s); s = scm_cdr (s))
200 SCM entry = scm_car (s);
201 if (scm_is_pair (scm_car (entry))
202 && scm_cdar (entry) == scm_from_int (n))
204 from_other_octaves = scm_cdr (entry);
210 && from_same_octave != SCM_BOOL_F
211 && recent_enough (bar_number, from_same_octave, laziness))
212 previous_alteration = from_same_octave;
213 else if (ignore_octave
214 && from_other_octaves != SCM_BOOL_F
215 && recent_enough (bar_number, from_other_octaves, laziness))
216 previous_alteration = from_other_octaves;
217 else if (from_key_signature != SCM_BOOL_F)
218 previous_alteration = from_key_signature;
220 if (is_tied (previous_alteration))
222 result.need_acc = true;
226 Rational prev = extract_alteration (previous_alteration);
227 Rational alter = pitch.get_alteration ();
231 result.need_acc = true;
233 && (alter.abs () < prev.abs ()
234 || (prev * alter).sign () < 0))
235 result.need_restore = true;
244 check_pitch_against_rules (Pitch const &pitch, Context *origin,
245 SCM rules, int bar_number)
247 Accidental_result result;
248 if (scm_is_pair (rules) && !scm_is_symbol (scm_car (rules)))
249 warning (_f ("accidental typesetting list must begin with context-name: %s",
250 ly_scm2string (scm_car (rules)).c_str ()));
252 for (; scm_is_pair (rules) && origin;
253 rules = scm_cdr (rules))
255 SCM rule = scm_car (rules);
256 if (scm_is_pair (rule))
258 SCM type = scm_car (rule);
259 SCM laziness = scm_cdr (rule);
260 SCM localsig = origin->get_property ("localKeySignature");
263 = (ly_symbol2scm ("same-octave") == type);
265 = (ly_symbol2scm ("any-octave") == type);
267 if (same_octave || any_octave)
269 Accidental_result rule_result = check_pitch_against_signature
270 (localsig, pitch, bar_number, laziness, any_octave);
272 result.need_acc |= rule_result.need_acc;
273 result.need_restore |= rule_result.need_restore;
276 warning (_f ("ignoring unknown accidental rule: %s",
277 ly_symbol2string (type).c_str ()));
280 /* if symbol then it is a context name. Scan parent contexts to
282 else if (scm_is_symbol (rule))
284 Context *dad = origin;
285 while (dad && !dad->is_alias (rule))
286 dad = dad->get_parent_context ();
292 warning (_f ("pair or context-name expected for accidental rule, found %s",
293 ly_scm2string (rule).c_str ()));
300 Accidental_engraver::process_acknowledged ()
302 if (accidentals_.size () && !accidentals_.back ().done_)
304 SCM accidental_rules = get_property ("autoAccidentals");
305 SCM cautionary_rules = get_property ("autoCautionaries");
306 int barnum = measure_number (context());
308 for (vsize i = 0; i < accidentals_.size (); i++)
310 if (accidentals_[i].done_)
312 accidentals_[i].done_ = true;
314 Stream_event *note = accidentals_[i].melodic_;
315 Context *origin = accidentals_[i].origin_;
317 Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
321 Accidental_result acc = check_pitch_against_rules (*pitch, origin,
322 accidental_rules, barnum);
323 Accidental_result caut = check_pitch_against_rules (*pitch, origin,
324 cautionary_rules, barnum);
326 bool cautionary = to_boolean (note->get_property ("cautionary"));
327 if (caut.score () > acc.score ())
329 acc.need_acc |= caut.need_acc;
330 acc.need_restore |= caut.need_restore;
335 bool forced = to_boolean (note->get_property ("force-accidental"));
336 if (!acc.need_acc && forced)
339 /* Cannot look for ties: it's not guaranteed that they reach
340 us before the notes. */
341 if (!note->in_event_class ("trill-span-event"))
344 create_accidental (&accidentals_[i], acc.need_restore, cautionary);
346 if (forced || cautionary)
347 accidentals_[i].accidental_->set_property ("forced", SCM_BOOL_T);
354 Accidental_engraver::create_accidental (Accidental_entry *entry,
355 bool restore_natural,
358 Stream_event *note = entry->melodic_;
359 Grob *support = entry->head_;
360 bool as_suggestion = to_boolean (entry->origin_->get_property ("suggestAccidentals"));
363 a = make_suggested_accidental (note, support, entry->origin_engraver_);
365 a = make_standard_accidental (note, support, entry->origin_engraver_, cautionary);
369 if (to_boolean (get_property ("extraNatural")))
370 a->set_property ("restore-first", SCM_BOOL_T);
373 entry->accidental_ = a;
377 Accidental_engraver::make_standard_accidental (Stream_event *note,
385 We construct the accidentals at the originating Voice
386 level, so that we get the property settings for
387 Accidental from the respective Voice.
391 a = trans->make_item ("AccidentalCautionary", note_head->self_scm ());
393 a = trans->make_item ("Accidental", note_head->self_scm ());
396 We add the accidentals to the support of the arpeggio,
397 so it is put left of the accidentals.
399 for (vsize i = 0; i < left_objects_.size (); i++)
401 if (left_objects_[i]->get_property ("side-axis") == scm_from_int (X_AXIS))
402 Side_position_interface::add_support (left_objects_[i], a);
405 for (vsize i = 0; i < right_objects_.size (); i++)
406 Side_position_interface::add_support (a, right_objects_[i]);
408 a->set_parent (note_head, Y_AXIS);
410 if (!accidental_placement_)
411 accidental_placement_ = make_item ("AccidentalPlacement",
413 Accidental_placement::add_accidental (accidental_placement_, a);
415 note_head->set_object ("accidental-grob", a->self_scm ());
421 Accidental_engraver::make_suggested_accidental (Stream_event *note,
427 Grob *a = trans->make_item ("AccidentalSuggestion", note_head->self_scm ());
429 Side_position_interface::add_support (a, note_head);
430 if (Grob *stem = unsmob_grob (a->get_object ("stem")))
431 Side_position_interface::add_support (a, stem);
433 a->set_parent (note_head, X_AXIS);
438 Accidental_engraver::finalize ()
440 last_keysig_ = SCM_EOL;
444 Accidental_engraver::stop_translation_timestep ()
446 for (vsize j = ties_.size (); j--;)
448 Grob *r = Tie::head (ties_[j], RIGHT);
449 for (vsize i = accidentals_.size (); i--;)
450 if (accidentals_[i].head_ == r)
452 if (Grob *g = accidentals_[i].accidental_)
454 g->set_object ("tie", ties_[j]->self_scm ());
455 accidentals_[i].tied_ = true;
457 ties_.erase (ties_.begin () + j);
462 for (vsize i = accidentals_.size (); i--;)
464 Stream_event *note = accidentals_[i].melodic_;
465 Context *origin = accidentals_[i].origin_;
467 int barnum = measure_number (origin);
469 Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
473 int n = pitch->get_notename ();
474 int o = pitch->get_octave ();
475 Rational a = pitch->get_alteration ();
476 SCM key = scm_cons (scm_from_int (o), scm_from_int (n));
478 SCM localsig = SCM_EOL;
480 && origin->where_defined (ly_symbol2scm ("localKeySignature"), &localsig))
483 if (accidentals_[i].tied_
484 && !(to_boolean (accidentals_[i].accidental_->get_property ("forced"))))
487 Remember an alteration that is different both from
488 that of the tied note and of the key signature.
490 localsig = ly_assoc_prepend_x (localsig, key, scm_cons (ly_symbol2scm ("tied"),
491 scm_from_int (barnum)));
498 not really really correct if there are more than one
499 noteheads with the same notename.
501 localsig = ly_assoc_prepend_x (localsig, key,
502 scm_cons (ly_rational2scm (a),
503 scm_from_int (barnum)));
508 origin->set_property ("localKeySignature", localsig);
510 origin = origin->get_parent_context ();
514 if (accidental_placement_)
515 for (vsize i = 0; i < note_columns_.size (); i++)
516 Separation_item::add_conditional_item (note_columns_[i], accidental_placement_);
518 accidental_placement_ = 0;
519 accidentals_.clear ();
520 left_objects_.clear ();
521 right_objects_.clear ();
525 Accidental_engraver::acknowledge_rhythmic_head (Grob_info info)
527 Stream_event *note = info.event_cause ();
529 && (note->in_event_class ("note-event")
530 || note->in_event_class ("trill-span-event")))
533 string harmonics usually don't have accidentals.
535 if (info.grob ()->get_property ("style") != ly_symbol2scm ("harmonic")
536 || to_boolean (get_property ("harmonicAccidentals")))
538 Accidental_entry entry;
539 entry.head_ = info.grob ();
540 entry.origin_engraver_ = dynamic_cast<Engraver *> (info.origin_translator ());
541 entry.origin_ = entry.origin_engraver_->context ();
542 entry.melodic_ = note;
544 accidentals_.push_back (entry);
550 Accidental_engraver::acknowledge_tie (Grob_info info)
552 ties_.push_back (dynamic_cast<Spanner *> (info.grob ()));
556 Accidental_engraver::acknowledge_note_column (Grob_info info)
558 note_columns_.push_back (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);
585 ADD_ACKNOWLEDGER (Accidental_engraver, note_column);
587 ADD_TRANSLATOR (Accidental_engraver,
590 " Catch note heads, ties and notices key-change events."
591 " This engraver usually lives at Staff level, but"
592 " reads the settings for Accidental at @code{Voice} level,"
593 " so you can @code{\\override} them at @code{Voice}.",
597 "AccidentalCautionary "
598 "AccidentalSuggestion ",
605 "harmonicAccidentals "
606 "localKeySignature ",