]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental-engraver.cc
* lily/include/translator.icc (ADD_ACKNOWLEDGER): new macro.
[lilypond.git] / lily / accidental-engraver.cc
1 /*
2   accidental-engraver.cc -- implement accidental_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   Modified 2001--2002 by Rune Zedeler <rz@daimi.au.dk>
8 */
9
10 #include "accidental-placement.hh"
11 #include "arpeggio.hh"
12 #include "context.hh"
13 #include "engraver.hh"
14 #include "protected-scm.hh"
15 #include "rhythmic-head.hh"
16 #include "side-position-interface.hh"
17 #include "tie.hh"
18 #include "warn.hh"
19 #include "music.hh"
20 #include "pitch.hh"
21
22 #include "translator.icc"
23
24 class Accidental_entry
25 {
26 public:
27   bool done_;
28   Music *melodic_;
29   Grob *accidental_;
30   Context *origin_;
31   Engraver *origin_engraver_;
32   Grob *head_;
33   bool tied_;
34
35   Accidental_entry ();
36 };
37
38 Accidental_entry::Accidental_entry ()
39 {
40   tied_ = false;
41   done_ = false;
42   melodic_ = 0;
43   accidental_ = 0;
44   origin_ = 0;
45   head_ = 0;
46 }
47
48 class Accidental_engraver : public Engraver
49 {
50   int get_bar_number ();
51   void update_local_key_signature ();
52   void create_accidental (Accidental_entry *entry, bool, bool);
53   Grob *make_standard_accidental (Music *note, Grob *note_head, Engraver *trans);
54   Grob *make_suggested_accidental (Music *note, Grob *note_head, Engraver *trans);
55   
56 protected:
57   TRANSLATOR_DECLARATIONS (Accidental_engraver);
58   PRECOMPUTED_VIRTUAL void process_music ();
59
60   void acknowledge_tie (Grob_info);
61   void acknowledge_arpeggio (Grob_info);
62   void acknowledge_rhythmic_head (Grob_info);
63   void acknowledge_finger (Grob_info);
64   
65   PRECOMPUTED_VIRTUAL void stop_translation_timestep ();
66   virtual void initialize ();
67   PRECOMPUTED_VIRTUAL void process_acknowledged ();
68   virtual void finalize ();
69   virtual void derived_mark () const;
70
71 public:
72   SCM last_keysig_;     // ugh.
73
74   /*
75     Urgh. Since the accidentals depend on lots of variables, we have
76     to store all information before we can really create the
77     accidentals.
78   */
79   Link_array<Grob> left_objects_;
80   Link_array<Grob> right_objects_;
81
82   Grob *accidental_placement_;
83
84   Array<Accidental_entry> accidentals_;
85   Link_array<Spanner> ties_;
86
87
88 };
89
90 /*
91   TODO:
92
93   ugh, it is not clear what properties are mutable and which
94   aren't. eg. localKeySignature is changed at runtime, which means
95   that references in grobs should always store ly_deep_copy ()s of
96   those.
97 */
98
99 static void
100 set_property_on_children (Context *trans, char const *sym, SCM val)
101 {
102   trans->set_property (sym, ly_deep_copy (val));
103   for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
104     {
105       Context *trg = unsmob_context (scm_car (p));
106       set_property_on_children (trg, sym, ly_deep_copy (val));
107     }
108 }
109
110 Accidental_engraver::Accidental_engraver ()
111 {
112   accidental_placement_ = 0;
113   last_keysig_ = SCM_EOL;
114 }
115
116 void
117 Accidental_engraver::derived_mark () const
118 {
119   scm_gc_mark (last_keysig_);
120 }
121
122 void
123 Accidental_engraver::update_local_key_signature ()
124 {
125   last_keysig_ = get_property ("keySignature");
126   set_property_on_children (context (), "localKeySignature", last_keysig_);
127
128   Context *trans = context ()->get_parent_context ();
129
130   /* Huh. Don't understand what this is good for. --hwn.  */
131   while (trans && trans->where_defined (ly_symbol2scm ("localKeySignature")))
132     {
133       trans->set_property ("localKeySignature", ly_deep_copy (last_keysig_));
134       trans = trans->get_parent_context ();
135     }
136 }
137
138 void
139 Accidental_engraver::initialize ()
140 {
141   update_local_key_signature ();
142 }
143
144 /** Calculate the number of accidentals on basis of the current local key
145     sig (passed as argument)
146
147     * First check step+octave (taking into account barnumbers if necessary).
148
149     * Then check the global signature (only step).
150
151     Return number of accidentals (0, 1 or 2).  */
152
153 static bool
154 recent_enough (int bar_number, SCM alteration_def, SCM laziness)
155 {
156   if (scm_is_number (alteration_def)
157       || laziness == SCM_BOOL_T)
158     return true;
159
160   return (bar_number <= scm_to_int (scm_cdr (alteration_def)) + scm_to_int (laziness));
161 }
162
163 static int
164 extract_alteration (SCM alteration_def)
165 {
166   if (scm_is_number (alteration_def))
167     return scm_to_int (alteration_def);
168   else if (scm_is_pair (alteration_def))
169     return scm_to_int (scm_car (alteration_def));
170   else if (alteration_def == SCM_BOOL_F)
171     return 0;
172   else
173     assert (0);
174   return 0;
175 }
176
177 bool
178 is_tied (SCM alteration_def)
179 {
180   return (alteration_def == SCM_BOOL_T)
181     || (scm_is_pair (alteration_def) && scm_car (alteration_def) == SCM_BOOL_T);
182 }
183
184 static int
185 number_accidentals_from_sig (bool *different, SCM sig, Pitch *pitch,
186                              int bar_number, SCM laziness, bool ignore_octave)
187 {
188   int n = pitch->get_notename ();
189   int o = pitch->get_octave ();
190
191   SCM previous_alteration = SCM_BOOL_F;
192
193   SCM from_same_octave = ly_assoc_get (scm_cons (scm_int2num (o),
194                                                  scm_int2num (n)), sig, SCM_BOOL_F);
195   SCM from_key_signature = ly_assoc_get (scm_int2num (n), sig, SCM_BOOL_F);
196   SCM from_other_octaves = SCM_BOOL_F;
197   for (SCM s = sig; scm_is_pair (s); s = scm_cdr (s))
198     {
199       SCM entry = scm_car (s);
200       if (scm_is_pair (scm_car (entry))
201           && scm_cdar (entry) == scm_int2num (n))
202         from_other_octaves = scm_cdr (entry);
203     }
204
205   if (from_same_octave != SCM_BOOL_F
206       && recent_enough (bar_number, from_same_octave, laziness))
207     {
208       previous_alteration = from_same_octave;
209     }
210   else if (ignore_octave
211            && from_other_octaves != SCM_BOOL_F
212            && recent_enough (bar_number, from_other_octaves, laziness))
213     {
214       previous_alteration = from_other_octaves;
215     }
216   else if (from_key_signature != SCM_BOOL_F)
217     {
218       previous_alteration = from_key_signature;
219     }
220
221   int num = 1;
222   if (is_tied (previous_alteration))
223     {
224       num = 1;
225       *different = true;
226     }
227   else
228     {
229       int prev = extract_alteration (previous_alteration);
230       int alter = pitch->get_alteration ();
231
232       if (alter == prev)
233         num = 0;
234       else if ((abs (alter) < abs (prev) || prev * alter < 0) && alter != 0)
235         num = 2;
236       *different = (alter != prev);
237     }
238   return num;
239 }
240
241 static int
242 number_accidentals (bool *different,
243                     Pitch *pitch, Context *origin,
244                     SCM accidentals, int bar_number)
245 {
246   int number = 0;
247
248   *different = false;
249   if (scm_is_pair (accidentals) && !scm_is_symbol (scm_car (accidentals)))
250     warning (_f ("accidental typesetting list must begin with context-name: %s",
251                  ly_scm2string (scm_car (accidentals)).to_str0 ()));
252
253   for (; scm_is_pair (accidentals) && origin;
254        accidentals = scm_cdr (accidentals))
255     {
256       // If pair then it is a new accidentals typesetting rule to be checked
257       SCM rule = scm_car (accidentals);
258       if (scm_is_pair (rule))
259         {
260           SCM type = scm_car (rule);
261           SCM laziness = scm_cdr (rule);
262           SCM localsig = origin->get_property ("localKeySignature");
263
264           bool same_octave_b
265             = scm_is_eq (ly_symbol2scm ("same-octave"), type);
266           bool any_octave_b
267             = scm_is_eq (ly_symbol2scm ("any-octave"), type);
268
269           if (same_octave_b || any_octave_b)
270             {
271               bool d = false;
272               int n = number_accidentals_from_sig
273                 (&d, localsig, pitch, bar_number, laziness, any_octave_b);
274               *different = *different || d;
275               number = max (number, n);
276             }
277           else
278             warning (_f ("ignoring unknown accidental: %s",
279                          ly_symbol2string (type).to_str0 ()));
280         }
281
282       /* if symbol then it is a context name.  Scan parent contexts to
283          find it. */
284       else if (scm_is_symbol (rule))
285         {
286           Context *dad = origin;
287           while (dad && !dad->is_alias (rule))
288             dad = dad->get_parent_context ();
289
290           if (dad)
291             origin = dad;
292         }
293       else
294         warning (_f ("pair or context-name expected for accidental rule, found %s",
295                      ly_scm2string (rule).to_str0 ()));
296     }
297
298   return number;
299 }
300
301 int
302 Accidental_engraver::get_bar_number ()
303 {
304   SCM barnum = get_property ("currentBarNumber");
305   SCM smp = get_property ("measurePosition");
306
307   int bn = robust_scm2int (barnum, 0);
308
309   Moment mp = robust_scm2moment (smp, Moment (0));
310   if (mp.main_part_ < Rational (0))
311     bn--;
312
313   return bn;
314 }
315
316 void
317 Accidental_engraver::process_acknowledged ()
318 {
319   if (accidentals_.size () && !accidentals_.top ().done_)
320     {
321       SCM accidentals = get_property ("autoAccidentals");
322       SCM cautionaries = get_property ("autoCautionaries");
323       int barnum = get_bar_number ();
324
325       for (int i = 0; i < accidentals_.size (); i++)
326         {
327           if (accidentals_[i].done_)
328             continue;
329           accidentals_[i].done_ = true;
330
331           Music *note = accidentals_[i].melodic_;
332           Context *origin = accidentals_[i].origin_;
333
334           Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
335           if (!pitch)
336             continue;
337
338           bool different = false;
339           bool different_caut = false;
340
341           int num = number_accidentals (&different,
342                                         pitch, origin,
343                                         accidentals, barnum);
344           int num_caut = number_accidentals (&different_caut,
345                                              pitch, origin,
346                                              cautionaries, barnum);
347
348           bool cautionary = to_boolean (note->get_property ("cautionary"));
349
350           if (num_caut > num)
351             {
352               num = num_caut;
353               different = different_caut;
354               cautionary = true;
355             }
356
357           if (num == 0 && to_boolean (note->get_property ("force-accidental")))
358             num = 1;
359
360           /* Cannot look for ties: it's not guaranteed that they reach
361              us before the notes. */
362           if (num)
363             {
364               create_accidental (&accidentals_[i], num > 1, cautionary);
365             }
366         }
367     }
368 }
369
370 void
371 Accidental_engraver::create_accidental (Accidental_entry *entry,
372                                         bool restore_natural,
373                                         bool cautionary)
374 {
375   Music *note = entry->melodic_;
376   Grob *support = entry->head_;
377   Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
378   
379   
380   bool as_suggestion = to_boolean (entry->origin_->get_property ("suggestAccidentals"));
381   Grob *a = 0;
382   if (as_suggestion)
383     a = make_suggested_accidental (note, support, entry->origin_engraver_);
384   else
385     a = make_standard_accidental (note, support, entry->origin_engraver_);
386  
387   SCM accs = scm_cons (scm_int2num (pitch->get_alteration ()),
388                        SCM_EOL);
389   if (restore_natural)
390     {
391       if (to_boolean (get_property ("extraNatural")))
392         accs = scm_cons (scm_int2num (0), accs);
393     }
394   
395   /* TODO: add cautionary option in accidental. */
396   if (cautionary)
397     a->set_property ("cautionary", SCM_BOOL_T);
398
399
400   a->set_property ("accidentals", accs);
401   entry->accidental_ = a;
402 }
403
404 Grob *
405 Accidental_engraver::make_standard_accidental (Music *note,
406                                                Grob *support,
407                                                Engraver *trans)
408 {
409   
410   /*
411     We construct the accidentals at the originating Voice
412     level, so that we get the property settings for
413     Accidental from the respective Voice.
414   */
415   Grob *a
416     = make_grob_from_properties (trans,
417                                  ly_symbol2scm ("Accidental"),
418                                  note->self_scm (),
419                                  "Accidental");
420
421   /*
422     We add the accidentals to the support of the arpeggio,
423     so it is put left of the accidentals.
424   */
425   for (int i = 0; i < left_objects_.size (); i++)
426     Side_position_interface::add_support (left_objects_[i], a);
427   for (int i = 0; i < right_objects_.size (); i++)
428     Side_position_interface::add_support (a, right_objects_[i]);
429
430   a->set_parent (support, Y_AXIS);
431
432   if (!accidental_placement_)
433     accidental_placement_ = make_item ("AccidentalPlacement",
434                                        a->self_scm ());
435   Accidental_placement::add_accidental (accidental_placement_, a);
436   
437   support->set_object ("accidental-grob", a->self_scm ());
438
439   return a;
440 }
441
442
443 Grob *
444 Accidental_engraver::make_suggested_accidental (Music *note,
445                                                    Grob *note_head, Engraver *trans)
446 {
447   
448   Grob *a
449     = make_grob_from_properties (trans,
450                                  ly_symbol2scm ("AccidentalSuggestion"),
451                                  note->self_scm (),
452                                  "AccidentalSuggestion");
453
454   
455   Side_position_interface::add_support (a, note_head);
456   if (Grob *stem = unsmob_grob (a->get_object ("stem")))
457     {
458       Side_position_interface::add_support (a, stem);
459     }
460
461   a->set_parent (note_head, X_AXIS);
462   return a;
463 }
464
465
466 void
467 Accidental_engraver::finalize ()
468 {
469   last_keysig_ = SCM_EOL;
470 }
471
472 void
473 Accidental_engraver::stop_translation_timestep ()
474 {
475   for (int j = ties_.size (); j--;)
476     {
477       Grob *r = Tie::head (ties_[j], RIGHT);
478       for (int i = accidentals_.size (); i--;)
479         if (accidentals_[i].head_ == r)
480           {
481             if (Grob *g = accidentals_[i].accidental_)
482               {
483                 g->set_object ("tie", ties_[j]->self_scm ());
484                 accidentals_[i].tied_ = true;
485               }
486             ties_.del (j);
487             break;
488           }
489     }
490
491   for (int i = accidentals_.size (); i--;)
492     {
493       int barnum = get_bar_number ();
494
495       Music *note = accidentals_[i].melodic_;
496       Context *origin = accidentals_[i].origin_;
497
498       Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
499       if (!pitch)
500         continue;
501
502       int n = pitch->get_notename ();
503       int o = pitch->get_octave ();
504       int a = pitch->get_alteration ();
505       SCM key = scm_cons (scm_int2num (o), scm_int2num (n));
506
507       while (origin
508              && origin->where_defined (ly_symbol2scm ("localKeySignature")))
509         {
510           /*
511             huh? we set props all the way to the top?
512           */
513           SCM localsig = origin->get_property ("localKeySignature");
514           bool change = false;
515           if (accidentals_[i].tied_)
516             {
517               /*
518                 Remember an alteration that is different both from
519                 that of the tied note and of the key signature.
520               */
521               localsig = ly_assoc_front_x
522                 (localsig, key, scm_cons (SCM_BOOL_T, scm_int2num (barnum)));
523
524               change = true;
525             }
526           else
527             {
528               /*
529                 not really really correct if there are more than one
530                 noteheads with the same notename.
531               */
532               localsig = ly_assoc_front_x (localsig, key,
533                                            scm_cons (scm_int2num (a),
534                                                      scm_int2num (barnum)));
535               change = true;
536             }
537
538           if (change)
539             origin->set_property ("localKeySignature", localsig);
540
541           origin = origin->get_parent_context ();
542         }
543     }
544
545   accidental_placement_ = 0;
546   accidentals_.clear ();
547   left_objects_.clear ();
548   right_objects_.clear ();
549 }
550
551 void
552 Accidental_engraver::acknowledge_rhythmic_head (Grob_info info)
553 {
554   Music *note = info.music_cause ();
555   if (note
556       && note->is_mus_type ("note-event"))
557     {
558       /*
559         String harmonics usually don't have accidentals.
560        */
561       if (to_boolean (get_property ("harmonicAccidentals"))
562           || !ly_is_equal (info.grob ()->get_property ("style"),
563                             ly_symbol2scm ("harmonic")))
564         {
565           Accidental_entry entry;
566           entry.head_ = info.grob ();
567           entry.origin_engraver_ = dynamic_cast<Engraver *> (info.origin_translator ());
568           entry.origin_ = entry.origin_engraver_->context ();
569           entry.melodic_ = note;
570
571           accidentals_.push (entry);
572         }
573     }
574 }
575
576 void
577 Accidental_engraver::acknowledge_tie (Grob_info info)
578 {
579   ties_.push (dynamic_cast<Spanner *> (info.grob ()));
580 }
581
582 void
583 Accidental_engraver::acknowledge_arpeggio (Grob_info info)
584 {
585   left_objects_.push (info.grob ());
586 }
587
588 void
589 Accidental_engraver::acknowledge_finger (Grob_info info)
590 {
591   left_objects_.push (info.grob ());
592 }
593
594 void
595 Accidental_engraver::process_music ()
596 {
597   SCM sig = get_property ("keySignature");
598   /* Detect key sig changes.
599      Update all parents and children.  */
600   if (last_keysig_ != sig)
601     update_local_key_signature ();
602 }
603
604
605 ADD_ACKNOWLEDGER(Accidental_engraver, arpeggio);
606 ADD_ACKNOWLEDGER(Accidental_engraver, finger);
607 ADD_ACKNOWLEDGER(Accidental_engraver, rhythmic_head);
608 ADD_ACKNOWLEDGER(Accidental_engraver, tie);
609
610 ADD_TRANSLATOR (Accidental_engraver,
611                 "Make accidentals.  "
612                 "Catch note heads, ties and notices key-change events.  "
613                 "This engraver usually lives at Staff level, but "
614                 "reads the settings for Accidental at @code{Voice} level, "
615                 "so you can @code{\\override} them at @code{Voice}. ",
616                 "Accidental AccidentalSuggestion",
617
618                 "",
619                 
620                 /* acks */
621                 "",
622
623                 "autoAccidentals "
624                 "autoCautionaries "
625                 "extraNatural "
626                 "harmonicAccidentals "
627                 "localKeySignature",
628                 "localKeySignature");