]> git.donarmstrong.com Git - lilypond.git/blob - lily/ambitus-engraver.cc
1982beeff142b8de84cae73a19c3fef098b2c350
[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--2003 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
16 /*
17  * This class implements an engraver for ambitus grobs.
18  *
19  * TODO: There are quite some conceptional issues left open:
20  *
21  * - Many publishers put ambitus _before_ the first occurrence of a
22  * clef.  Hence, formally the pitches are undefined in this case.  Of
23  * course, one could always silently assume that ambitus pitches refer
24  * to the first occurrence of a clef.  Or should we, by default, put
25  * the ambitus always after the first clef, if any?
26  *
27  * - Enharmonically equal pitches: Assume piece contains once a "gis",
28  * another time an "aes" as highest pitch.  Which one should be
29  * selected for the ambitus grob?  The "aes", because it is
30  * musically/notationally "higher" than "gis"?  Or "gis", because (if
31  * using pure temperament) it has a slightly higher frequency?  Or
32  * that pitch that come closer to the key signature?  But there may be
33  * key signature changes in the piece...
34  *
35  * - Multiple voices in single staff: Assume a vocal piece of music,
36  * where the soprano voice and the alto voice are put into the same
37  * staff (this is generally a bad idea, but unfortunately common
38  * practice).  Then, there probably should be two ambitus grobs, one
39  * for each voice.  But how can you see which ambitus grob refers to
40  * which voice?  Most probably you can guess it from the fact that the
41  * ambitus of the alto voice typically lies in a lower range than that
42  * of the soprano voice, but this is just a heuristic rather than a
43  * generally valid rule.  In the case of only two voices, using stems
44  * in the ambitus grob might help, but probably looks quite ugly.
45  *
46  * - If a piece consists of several loosely coupled sections, should
47  * there be multiple ambitus grobs allowed, one for each section?
48  * Then there probably should be some "\ambitus" event added to
49  * mudela, stating where an ambitus grob should be placed.  This
50  * ambitus grob should then represent the ambitus in the range of time
51  * between this "\ambitus" event and the next one (or the end of the
52  * piece, if there is no more such event).  To be compliant with the
53  * current implementation, we might implicitly assume an "\ambitus"
54  * event at the beginning of the piece, but then the question where
55  * to put this first ambitus grob (before/after the clef?) becomes
56  * even more urgent.
57  *
58  * - Incipits of transcribed music may need special treatment for
59  * ambitus, since, for readability, the ambitus most probably should
60  * not refer to the ancient clefs of the incipit, but rather to the
61  * clefs used in the transcribed parts.
62  */
63 class Ambitus_engraver : public Engraver
64 {
65 public:
66 TRANSLATOR_DECLARATIONS(Ambitus_engraver);
67   virtual void process_music ();
68   virtual void acknowledge_grob (Grob_info);
69   virtual void stop_translation_timestep ();
70   virtual void finalize ();
71
72 private:
73   void create_ambitus ();
74   Item *ambitus_;
75   int/*bool*/ is_typeset;
76   Pitch pitch_min, pitch_max;
77 };
78
79 Ambitus_engraver::Ambitus_engraver ()
80 {
81   ambitus_ = 0;
82   is_typeset = 0;
83
84   /*
85    * (pitch_min > pitch_max) means that pitches are not yet
86    * initialized
87    */
88   pitch_min = Pitch (0, 0, SHARP);
89   pitch_max = Pitch (0, 0, FLAT);
90 }
91
92 void
93 Ambitus_engraver::process_music ()
94 {
95   /*
96    * Ensure that ambitus is created in the very first timestep (on
97    * which lily does not call start_translation_timestep ()).
98    * Otherwise, if a voice begins with a rest, the ambitus grob will
99    * be placed after the rest.
100    */
101   if (!ambitus_) {
102     create_ambitus ();
103   }
104 }
105
106 void
107 Ambitus_engraver::stop_translation_timestep ()
108 {
109   if (ambitus_ && !is_typeset)
110     {
111       /*
112        * Evaluate centralCPosition not until now, since otherwise we
113        * may then oversee a clef that is defined in a staff context if
114        * we are in a voice context; centralCPosition would then be
115        * assumed to be 0.
116        */
117       SCM c0 = get_property ("centralCPosition");
118       ambitus_->set_grob_property ("c0-position", c0);
119
120       /*
121        * Similar for keySignature.
122        */
123       SCM key_signature = get_property ("keySignature");
124       ambitus_->set_grob_property ("accidentals", key_signature);
125
126       typeset_grob (ambitus_);
127       is_typeset = 1;
128     }
129 }
130
131 void
132 Ambitus_engraver::acknowledge_grob (Grob_info info)
133 {
134   Item *item = dynamic_cast <Item *>(info.grob_);
135   if (item)
136     {
137       if (Note_head::has_interface (info.grob_))
138         {
139           Music *nr = info.music_cause ();
140           if (nr && nr->is_mus_type ("note-event"))
141             {
142               Pitch pitch = *unsmob_pitch (nr->get_mus_property ("pitch"));
143               if (Pitch::compare (pitch_min, pitch_max) > 0) // already init'd?
144                 {
145                   // not yet init'd; use current pitch to init min/max
146                   pitch_min = pitch;
147                   pitch_max = pitch;
148                 }
149               else if (Pitch::compare (pitch, pitch_max) > 0) // new max?
150                 {
151                   pitch_max = pitch;
152                 }
153               else if (Pitch::compare (pitch, pitch_min) < 0) // new min?
154                 {
155                   pitch_min = pitch;
156                 }
157             }
158         }
159     }
160 }
161
162 void
163 Ambitus_engraver::create_ambitus ()
164 {
165   ambitus_ = make_item ("Ambitus");
166   is_typeset = 0;               // UGH.
167   announce_grob (ambitus_, SCM_EOL);
168 }
169
170 void
171 Ambitus_engraver::finalize ()
172 {
173   if (ambitus_)
174     {
175       if (Pitch::compare (pitch_min, pitch_max) <= 0)
176         {
177           ambitus_->set_grob_property ("pitch-min",
178                                          pitch_min.smobbed_copy ());
179           ambitus_->set_grob_property ("pitch-max",
180                                          pitch_max.smobbed_copy ());
181         }
182       else // have not seen any pitch, so forget about the ambitus
183         {
184           /*
185            * Do not print a warning on empty ambitus range, since this
186            * most probably arises from an empty voice, such as shared
187            * global timesig/clef definitions.
188            */
189           ambitus_->suicide();
190         }
191     }
192 }
193
194 ENTER_DESCRIPTION(Ambitus_engraver,
195 /* descr */       "",
196 /* creats*/       "Ambitus",
197 /* accepts */ "",
198 /* acks  */     "note-head-interface",
199 /* reads */       "",
200 /* write */       "");