]> git.donarmstrong.com Git - lilypond.git/blob - lily/ambitus-engraver.cc
* lily/ledger-line-engraver.cc: new file.
[lilypond.git] / lily / ambitus-engraver.cc
1 /*
2   ambitus-engraver.cc -- implement Ambitus_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2002--2004 Juergen Reuter <reuter@ipd.uka.de>
7 */
8
9 #include "engraver.hh"
10 #include "item.hh"
11 #include "note-head.hh"
12 #include "staff-symbol-referencer.hh"
13 #include "event.hh"
14 #include "pitch.hh"
15 #include "pitch-interval.hh"
16 #include "protected-scm.hh"
17
18
19
20 /*
21  * This class implements an engraver for ambitus grobs.
22  *
23  * TODO: There are quite some conceptional issues left open:
24  *
25  * - Many publishers put ambitus _before_ the first occurrence of a
26  * clef.  Hence, formally the pitches are undefined in this case.  Of
27  * course, one could always silently assume that ambitus pitches refer
28  * to the first occurrence of a clef.  Or should we, by default, put
29  * the ambitus always after the first clef, if any?
30  *
31  * - Enharmonically equal pitches: Assume piece contains once a "gis",
32  * another time an "aes" as highest pitch.  Which one should be
33  * selected for the ambitus grob?  The "aes", because it is
34  * musically/notationally "higher" than "gis"?  Or "gis", because (if
35  * using pure temperament) it has a slightly higher frequency?  Or
36  * that pitch that come closer to the key signature?  But there may be
37  * key signature changes in the piece...
38  *
39  * - Multiple voices in single staff: Assume a vocal piece of music,
40  * where the soprano voice and the alto voice are put into the same
41  * staff (this is generally a bad idea, but unfortunately common
42  * practice).  Then, there probably should be two ambitus grobs, one
43  * for each voice.  But how can you see which ambitus grob refers to
44  * which voice?  Most probably you can guess it from the fact that the
45  * ambitus of the alto voice typically lies in a lower range than that
46  * of the soprano voice, but this is just a heuristic rather than a
47  * generally valid rule.  In the case of only two voices, using stems
48  * in the ambitus grob might help, but probably looks quite ugly.
49  *
50  * - If a piece consists of several loosely coupled sections, should
51  * there be multiple ambitus grobs allowed, one for each section?
52  * Then there probably should be some "\ambitus" event added to
53  * mudela, stating where an ambitus grob should be placed.  This
54  * ambitus grob should then represent the ambitus in the range of time
55  * between this "\ambitus" event and the next one (or the end of the
56  * piece, if there is no more such event).  To be compliant with the
57  * current implementation, we might implicitly assume an "\ambitus"
58  * event at the beginning of the piece, but then the question where
59  * to put this first ambitus grob (before/after the clef?) becomes
60  * even more urgent.
61  *
62  * - Incipits of transcribed music may need special treatment for
63  * ambitus, since, for readability, the ambitus most probably should
64  * not refer to the ancient clefs of the incipit, but rather to the
65  * clefs used in the transcribed parts.
66  */
67 class Ambitus_engraver : public Engraver
68 {
69 public:
70 TRANSLATOR_DECLARATIONS (Ambitus_engraver);
71   virtual void process_music ();
72   virtual void acknowledge_grob (Grob_info);
73   virtual void stop_translation_timestep ();
74   virtual void finalize ();
75
76 private:
77   void create_ambitus ();
78   Item *ambitus_;
79   Drul_array<Item *> heads_;
80   Drul_array<Item *> accidentals_;
81   Pitch_interval pitch_interval_;
82   bool is_typeset_;
83   int start_c0_;
84   Protected_scm start_key_sig_;
85 };
86
87 void
88 Ambitus_engraver::create_ambitus ()
89 {
90   ambitus_ = make_item ("Ambitus",SCM_EOL);
91   is_typeset_ = false;          
92 }
93
94
95 Ambitus_engraver::Ambitus_engraver ()
96 {
97   ambitus_ = 0;
98   is_typeset_ = false;
99 }
100
101 void
102 Ambitus_engraver::process_music ()
103 {
104   /*
105    * Ensure that ambitus is created in the very first timestep (on
106    * which lily does not call start_translation_timestep ()).
107    * Otherwise, if a voice begins with a rest, the ambitus grob will
108    * be placed after the rest.
109    */
110   if (!ambitus_)
111     {
112       create_ambitus ();
113     }
114 }
115
116 void
117 Ambitus_engraver::stop_translation_timestep ()
118 {
119   if (ambitus_ && !is_typeset_)
120     {
121       /*
122        * Evaluate middleCPosition not until now, since otherwise we
123        * may then oversee a clef that is defined in a staff context if
124        * we are in a voice context; middleCPosition would then be
125        * assumed to be 0.
126        */
127       start_c0_ = robust_scm2int (get_property ("middleCPosition"), 0);
128       start_key_sig_ = get_property ("keySignature");
129
130       Direction d = DOWN;
131       do
132         {
133           heads_[d] = make_item ("AmbitusNoteHead", SCM_EOL);
134           accidentals_[d] = make_item ("AmbitusAccidental", SCM_EOL);
135           heads_[d]->set_property ("accidental-grob", accidentals_[d]->self_scm ());
136         }
137       while (flip (&d) != DOWN);
138       is_typeset_ = true;
139     }
140 }
141
142 void
143 Ambitus_engraver::acknowledge_grob (Grob_info info)
144 {
145   Item *item = dynamic_cast <Item *>(info.grob_);
146   if (item)
147     {
148       if (Note_head::has_interface (info.grob_))
149         {
150           Music *nr = info.music_cause ();
151           if (nr && nr->is_mus_type ("note-event"))
152             {
153               Pitch pitch = *unsmob_pitch (nr->get_property ("pitch"));
154               pitch_interval_.add_point (pitch);
155             }
156         }
157     }
158 }
159
160 void
161 Ambitus_engraver::finalize ()
162 {
163   if (ambitus_ && !pitch_interval_.is_empty ())
164     {
165       Direction d = DOWN;
166       do
167         {
168           Pitch p = pitch_interval_[d];
169           heads_[d]->set_property ("position",
170                                    scm_from_int (start_c0_ +
171                                                  p.steps ()));
172
173           SCM handle = scm_assoc (scm_cons (scm_from_int (p.get_octave ()),
174                                             scm_from_int (p.get_notename ())),
175                                   start_key_sig_);
176
177           int sig_alter = (handle != SCM_BOOL_F) ? ly_scm2int (ly_car (handle)) : 0;
178           if (sig_alter == p.get_alteration ())
179             {
180               accidentals_[d]->suicide();
181               heads_[d]->set_property ("accidental-grob", SCM_EOL);
182             }
183           else
184             {
185               accidentals_[d]->set_property ("accidentals",
186                                              scm_list_1 (scm_from_int (p.get_alteration ())));
187             }
188         }
189       while (flip (&d) != DOWN);
190
191       ambitus_->set_property ("note-heads", scm_list_2 (heads_[DOWN]->self_scm (),
192                                                         heads_[UP]->self_scm ()));
193     }
194   else
195     {
196       Direction d = DOWN;
197       do
198         {
199           accidentals_[d]->suicide();
200           heads_[d]->suicide();
201         }
202       while (flip (&d) != DOWN);
203       ambitus_->suicide();
204     }
205 }
206
207 ENTER_DESCRIPTION (Ambitus_engraver,
208 /* descr */       "",
209 /* creats*/       "Ambitus",
210 /* accepts */ "",
211 /* acks  */     "note-head-interface",
212 /* reads */       "",
213 /* write */       "");