]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental-engraver.cc
unsmob_pitch -> Pitch::unsmob and related
[lilypond.git] / lily / accidental-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 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 class Accidental_entry
41 {
42 public:
43   bool done_;
44   Stream_event *melodic_;
45   Grob *accidental_;
46   Context *origin_;
47   Engraver *origin_engraver_;
48   Grob *head_;
49   bool tied_;
50
51   Accidental_entry ();
52 };
53
54 Accidental_entry::Accidental_entry ()
55 {
56   tied_ = false;
57   done_ = false;
58   melodic_ = 0;
59   accidental_ = 0;
60   origin_ = 0;
61   origin_engraver_ = 0;
62   head_ = 0;
63 }
64
65 class Accidental_engraver : public Engraver
66 {
67   void update_local_key_signature (SCM new_signature);
68   void create_accidental (Accidental_entry *entry, bool, bool);
69   Grob *make_standard_accidental (Stream_event *note, Grob *note_head, Engraver *trans, bool);
70   Grob *make_suggested_accidental (Stream_event *note, Grob *note_head, Engraver *trans);
71
72 protected:
73   TRANSLATOR_DECLARATIONS (Accidental_engraver);
74   void process_music ();
75
76   void acknowledge_end_tie (Grob_info);
77   void acknowledge_arpeggio (Grob_info);
78   void acknowledge_rhythmic_head (Grob_info);
79   void acknowledge_finger (Grob_info);
80   void acknowledge_note_column (Grob_info);
81
82   void stop_translation_timestep ();
83   void process_acknowledged ();
84
85   virtual void finalize ();
86   virtual void derived_mark () const;
87
88 public:
89   SCM last_keysig_;
90
91   vector<Grob *> left_objects_;
92   vector<Grob *> right_objects_;
93
94   Grob *accidental_placement_;
95
96   vector<Accidental_entry> accidentals_;
97   vector<Spanner *> ties_;
98   vector<Grob *> note_columns_;
99 };
100
101 /*
102   localAlterations is changed at runtime, which means that references
103   in grobs should always store ly_deep_copy ()s of those.
104 */
105
106 Accidental_engraver::Accidental_engraver ()
107 {
108   accidental_placement_ = 0;
109   last_keysig_ = SCM_EOL;
110 }
111
112 void
113 Accidental_engraver::derived_mark () const
114 {
115   scm_gc_mark (last_keysig_);
116 }
117
118 void
119 Accidental_engraver::update_local_key_signature (SCM new_sig)
120 {
121   last_keysig_ = new_sig;
122   set_context_property_on_children (context (),
123                                     ly_symbol2scm ("localAlterations"),
124                                     new_sig);
125
126   Context *trans = context ()->get_parent_context ();
127
128   /*
129     Reset parent contexts so that e.g. piano-accidentals won't remember old
130     cross-staff accidentals after key-sig-changes.
131   */
132
133   SCM val;
134   while (trans && trans->where_defined (ly_symbol2scm ("localAlterations"), &val) == trans)
135     {
136       trans->set_property ("localAlterations", ly_deep_copy (last_keysig_));
137       trans = trans->get_parent_context ();
138     }
139 }
140
141 struct Accidental_result
142 {
143   bool need_acc;
144   bool need_restore;
145
146   Accidental_result ()
147   {
148     need_restore = need_acc = false;
149   }
150
151   Accidental_result (bool restore, bool acc)
152   {
153     need_restore = restore;
154     need_acc = acc;
155   }
156
157   Accidental_result (SCM scm)
158   {
159     need_restore = to_boolean (scm_car (scm));
160     need_acc = to_boolean (scm_cdr (scm));
161   }
162
163   int score () const
164   {
165     return need_acc ? 1 : 0
166            + need_restore ? 1 : 0;
167   }
168 };
169
170 static
171 Accidental_result
172 check_pitch_against_rules (Pitch const &pitch, Context *origin,
173                            SCM rules, int bar_number, SCM measurepos)
174 {
175   Accidental_result result;
176   SCM pitch_scm = pitch.smobbed_copy ();
177   SCM barnum_scm = scm_from_int (bar_number);
178
179   if (scm_is_pair (rules) && !scm_is_symbol (scm_car (rules)))
180     warning (_f ("accidental typesetting list must begin with context-name: %s",
181                  ly_scm2string (scm_car (rules)).c_str ()));
182
183   for (; scm_is_pair (rules) && origin; rules = scm_cdr (rules))
184     {
185       SCM rule = scm_car (rules);
186       if (ly_is_procedure (rule))
187         {
188           SCM rule_result_scm = scm_call_4 (rule, origin->self_scm (),
189                                             pitch_scm, barnum_scm, measurepos);
190           Accidental_result rule_result (rule_result_scm);
191
192           result.need_acc |= rule_result.need_acc;
193           result.need_restore |= rule_result.need_restore;
194         }
195
196       /*
197         If symbol then it is a context name.  Scan parent contexts to
198         find it.
199       */
200       else if (scm_is_symbol (rule))
201         {
202           Context *dad = origin;
203           while (dad && !dad->is_alias (rule))
204             dad = dad->get_parent_context ();
205
206           if (dad)
207             origin = dad;
208         }
209       else
210         warning (_f ("procedure or context-name expected for accidental rule, found %s",
211                      print_scm_val (rule).c_str ()));
212     }
213
214   return result;
215 }
216
217 void
218 Accidental_engraver::process_acknowledged ()
219 {
220   if (accidentals_.size () && !accidentals_.back ().done_)
221     {
222       SCM accidental_rules = get_property ("autoAccidentals");
223       SCM cautionary_rules = get_property ("autoCautionaries");
224       SCM measure_position = get_property ("measurePosition");
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 = Pitch::unsmob (note->get_property ("pitch"));
237           if (!pitch)
238             continue;
239
240           Accidental_result acc = check_pitch_against_rules (*pitch, origin, accidental_rules,
241                                                              barnum, measure_position);
242           Accidental_result caut = check_pitch_against_rules (*pitch, origin, cautionary_rules,
243                                                               barnum, measure_position);
244
245           bool cautionary = to_boolean (note->get_property ("cautionary"));
246           if (caut.score () > acc.score ())
247             {
248               acc.need_acc |= caut.need_acc;
249               acc.need_restore |= caut.need_restore;
250
251               cautionary = true;
252             }
253
254           bool forced = to_boolean (note->get_property ("force-accidental"));
255           if (!acc.need_acc && forced)
256             acc.need_acc = true;
257
258           /*
259             Cannot look for ties: it's not guaranteed that they reach
260             us before the notes.
261           */
262           if (!note->in_event_class ("trill-span-event"))
263             {
264               if (acc.need_acc)
265                 create_accidental (&accidentals_[i], acc.need_restore, cautionary);
266
267               if (forced || cautionary)
268                 accidentals_[i].accidental_->set_property ("forced", SCM_BOOL_T);
269             }
270         }
271     }
272 }
273
274 void
275 Accidental_engraver::create_accidental (Accidental_entry *entry,
276                                         bool restore_natural,
277                                         bool cautionary)
278 {
279   Stream_event *note = entry->melodic_;
280   Grob *support = entry->head_;
281   bool as_suggestion = to_boolean (entry->origin_->get_property ("suggestAccidentals"));
282   Grob *a = 0;
283   if (as_suggestion)
284     a = make_suggested_accidental (note, support, entry->origin_engraver_);
285   else
286     a = make_standard_accidental (note, support, entry->origin_engraver_, cautionary);
287
288   if (restore_natural)
289     {
290       if (to_boolean (get_property ("extraNatural")))
291         a->set_property ("restore-first", SCM_BOOL_T);
292     }
293
294   entry->accidental_ = a;
295 }
296
297 Grob *
298 Accidental_engraver::make_standard_accidental (Stream_event * /* note */,
299                                                Grob *note_head,
300                                                Engraver *trans,
301                                                bool cautionary)
302 {
303   /*
304     We construct the accidentals at the originating Voice
305     level, so that we get the property settings for
306     Accidental from the respective Voice.
307   */
308   Grob *a = 0;
309   if (cautionary)
310     a = trans->make_item ("AccidentalCautionary", note_head->self_scm ());
311   else
312     a = trans->make_item ("Accidental", note_head->self_scm ());
313
314   /*
315     We add the accidentals to the support of the arpeggio,
316     so it is put left of the accidentals.
317   */
318   for (vsize i = 0; i < left_objects_.size (); i++)
319     {
320       if (left_objects_[i]->get_property ("side-axis") == scm_from_int (X_AXIS))
321         Side_position_interface::add_support (left_objects_[i], a);
322     }
323
324   for (vsize i = 0; i < right_objects_.size (); i++)
325     Side_position_interface::add_support (a, right_objects_[i]);
326
327   a->set_parent (note_head, Y_AXIS);
328
329   if (!accidental_placement_)
330     accidental_placement_ = make_item ("AccidentalPlacement",
331                                        a->self_scm ());
332
333   Accidental_placement::add_accidental
334     (accidental_placement_, a,
335      get_property ("accidentalGrouping") == ly_symbol2scm ("voice"),
336      (long) trans);
337
338   note_head->set_object ("accidental-grob", a->self_scm ());
339
340   return a;
341 }
342
343 Grob *
344 Accidental_engraver::make_suggested_accidental (Stream_event * /* note */,
345                                                 Grob *note_head,
346                                                 Engraver *trans)
347 {
348   Grob *a = trans->make_item ("AccidentalSuggestion", note_head->self_scm ());
349
350   Side_position_interface::add_support (a, note_head);
351   if (Grob *stem = Grob::unsmob (a->get_object ("stem")))
352     Side_position_interface::add_support (a, stem);
353
354   a->set_parent (note_head, X_AXIS);
355   return a;
356 }
357
358 void
359 Accidental_engraver::finalize ()
360 {
361   last_keysig_ = SCM_EOL;
362 }
363
364 void
365 Accidental_engraver::stop_translation_timestep ()
366 {
367   for (vsize j = ties_.size (); j--;)
368     {
369       Grob *r = Tie::head (ties_[j], RIGHT);
370       for (vsize i = accidentals_.size (); i--;)
371         if (accidentals_[i].head_ == r)
372           {
373             if (Grob *g = accidentals_[i].accidental_)
374               {
375                 g->set_object ("tie", ties_[j]->self_scm ());
376                 accidentals_[i].tied_ = true;
377               }
378             ties_.erase (ties_.begin () + j);
379             break;
380           }
381     }
382
383   for (vsize i = accidentals_.size (); i--;)
384     {
385       Stream_event *note = accidentals_[i].melodic_;
386       Context *origin = accidentals_[i].origin_;
387
388       int barnum = measure_number (origin);
389
390       Pitch *pitch = Pitch::unsmob (note->get_property ("pitch"));
391       if (!pitch)
392         continue;
393
394       int n = pitch->get_notename ();
395       int o = pitch->get_octave ();
396       Rational a = pitch->get_alteration ();
397       SCM key = scm_cons (scm_from_int (o), scm_from_int (n));
398
399       Moment end_mp = measure_position (context (),
400                                         Duration::unsmob (note->get_property ("duration")));
401       SCM position = scm_cons (scm_from_int (barnum), end_mp.smobbed_copy ());
402
403       SCM localsig = SCM_EOL;
404       while (origin
405              && origin->where_defined (ly_symbol2scm ("localAlterations"), &localsig))
406         {
407           bool change = false;
408           if (accidentals_[i].tied_
409               && !(to_boolean (accidentals_[i].accidental_->get_property ("forced"))))
410             {
411               /*
412                 Remember an alteration that is different both from
413                 that of the tied note and of the key signature.
414               */
415               localsig = ly_assoc_prepend_x (localsig, key, scm_cons (ly_symbol2scm ("tied"),
416                                                                       position));
417               change = true;
418             }
419           else
420             {
421               /*
422                 not really correct if there is more than one
423                 note head with the same notename.
424               */
425               localsig = ly_assoc_prepend_x (localsig, key,
426                                              scm_cons (ly_rational2scm (a),
427                                                        position));
428               change = true;
429             }
430
431           if (change)
432             origin->set_property ("localAlterations", localsig);
433
434           origin = origin->get_parent_context ();
435         }
436     }
437
438   if (accidental_placement_)
439     for (vsize i = 0; i < note_columns_.size (); i++)
440       Separation_item::add_conditional_item (note_columns_[i], accidental_placement_);
441
442   accidental_placement_ = 0;
443   accidentals_.clear ();
444   note_columns_.clear ();
445   left_objects_.clear ();
446   right_objects_.clear ();
447 }
448
449 void
450 Accidental_engraver::acknowledge_rhythmic_head (Grob_info info)
451 {
452   Stream_event *note = info.event_cause ();
453   if (note
454       && (note->in_event_class ("note-event")
455           || note->in_event_class ("trill-span-event")))
456     {
457       /*
458         string harmonics usually don't have accidentals.
459       */
460       if (info.grob ()->get_property ("style") != ly_symbol2scm ("harmonic")
461           || to_boolean (get_property ("harmonicAccidentals")))
462         {
463           Accidental_entry entry;
464           entry.head_ = info.grob ();
465           entry.origin_engraver_ = dynamic_cast<Engraver *> (info.origin_translator ());
466           entry.origin_ = entry.origin_engraver_->context ();
467           entry.melodic_ = note;
468
469           accidentals_.push_back (entry);
470         }
471     }
472 }
473
474 void
475 Accidental_engraver::acknowledge_end_tie (Grob_info info)
476 {
477   ties_.push_back (dynamic_cast<Spanner *> (info.grob ()));
478 }
479
480 void
481 Accidental_engraver::acknowledge_note_column (Grob_info info)
482 {
483   note_columns_.push_back (info.grob ());
484 }
485
486 void
487 Accidental_engraver::acknowledge_arpeggio (Grob_info info)
488 {
489   left_objects_.push_back (info.grob ());
490 }
491
492 void
493 Accidental_engraver::acknowledge_finger (Grob_info info)
494 {
495   left_objects_.push_back (info.grob ());
496 }
497
498 void
499 Accidental_engraver::process_music ()
500 {
501   SCM sig = get_property ("keyAlterations");
502   if (last_keysig_ != sig)
503     update_local_key_signature (sig);
504 }
505
506 ADD_ACKNOWLEDGER (Accidental_engraver, arpeggio);
507 ADD_ACKNOWLEDGER (Accidental_engraver, finger);
508 ADD_ACKNOWLEDGER (Accidental_engraver, rhythmic_head);
509 ADD_END_ACKNOWLEDGER (Accidental_engraver, tie);
510 ADD_ACKNOWLEDGER (Accidental_engraver, note_column);
511
512 ADD_TRANSLATOR (Accidental_engraver,
513                 /* doc */
514                 "Make accidentals."
515                 "  Catch note heads, ties and notices key-change events."
516                 "  This engraver usually lives at Staff level, but"
517                 " reads the settings for Accidental at @code{Voice} level,"
518                 " so you can @code{\\override} them at @code{Voice}.",
519
520                 /* create */
521                 "Accidental "
522                 "AccidentalCautionary "
523                 "AccidentalPlacement "
524                 "AccidentalSuggestion ",
525
526                 /* read */
527                 "autoAccidentals "
528                 "autoCautionaries "
529                 "internalBarNumber "
530                 "extraNatural "
531                 "harmonicAccidentals "
532                 "accidentalGrouping "
533                 "keyAlterations "
534                 "localAlterations ",
535
536                 /* write */
537                 "localAlterations "
538                );