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 ();
55 void create_accidental (Accidental_entry *entry, bool, bool);
56 Grob *make_standard_accidental (Stream_event *note, Grob *note_head, Engraver *trans);
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 virtual void initialize ();
70 void process_acknowledged ();
71 virtual void finalize ();
72 virtual void derived_mark () const;
75 SCM last_keysig_; // ugh.
78 Urgh. Since the accidentals depend on lots of variables, we have
79 to store all information before we can really create the
82 vector<Grob*> left_objects_;
83 vector<Grob*> right_objects_;
85 Grob *accidental_placement_;
87 vector<Accidental_entry> accidentals_;
88 vector<Spanner*> ties_;
94 ugh, it is not clear what properties are mutable and which
95 aren't. eg. localKeySignature is changed at runtime, which means
96 that references in grobs should always store ly_deep_copy ()s of
101 Accidental_engraver::Accidental_engraver ()
103 accidental_placement_ = 0;
104 last_keysig_ = SCM_EOL;
108 Accidental_engraver::derived_mark () const
110 scm_gc_mark (last_keysig_);
114 Accidental_engraver::update_local_key_signature ()
116 last_keysig_ = get_property ("keySignature");
117 set_context_property_on_children (context (),
118 ly_symbol2scm ("localKeySignature"),
121 Context *trans = context ()->get_parent_context ();
123 /* Huh. Don't understand what this is good for. --hwn. */
126 while (trans && trans->where_defined (ly_symbol2scm ("localKeySignature"), &val))
128 trans->set_property ("localKeySignature", ly_deep_copy (last_keysig_));
129 trans = trans->get_parent_context ();
134 Accidental_engraver::initialize ()
136 update_local_key_signature ();
139 /** Calculate the number of accidentals on basis of the current local key
140 sig (passed as argument)
142 * First check step+octave (taking into account barnumbers if necessary).
144 * Then check the global signature (only step).
146 Return number of accidentals (0, 1 or 2). */
149 recent_enough (int bar_number, SCM alteration_def, SCM laziness)
151 if (scm_is_number (alteration_def)
152 || laziness == SCM_BOOL_T)
155 return (bar_number <= scm_to_int (scm_cdr (alteration_def)) + scm_to_int (laziness));
159 extract_alteration (SCM alteration_def)
161 if (scm_is_number (alteration_def))
162 return scm_to_int (alteration_def);
163 else if (scm_is_pair (alteration_def))
164 return scm_to_int (scm_car (alteration_def));
165 else if (alteration_def == SCM_BOOL_F)
173 is_tied (SCM alteration_def)
175 return (alteration_def == SCM_BOOL_T)
176 || (scm_is_pair (alteration_def) && scm_car (alteration_def) == SCM_BOOL_T);
180 number_accidentals_from_sig (bool *different, SCM sig, Pitch *pitch,
181 int bar_number, SCM laziness, bool ignore_octave)
183 int n = pitch->get_notename ();
184 int o = pitch->get_octave ();
186 SCM previous_alteration = SCM_BOOL_F;
188 SCM from_same_octave = ly_assoc_get (scm_cons (scm_from_int (o),
189 scm_from_int (n)), sig, SCM_BOOL_F);
190 SCM from_key_signature = ly_assoc_get (scm_from_int (n), sig, SCM_BOOL_F);
191 SCM from_other_octaves = SCM_BOOL_F;
192 for (SCM s = sig; scm_is_pair (s); s = scm_cdr (s))
194 SCM entry = scm_car (s);
195 if (scm_is_pair (scm_car (entry))
196 && scm_cdar (entry) == scm_from_int (n))
197 from_other_octaves = scm_cdr (entry);
200 if (from_same_octave != SCM_BOOL_F
201 && recent_enough (bar_number, from_same_octave, laziness))
202 previous_alteration = from_same_octave;
203 else if (ignore_octave
204 && from_other_octaves != SCM_BOOL_F
205 && recent_enough (bar_number, from_other_octaves, laziness))
206 previous_alteration = from_other_octaves;
207 else if (from_key_signature != SCM_BOOL_F)
208 previous_alteration = from_key_signature;
211 if (is_tied (previous_alteration))
218 int prev = extract_alteration (previous_alteration);
219 int alter = pitch->get_alteration ();
223 else if ((abs (alter) < abs (prev)
224 || prev * alter < 0) && alter != 0)
226 *different = (alter != prev);
232 number_accidentals (bool *different,
233 Pitch *pitch, Context *origin,
234 SCM accidentals, int bar_number)
239 if (scm_is_pair (accidentals) && !scm_is_symbol (scm_car (accidentals)))
240 warning (_f ("accidental typesetting list must begin with context-name: %s",
241 ly_scm2string (scm_car (accidentals)).c_str ()));
243 for (; scm_is_pair (accidentals) && origin;
244 accidentals = scm_cdr (accidentals))
246 // If pair then it is a new accidentals typesetting rule to be checked
247 SCM rule = scm_car (accidentals);
248 if (scm_is_pair (rule))
250 SCM type = scm_car (rule);
251 SCM laziness = scm_cdr (rule);
252 SCM localsig = origin->get_property ("localKeySignature");
255 = scm_is_eq (ly_symbol2scm ("same-octave"), type);
257 = scm_is_eq (ly_symbol2scm ("any-octave"), type);
259 if (same_octave_b || any_octave_b)
262 int n = number_accidentals_from_sig
263 (&d, localsig, pitch, bar_number, laziness, any_octave_b);
264 *different = *different || d;
265 number = max (number, n);
268 warning (_f ("ignoring unknown accidental: %s",
269 ly_symbol2string (type).c_str ()));
272 /* if symbol then it is a context name. Scan parent contexts to
274 else if (scm_is_symbol (rule))
276 Context *dad = origin;
277 while (dad && !dad->is_alias (rule))
278 dad = dad->get_parent_context ();
284 warning (_f ("pair or context-name expected for accidental rule, found %s",
285 ly_scm2string (rule).c_str ()));
292 Accidental_engraver::get_bar_number ()
294 SCM barnum = get_property ("internalBarNumber");
295 SCM smp = get_property ("measurePosition");
297 int bn = robust_scm2int (barnum, 0);
299 Moment mp = robust_scm2moment (smp, Moment (0));
300 if (mp.main_part_ < Rational (0))
307 Accidental_engraver::process_acknowledged ()
309 if (accidentals_.size () && !accidentals_.back ().done_)
311 SCM accidentals = get_property ("autoAccidentals");
312 SCM cautionaries = get_property ("autoCautionaries");
313 int barnum = get_bar_number ();
315 for (vsize i = 0; i < accidentals_.size (); i++)
317 if (accidentals_[i].done_)
319 accidentals_[i].done_ = true;
321 Stream_event *note = accidentals_[i].melodic_;
322 Context *origin = accidentals_[i].origin_;
324 Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
328 bool different = false;
329 bool different_caut = false;
331 int num = number_accidentals (&different,
333 accidentals, barnum);
334 int num_caut = number_accidentals (&different_caut,
336 cautionaries, barnum);
339 bool cautionary = to_boolean (note->get_property ("cautionary"));
343 different = different_caut;
347 bool forced = to_boolean (note->get_property ("force-accidental"));
348 if (num == 0 && forced)
351 /* Cannot look for ties: it's not guaranteed that they reach
352 us before the notes. */
354 && !note->in_event_class ("trill-span-event"))
355 create_accidental (&accidentals_[i], num > 1, 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 Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
373 bool as_suggestion = to_boolean (entry->origin_->get_property ("suggestAccidentals"));
376 a = make_suggested_accidental (note, support, entry->origin_engraver_);
378 a = make_standard_accidental (note, support, entry->origin_engraver_);
380 SCM accs = scm_cons (scm_from_int (pitch->get_alteration ()),
384 if (to_boolean (get_property ("extraNatural")))
385 accs = scm_cons (scm_from_int (0), accs);
388 /* TODO: add cautionary option in accidental. */
390 a->set_property ("cautionary", SCM_BOOL_T);
392 a->set_property ("accidentals", accs);
393 entry->accidental_ = a;
397 Accidental_engraver::make_standard_accidental (Stream_event *note,
404 We construct the accidentals at the originating Voice
405 level, so that we get the property settings for
406 Accidental from the respective Voice.
408 Grob *a = trans->make_item ("Accidental", note_head->self_scm ());
411 We add the accidentals to the support of the arpeggio,
412 so it is put left of the accidentals.
414 for (vsize i = 0; i < left_objects_.size (); i++)
416 if (left_objects_[i]->get_property ("side-axis") == scm_from_int (X_AXIS))
417 Side_position_interface::add_support (left_objects_[i], a);
423 for (vsize i = 0; i < right_objects_.size (); i++)
424 Side_position_interface::add_support (a, right_objects_[i]);
426 a->set_parent (note_head, Y_AXIS);
428 if (!accidental_placement_)
429 accidental_placement_ = make_item ("AccidentalPlacement",
431 Accidental_placement::add_accidental (accidental_placement_, a);
433 note_head->set_object ("accidental-grob", a->self_scm ());
439 Accidental_engraver::make_suggested_accidental (Stream_event *note,
444 Grob *a = trans->make_item ("AccidentalSuggestion", note_head->self_scm ());
446 Side_position_interface::add_support (a, note_head);
447 if (Grob *stem = unsmob_grob (a->get_object ("stem")))
448 Side_position_interface::add_support (a, stem);
450 a->set_parent (note_head, X_AXIS);
455 Accidental_engraver::finalize ()
457 last_keysig_ = SCM_EOL;
461 Accidental_engraver::stop_translation_timestep ()
463 for (vsize j = ties_.size (); j--;)
465 Grob *r = Tie::head (ties_[j], RIGHT);
466 for (vsize i = accidentals_.size (); i--;)
467 if (accidentals_[i].head_ == r)
469 if (Grob *g = accidentals_[i].accidental_)
471 g->set_object ("tie", ties_[j]->self_scm ());
472 accidentals_[i].tied_ = true;
474 ties_.erase (ties_.begin () + j);
479 for (vsize i = accidentals_.size (); i--;)
481 int barnum = get_bar_number ();
483 Stream_event *note = accidentals_[i].melodic_;
484 Context *origin = accidentals_[i].origin_;
486 Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
490 int n = pitch->get_notename ();
491 int o = pitch->get_octave ();
492 int a = pitch->get_alteration ();
493 SCM key = scm_cons (scm_from_int (o), scm_from_int (n));
495 SCM localsig = SCM_EOL;
497 && origin->where_defined (ly_symbol2scm ("localKeySignature"), &localsig))
500 if (accidentals_[i].tied_)
503 Remember an alteration that is different both from
504 that of the tied note and of the key signature.
506 localsig = ly_assoc_front_x
507 (localsig, key, scm_cons (SCM_BOOL_T, scm_from_int (barnum)));
514 not really really correct if there are more than one
515 noteheads with the same notename.
517 localsig = ly_assoc_front_x (localsig, key,
518 scm_cons (scm_from_int (a),
519 scm_from_int (barnum)));
524 origin->set_property ("localKeySignature", localsig);
526 origin = origin->get_parent_context ();
530 accidental_placement_ = 0;
531 accidentals_.clear ();
532 left_objects_.clear ();
533 right_objects_.clear ();
537 Accidental_engraver::acknowledge_rhythmic_head (Grob_info info)
539 Stream_event *note = info.event_cause ();
541 && (note->in_event_class ("note-event")
542 || note->in_event_class ("trill-span-event")))
545 string harmonics usually don't have accidentals.
547 if (to_boolean (get_property ("harmonicAccidentals"))
548 || !ly_is_equal (info.grob ()->get_property ("style"),
549 ly_symbol2scm ("harmonic")))
551 Accidental_entry entry;
552 entry.head_ = info.grob ();
553 entry.origin_engraver_ = dynamic_cast<Engraver *> (info.origin_translator ());
554 entry.origin_ = entry.origin_engraver_->context ();
555 entry.melodic_ = note;
557 accidentals_.push_back (entry);
563 Accidental_engraver::acknowledge_tie (Grob_info info)
565 ties_.push_back (dynamic_cast<Spanner *> (info.grob ()));
569 Accidental_engraver::acknowledge_arpeggio (Grob_info info)
571 left_objects_.push_back (info.grob ());
575 Accidental_engraver::acknowledge_finger (Grob_info info)
577 left_objects_.push_back (info.grob ());
581 Accidental_engraver::process_music ()
583 SCM sig = get_property ("keySignature");
584 /* Detect key sig changes.
585 Update all parents and children. */
586 if (last_keysig_ != sig)
587 update_local_key_signature ();
590 ADD_ACKNOWLEDGER (Accidental_engraver, arpeggio);
591 ADD_ACKNOWLEDGER (Accidental_engraver, finger);
592 ADD_ACKNOWLEDGER (Accidental_engraver, rhythmic_head);
593 ADD_ACKNOWLEDGER (Accidental_engraver, tie);
595 ADD_TRANSLATOR (Accidental_engraver,
598 "Catch note heads, ties and notices key-change events. "
599 "This engraver usually lives at Staff level, but "
600 "reads the settings for Accidental at @code{Voice} level, "
601 "so you can @code{\\override} them at @code{Voice}. ",
602 "Accidental AccidentalSuggestion",
608 "harmonicAccidentals "
610 "localKeySignature");