1 /*
2 This file is part of LilyPond, the GNU music typesetter.
3
4 Copyright (C) 1997--2021 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 Modified 2001--2002 by Rune Zedeler <rz@daimi.au.dk>
6
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "accidental-placement.hh"
22 #include "arpeggio.hh"
23 #include "context.hh"
24 #include "duration.hh"
25 #include "engraver.hh"
26 #include "international.hh"
27 #include "item.hh"
28 #include "pitch.hh"
29 #include "protected-scm.hh"
30 #include "rhythmic-head.hh"
31 #include "separation-item.hh"
32 #include "side-position-interface.hh"
33 #include "spanner.hh"
34 #include "stream-event.hh"
35 #include "tie.hh"
36 #include "warn.hh"
37
38 #include "translator.icc"
39
40 #include <cstdint>
41
42 using std::vector;
43
44 class Accidental_entry
45 {
46 public:
47 bool done_;
48 Stream_event *melodic_;
49 Grob *accidental_;
50 Context *origin_;
51 Engraver *origin_engraver_;
52 Grob *head_;
53 bool tied_;
54
55 Accidental_entry ();
56 };
57
Accidental_entry()58 Accidental_entry::Accidental_entry ()
59 {
60 tied_ = false;
61 done_ = false;
62 melodic_ = 0;
63 accidental_ = 0;
64 origin_ = 0;
65 origin_engraver_ = 0;
66 head_ = 0;
67 }
68
69 class Accidental_engraver : public Engraver
70 {
71 void update_local_key_signature (SCM new_signature);
72 void create_accidental (Accidental_entry *entry, bool, bool);
73 Grob *make_standard_accidental (Stream_event *note, Grob *note_head, Engraver *trans, bool);
74 Grob *make_suggested_accidental (Stream_event *note, Grob *note_head, Engraver *trans);
75
76 protected:
77 TRANSLATOR_DECLARATIONS (Accidental_engraver);
78 void process_music ();
79
80 void acknowledge_end_tie (Grob_info_t<Spanner>);
81 void acknowledge_arpeggio (Grob_info);
82 void acknowledge_rhythmic_head (Grob_info);
83 void acknowledge_finger (Grob_info);
84 void acknowledge_note_column (Grob_info_t<Item>);
85
86 void stop_translation_timestep ();
87 void process_acknowledged ();
88
89 void finalize () override;
90 void derived_mark () const override;
91
92 public:
93 SCM last_keysig_;
94
95 vector<Grob *> left_objects_;
96 vector<Grob *> right_objects_;
97
98 Grob *accidental_placement_;
99
100 vector<Accidental_entry> accidentals_;
101 vector<Spanner *> ties_;
102 vector<Item *> note_columns_;
103 };
104
105 /*
106 localAlterations is changed at runtime, which means that references
107 in grobs should always store ly_deep_copy ()s of those.
108 */
109
Accidental_engraver(Context * c)110 Accidental_engraver::Accidental_engraver (Context *c)
111 : Engraver (c)
112 {
113 accidental_placement_ = 0;
114 last_keysig_ = SCM_EOL;
115 }
116
117 void
derived_mark() const118 Accidental_engraver::derived_mark () const
119 {
120 scm_gc_mark (last_keysig_);
121 }
122
123 void
update_local_key_signature(SCM new_sig)124 Accidental_engraver::update_local_key_signature (SCM new_sig)
125 {
126 last_keysig_ = new_sig;
127 set_context_property_on_children (context (),
128 ly_symbol2scm ("localAlterations"),
129 new_sig);
130
131 Context *trans = context ()->get_parent ();
132
133 /*
134 Reset parent contexts so that e.g. piano-accidentals won't remember old
135 cross-staff accidentals after key-sig-changes.
136 */
137
138 while (trans && here_defined (trans, "localAlterations"))
139 {
140 set_property (trans, "localAlterations", ly_deep_copy (last_keysig_));
141 trans = trans->get_parent ();
142 }
143 }
144
145 struct Accidental_result
146 {
147 bool need_acc;
148 bool need_restore;
149
Accidental_resultAccidental_result150 Accidental_result ()
151 {
152 need_restore = need_acc = false;
153 }
154
Accidental_resultAccidental_result155 Accidental_result (bool restore, bool acc)
156 {
157 need_restore = restore;
158 need_acc = acc;
159 }
160
Accidental_resultAccidental_result161 Accidental_result (SCM scm)
162 {
163 need_restore = from_scm<bool> (scm_car (scm));
164 need_acc = from_scm<bool> (scm_cdr (scm));
165 }
166
scoreAccidental_result167 int score () const
168 {
169 return (need_acc ? 1 : 0)
170 + (need_restore ? 1 : 0);
171 }
172 };
173
174 static
175 Accidental_result
check_pitch_against_rules(Pitch const & pitch,Context * origin,SCM rules,int bar_number)176 check_pitch_against_rules (Pitch const &pitch, Context *origin,
177 SCM rules, int bar_number)
178 {
179 Accidental_result result;
180 SCM pitch_scm = pitch.smobbed_copy ();
181 SCM barnum_scm = to_scm (bar_number);
182
183 if (scm_is_pair (rules) && !scm_is_symbol (scm_car (rules)))
184 warning (_f ("accidental typesetting list must begin with context-name: %s",
185 ly_scm2string (scm_car (rules)).c_str ()));
186
187 for (; scm_is_pair (rules) && origin; rules = scm_cdr (rules))
188 {
189 SCM rule = scm_car (rules);
190 if (ly_is_procedure (rule))
191 {
192 SCM rule_result_scm = scm_call_3 (rule, origin->self_scm (),
193 pitch_scm, barnum_scm);
194 Accidental_result rule_result (rule_result_scm);
195
196 result.need_acc |= rule_result.need_acc;
197 result.need_restore |= rule_result.need_restore;
198 }
199
200 /*
201 If symbol then it is a context name. Scan parent contexts to
202 find it.
203 */
204 else if (scm_is_symbol (rule))
205 {
206 Context *dad = find_context_above (origin, rule);
207 if (dad)
208 origin = dad;
209 }
210 else
211 warning (_f ("procedure or context-name expected for accidental rule, found %s",
212 print_scm_val (rule).c_str ()));
213 }
214
215 return result;
216 }
217
218 void
process_acknowledged()219 Accidental_engraver::process_acknowledged ()
220 {
221 if (accidentals_.size () && !accidentals_.back ().done_)
222 {
223 SCM accidental_rules = get_property (this, "autoAccidentals");
224 SCM cautionary_rules = get_property (this, "autoCautionaries");
225 int barnum = measure_number (context ());
226
227 for (vsize i = 0; i < accidentals_.size (); i++)
228 {
229 if (accidentals_[i].done_)
230 continue;
231 accidentals_[i].done_ = true;
232
233 Stream_event *note = accidentals_[i].melodic_;
234 Context *origin = accidentals_[i].origin_;
235
236 Pitch *pitch = unsmob<Pitch> (get_property (note, "pitch"));
237 if (!pitch)
238 continue;
239
240 Accidental_result acc = check_pitch_against_rules (*pitch, origin,
241 accidental_rules,
242 barnum);
243 Accidental_result caut = check_pitch_against_rules (*pitch, origin,
244 cautionary_rules,
245 barnum);
246
247 bool cautionary = from_scm<bool> (get_property (note, "cautionary"));
248 if (caut.score () > acc.score ())
249 {
250 acc.need_acc |= caut.need_acc;
251 acc.need_restore |= caut.need_restore;
252
253 cautionary = true;
254 }
255
256 bool forced = from_scm<bool> (get_property (note, "force-accidental"));
257 if (!acc.need_acc && forced)
258 acc.need_acc = true;
259
260 /*
261 Cannot look for ties: it's not guaranteed that they reach
262 us before the notes.
263 */
264 if (!note->in_event_class ("trill-span-event"))
265 {
266 if (acc.need_acc)
267 create_accidental (&accidentals_[i], acc.need_restore, cautionary);
268
269 if (forced || cautionary)
270 set_property (accidentals_[i].accidental_, "forced", SCM_BOOL_T);
271 }
272 }
273 }
274 }
275
276 void
create_accidental(Accidental_entry * entry,bool restore_natural,bool cautionary)277 Accidental_engraver::create_accidental (Accidental_entry *entry,
278 bool restore_natural,
279 bool cautionary)
280 {
281 Stream_event *note = entry->melodic_;
282 Grob *support = entry->head_;
283 SCM suggest = get_property (entry->origin_, "suggestAccidentals");
284 bool bsuggest = from_scm<bool> (suggest);
285 Grob *a = 0;
286 if (bsuggest
287 || (cautionary && scm_is_eq (suggest, ly_symbol2scm ("cautionary"))))
288 a = make_suggested_accidental (note, support, entry->origin_engraver_);
289 else
290 a = make_standard_accidental (note, support, entry->origin_engraver_, cautionary);
291
292 if (restore_natural)
293 {
294 if (from_scm<bool> (get_property (this, "extraNatural")))
295 set_property (a, "restore-first", SCM_BOOL_T);
296 }
297
298 entry->accidental_ = a;
299 }
300
301 Grob *
make_standard_accidental(Stream_event *,Grob * note_head,Engraver * trans,bool cautionary)302 Accidental_engraver::make_standard_accidental (Stream_event * /* note */,
303 Grob *note_head,
304 Engraver *trans,
305 bool cautionary)
306 {
307 /*
308 We construct the accidentals at the originating Voice
309 level, so that we get the property settings for
310 Accidental from the respective Voice.
311 */
312 Grob *a = 0;
313 if (cautionary)
314 a = trans->make_item ("AccidentalCautionary", note_head->self_scm ());
315 else
316 a = trans->make_item ("Accidental", note_head->self_scm ());
317
318 /*
319 We add the accidentals to the support of the arpeggio,
320 so it is put left of the accidentals.
321 */
322 for (vsize i = 0; i < left_objects_.size (); i++)
323 {
324 if (ly_is_equal (get_property (left_objects_[i], "side-axis"), to_scm (X_AXIS)))
325 Side_position_interface::add_support (left_objects_[i], a);
326 }
327
328 for (vsize i = 0; i < right_objects_.size (); i++)
329 Side_position_interface::add_support (a, right_objects_[i]);
330
331 a->set_y_parent (note_head);
332
333 if (!accidental_placement_)
334 accidental_placement_ = make_item ("AccidentalPlacement",
335 a->self_scm ());
336
337 Accidental_placement::add_accidental
338 (accidental_placement_, a,
339 scm_is_eq (get_property (this, "accidentalGrouping"), ly_symbol2scm ("voice")),
340 trans);
341
342 set_object (note_head, "accidental-grob", a->self_scm ());
343
344 return a;
345 }
346
347 Grob *
make_suggested_accidental(Stream_event *,Grob * note_head,Engraver * trans)348 Accidental_engraver::make_suggested_accidental (Stream_event * /* note */,
349 Grob *note_head,
350 Engraver *trans)
351 {
352 Grob *a = trans->make_item ("AccidentalSuggestion", note_head->self_scm ());
353
354 Side_position_interface::add_support (a, note_head);
355 if (Grob *stem = unsmob<Grob> (get_object (a, "stem")))
356 Side_position_interface::add_support (a, stem);
357
358 a->set_x_parent (note_head);
359 return a;
360 }
361
362 void
finalize()363 Accidental_engraver::finalize ()
364 {
365 last_keysig_ = SCM_EOL;
366 }
367
368 void
stop_translation_timestep()369 Accidental_engraver::stop_translation_timestep ()
370 {
371 for (vsize j = ties_.size (); j--;)
372 {
373 Grob *r = Tie::head (ties_[j], RIGHT);
374 Grob *l = Tie::head (ties_[j], LEFT);
375 if (l && r)
376 {
377 // Don't mark accidentals as "tied" when the pitch is not
378 // actually the same. This is relevant for enharmonic ties.
379 Stream_event *le = l->event_cause ();
380 Stream_event *re = r->event_cause ();
381 if (le && re
382 && !ly_is_equal (get_property (le, "pitch"), get_property (re, "pitch")))
383 continue;
384 }
385
386 for (vsize i = accidentals_.size (); i--;)
387 if (accidentals_[i].head_ == r)
388 {
389 if (Grob *g = accidentals_[i].accidental_)
390 {
391 set_object (g, "tie", ties_[j]->self_scm ());
392 accidentals_[i].tied_ = true;
393 }
394 ties_.erase (ties_.begin () + j);
395 break;
396 }
397 }
398
399 for (vsize i = accidentals_.size (); i--;)
400 {
401 Stream_event *note = accidentals_[i].melodic_;
402 Context *origin = accidentals_[i].origin_;
403
404 int barnum = measure_number (origin);
405
406 Pitch *pitch = unsmob<Pitch> (get_property (note, "pitch"));
407 if (!pitch)
408 continue;
409
410 int n = pitch->get_notename ();
411 int o = pitch->get_octave ();
412 Rational a = pitch->get_alteration ();
413 SCM key = scm_cons (to_scm (o), to_scm (n));
414
415 Duration *dur = unsmob<Duration> (get_property (note, "duration"));
416 Moment end_mom = note_end_mom (context (), dur);
417 SCM position = scm_cons (to_scm (barnum), end_mom.smobbed_copy ());
418
419 SCM localsig = SCM_EOL;
420 while (origin && where_defined (origin, "localAlterations", &localsig))
421 {
422 bool change = false;
423 if (accidentals_[i].tied_
424 && !(from_scm<bool> (get_property (accidentals_[i].accidental_, "forced"))))
425 {
426 /*
427 Remember an alteration that is different both from
428 that of the tied note and of the key signature.
429 */
430 localsig = ly_assoc_prepend_x (localsig, key, scm_cons (ly_symbol2scm ("tied"),
431 position));
432 change = true;
433 }
434 else
435 {
436 /*
437 not really correct if there is more than one
438 note head with the same notename.
439 */
440 localsig = ly_assoc_prepend_x (localsig, key,
441 scm_cons (to_scm (a),
442 position));
443 change = true;
444 }
445
446 if (change)
447 set_property (origin, "localAlterations", localsig);
448
449 origin = origin->get_parent ();
450 }
451 }
452
453 if (accidental_placement_)
454 for (vsize i = 0; i < note_columns_.size (); i++)
455 Separation_item::add_conditional_item (note_columns_[i], accidental_placement_);
456
457 accidental_placement_ = 0;
458 accidentals_.clear ();
459 note_columns_.clear ();
460 left_objects_.clear ();
461 right_objects_.clear ();
462 }
463
464 void
acknowledge_rhythmic_head(Grob_info info)465 Accidental_engraver::acknowledge_rhythmic_head (Grob_info info)
466 {
467 Stream_event *note = info.event_cause ();
468 if (note
469 && (note->in_event_class ("note-event")
470 || note->in_event_class ("trill-span-event"))
471 // option to skip accidentals on string harmonics
472 && (from_scm<bool> (get_property (this, "harmonicAccidentals"))
473 || !scm_is_eq (get_property (info.grob (), "style"),
474 ly_symbol2scm ("harmonic")))
475 // ignore accidentals in non-printing voices like NullVoice
476 && !from_scm<bool> (get_property (info.origin_engraver ()->context (),
477 "nullAccidentals")))
478 {
479 Accidental_entry entry;
480 entry.head_ = info.grob ();
481 entry.origin_engraver_ = info.origin_engraver ();
482 entry.origin_ = entry.origin_engraver_->context ();
483 entry.melodic_ = note;
484
485 accidentals_.push_back (entry);
486 }
487 }
488
489 void
acknowledge_end_tie(Grob_info_t<Spanner> info)490 Accidental_engraver::acknowledge_end_tie (Grob_info_t<Spanner> info)
491 {
492 ties_.push_back (info.grob ());
493 }
494
495 void
acknowledge_note_column(Grob_info_t<Item> info)496 Accidental_engraver::acknowledge_note_column (Grob_info_t<Item> info)
497 {
498 note_columns_.push_back (info.grob ());
499 }
500
501 void
acknowledge_arpeggio(Grob_info info)502 Accidental_engraver::acknowledge_arpeggio (Grob_info info)
503 {
504 left_objects_.push_back (info.grob ());
505 }
506
507 void
acknowledge_finger(Grob_info info)508 Accidental_engraver::acknowledge_finger (Grob_info info)
509 {
510 left_objects_.push_back (info.grob ());
511 }
512
513 void
process_music()514 Accidental_engraver::process_music ()
515 {
516 SCM sig = get_property (this, "keyAlterations");
517 if (!scm_is_eq (last_keysig_, sig))
518 update_local_key_signature (sig);
519 }
520
521 void
boot()522 Accidental_engraver::boot ()
523 {
524 ADD_ACKNOWLEDGER (Accidental_engraver, arpeggio);
525 ADD_ACKNOWLEDGER (Accidental_engraver, finger);
526 ADD_ACKNOWLEDGER (Accidental_engraver, rhythmic_head);
527 ADD_END_ACKNOWLEDGER (Accidental_engraver, tie);
528 ADD_ACKNOWLEDGER (Accidental_engraver, note_column);
529 }
530
531 ADD_TRANSLATOR (Accidental_engraver,
532 /* doc */
533 "Make accidentals."
534 " Catch note heads, ties and notices key-change events."
535 " This engraver usually lives at Staff level, but"
536 " reads the settings for Accidental at @code{Voice} level,"
537 " so you can @code{\\override} them at @code{Voice}.",
538
539 /* create */
540 "Accidental "
541 "AccidentalCautionary "
542 "AccidentalPlacement "
543 "AccidentalSuggestion ",
544
545 /* read */
546 "autoAccidentals "
547 "autoCautionaries "
548 "internalBarNumber "
549 "extraNatural "
550 "harmonicAccidentals "
551 "accidentalGrouping "
552 "keyAlterations "
553 "localAlterations ",
554
555 /* write */
556 "localAlterations "
557 );
558