]> git.donarmstrong.com Git - lilypond.git/blob - lily/ambitus-engraver.cc
85dbbd46b87f3b396dd01c5b350651ed946bd942
[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 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 "musical-request.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" request 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" request and the next one (or the end of the
52  * piece, if there is no more such request).  To be compliant with the
53  * current implementation, we might implicitly assume an "\ambitus"
54  * request 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 acknowledge_grob (Grob_info);
68   virtual void stop_translation_timestep ();
69   virtual void finalize ();
70
71 private:
72   void create_ambitus ();
73   Item *ambitus_p_;
74   int isActive;
75   Pitch pitch_min, pitch_max;
76 };
77
78 Ambitus_engraver::Ambitus_engraver ()
79 {
80   ambitus_p_ = 0; isActive = 0;
81
82   // (pitch_min > pitch_max) means that pitches are not yet
83   // initialized
84   pitch_min = Pitch (0, 0, +1);
85   pitch_max = Pitch (0, 0, -1);
86 }
87
88 void
89 Ambitus_engraver::stop_translation_timestep ()
90 {
91   if (!ambitus_p_) {
92     // Create ambitus not before stopping timestep.  centralCPosition
93     // will then be the same as that for the first timestep.
94     //
95     // TODO: is this really a good idea?  At least, creating the
96     // ambitus in start_translation_timestep is a *bad* idea, since we
97     // may then oversee a clef that is defined in a staff context if
98     // we are in a voice context; centralCPosition would then be
99     // assumed to be 0.
100     create_ambitus ();
101   }
102   if (ambitus_p_ && isActive)
103     {
104       SCM key_signature = get_property ("keySignature");
105       ambitus_p_->set_grob_property ("keySignature", key_signature);
106       typeset_grob (ambitus_p_);
107       isActive = 0;
108     }
109 }
110
111 void
112 Ambitus_engraver::acknowledge_grob (Grob_info info)
113 {
114   if (!ambitus_p_) {
115     create_ambitus ();
116   }
117   if (!ambitus_p_)
118     return;
119   Item *item = dynamic_cast <Item *>(info.grob_l_);
120   if (item)
121     {
122       if (Note_head::has_interface (info.grob_l_))
123         {
124           Note_req *nr = dynamic_cast<Note_req*> (info.music_cause ());
125           if (nr)
126             {
127               Pitch pitch = *unsmob_pitch (nr->get_mus_property ("pitch"));
128               if (Pitch::compare (pitch_min, pitch_max) > 0) // already init'd?
129                 {
130                   // not yet init'd; use current pitch to init min/max
131                   pitch_min = pitch;
132                   pitch_max = pitch;
133                 }
134               else if (Pitch::compare (pitch, pitch_max) > 0) // new max?
135                 {
136                   pitch_max = pitch;
137                 }
138               else if (Pitch::compare (pitch, pitch_min) < 0) // new min?
139                 {
140                   pitch_min = pitch;
141                 }
142             }
143         }
144     }
145 }
146
147 void
148 Ambitus_engraver::create_ambitus ()
149 {
150   SCM basicProperties = get_property ("Ambitus");
151   SCM c0 = get_property ("centralCPosition");
152   ambitus_p_ = new Item (basicProperties); isActive = 1;
153   ambitus_p_->set_grob_property ("centralCPosition", c0);
154   announce_grob (ambitus_p_, SCM_EOL);
155 }
156
157 void
158 Ambitus_engraver::finalize ()
159 {
160   if (ambitus_p_)
161     {
162       if (Pitch::compare (pitch_min, pitch_max) <= 0)
163         {
164           ambitus_p_->set_grob_property ("pitch-min",
165                                          pitch_min.smobbed_copy ());
166           ambitus_p_->set_grob_property ("pitch-max",
167                                          pitch_max.smobbed_copy ());
168         }
169       else // have not seen any pitch, so forget about the ambitus
170         {
171           // Do not print a warning on empty ambitus range, since this
172           // most probably arises from an empty voice, such as shared
173           // global timesig/clef definitions.
174 #if 0
175           ambitus_p_->warning("empty ambitus range [ignored]");
176 #endif
177           ambitus_p_->suicide();
178         }
179     }
180 }
181
182 ENTER_DESCRIPTION(Ambitus_engraver,
183 /* descr */       "",
184 /* creats*/       "Ambitus",
185 /* acks  */       "note-head-interface",
186 /* reads */       "",
187 /* write */       "");