2 local-key-engraver.cc -- implement Local_key_engraver
4 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 #include "musical-request.hh"
8 #include "command-request.hh"
9 #include "local-key-item.hh"
10 #include "key-item.hh"
12 #include "note-head.hh"
13 #include "timing-translator.hh"
14 #include "engraver-group-engraver.hh"
15 #include "grace-align-item.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "side-position-interface.hh"
18 #include "engraver.hh"
22 Make accidentals. Catches note heads, ties and notices key-change
23 events. Due to interaction with ties (which don't come together
24 with note heads), this needs to be in a context higher than Tie_engraver.
27 struct Local_key_engraver : Engraver {
28 Local_key_item *key_item_p_;
30 VIRTUAL_COPY_CONS(Translator);
31 virtual void do_process_music();
32 virtual void acknowledge_element (Score_element_info);
33 virtual void do_pre_move_processing();
34 virtual void do_creation_processing ();
35 virtual void process_acknowledged ();
36 virtual void do_removal_processing ();
41 Key_engraver *key_grav_l_;
43 Link_array<Note_req> mel_l_arr_;
44 Link_array<Item> support_l_arr_;
45 Link_array<Item> forced_l_arr_;
46 Link_array<Item> tied_l_arr_;
49 Grace_align_item * grace_align_l_;
52 Local_key_engraver::Local_key_engraver()
57 last_keysig_ = SCM_EOL;
61 Local_key_engraver::do_creation_processing ()
63 last_keysig_ = get_property ("keySignature");
64 daddy_trans_l_->set_property ("localKeySignature", last_keysig_);
68 Local_key_engraver::process_acknowledged ()
70 SCM localsig = get_property ("localKeySignature");
72 if (!key_item_p_ && mel_l_arr_.size())
74 SCM f = get_property ("forgetAccidentals");
75 bool forget = to_boolean (f);
76 for (int i=0; i < mel_l_arr_.size(); i++)
78 Item * support_l = support_l_arr_[i];
79 Note_req * note_l = mel_l_arr_[i];
81 int n = note_l->pitch_.notename_i_;
82 int o = note_l->pitch_.octave_i_;
83 int a = note_l->pitch_.accidental_i_;
85 /* see if there's a tie that "changes" the accidental */
86 /* works because if there's a tie, the note to the left
87 is of the same pitch as the actual note */
89 SCM prev = scm_assoc (gh_cons (gh_int2scm (o), gh_int2scm (n)), localsig);
90 if (prev == SCM_BOOL_F)
91 prev = scm_assoc (gh_int2scm (n), localsig);
92 int prev_acc = (prev == SCM_BOOL_F) ? 0 : gh_scm2int (gh_cdr (prev));
93 bool different = prev_acc != a;
95 bool tie_changes = tied_l_arr_.find_l (support_l) && different;
97 && (note_l->forceacc_b_ || different)
102 key_item_p_ = new Local_key_item (get_property ("basicLocalKeyProperties"));
103 Side_position_interface (key_item_p_).set_axis (X_AXIS);
104 Side_position_interface (key_item_p_).set_direction (LEFT);
105 staff_symbol_referencer(key_item_p_).set_interface ();
107 announce_element (Score_element_info (key_item_p_, 0));
112 sign (prev_acc) * (prev_acc - a) == 1
113 && abs(prev_acc) == 2;
115 key_item_p_->add_pitch (note_l->pitch_,
116 note_l->cautionary_b_,
118 Side_position_interface (key_item_p_).add_support (support_l);
123 localsig = scm_assoc_set_x (localsig, gh_cons (gh_int2scm (o),
131 if (!tied_l_arr_.find_l (support_l))
133 local_key_.clear_internal_forceacc (note_l->pitch_);
135 else if (tie_changes)
137 local_key_.set_internal_forceacc (note_l->pitch_);
148 if (key_item_p_ && grace_align_l_)
150 Side_position_interface (grace_align_l_).add_support (key_item_p_);
157 Local_key_engraver::do_removal_processing ()
159 // TODO: if grace ? signal accidentals to Local_key_engraver the
163 Local_key_engraver::do_pre_move_processing()
167 for (int i=0; i < support_l_arr_.size(); i++)
168 Side_position_interface (key_item_p_).add_support (support_l_arr_[i]);
170 typeset_element (key_item_p_);
177 support_l_arr_.clear();
178 forced_l_arr_.clear();
182 Local_key_engraver::acknowledge_element (Score_element_info info)
184 SCM wg= get_property ("weAreGraceContext");
186 bool selfgr = gh_boolean_p (wg) &&gh_scm2bool (wg);
187 bool he_gr = to_boolean (info.elem_l_->get_elt_property ("grace"));
189 Grace_align_item * gai = dynamic_cast<Grace_align_item*> (info.elem_l_);
190 if (he_gr && !selfgr && gai)
192 grace_align_l_ = gai;
194 Note_req * note_l = dynamic_cast <Note_req *> (info.req_l_);
195 Note_head * note_head = dynamic_cast<Note_head *> (info.elem_l_);
200 if (note_l && note_head)
202 mel_l_arr_.push (note_l);
203 support_l_arr_.push (note_head);
205 else if (Tie * tie_l = dynamic_cast<Tie *> (info.elem_l_))
207 tied_l_arr_.push (tie_l->head (RIGHT));
212 Local_key_engraver::do_process_music()
214 SCM smp = get_property ("measurePosition");
215 Moment mp = (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
217 SCM sig = get_property ("keySignature");
220 if (!to_boolean (get_property ("noResetKey")))
221 daddy_trans_l_->set_property ("localKeySignature", sig);
223 else if (last_keysig_ != sig)
225 daddy_trans_l_->set_property ("localKeySignature", sig);
231 ADD_THIS_TRANSLATOR(Local_key_engraver);