2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 Jan Nieuwenhuizen <janneke@gnu.org>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
22 #include "engraver.hh"
24 #include "note-head.hh"
25 #include "stream-event.hh"
26 #include "international.hh"
28 #include "translator.icc"
31 Generate texts for lyric syllables. We only do one lyric at a time.
32 Multiple copies of this engraver should be used to do multiple voices.
34 class Lyric_engraver : public Engraver
37 void stop_translation_timestep ();
38 void process_music ();
39 void listen_lyric (Stream_event *);
42 TRANSLATOR_DECLARATIONS (Lyric_engraver);
49 Context *get_voice_context ();
52 Lyric_engraver::Lyric_engraver ()
60 Lyric_engraver::listen_lyric (Stream_event *ev)
62 ASSIGN_EVENT_ONCE (event_, ev);
66 Lyric_engraver::process_music ()
70 SCM text = event_->get_property ("text");
72 if (ly_is_equal (text, scm_from_ascii_string (" ")))
75 last_text_->set_property ("self-alignment-X",
76 get_property ("lyricMelismaAlignment"));
79 text_ = make_item ("LyricText", event_->self_scm ());
82 Context *voice = get_voice_to_lyrics (context ());
85 && to_boolean (voice->get_property ("melismaBusy"))
86 && !to_boolean (context ()->get_property ("ignoreMelismata")))
87 last_text_->set_property ("self-alignment-X",
88 get_property ("lyricMelismaAlignment"));
92 get_voice_to_lyrics (Context *lyrics)
94 bool searchForVoice = to_boolean (lyrics->get_property ("searchForVoice"));
96 SCM avc = lyrics->get_property ("associatedVoiceContext");
97 if (Context *c = unsmob<Context> (avc))
99 if (!c->is_removable ())
103 SCM voice_name = lyrics->get_property ("associatedVoice");
104 string nm = lyrics->id_string ();
106 if (scm_is_string (voice_name))
107 nm = ly_scm2string (voice_name);
108 else if (nm == "" || !searchForVoice)
112 ssize idx = nm.rfind ('-');
114 nm = nm.substr (0, idx);
117 SCM voice_type = lyrics->get_property ("associatedVoiceType");
118 if (!scm_is_symbol (voice_type))
121 Context *voice = find_context_near (lyrics, voice_type, nm);
125 return find_context_near (lyrics, voice_type, "");
129 get_current_note_head (Context *voice)
131 Moment now = voice->now_mom ();
132 for (SCM s = voice->get_property ("busyGrobs");
133 scm_is_pair (s); s = scm_cdr (s))
135 Grob *g = unsmob<Grob> (scm_cdar (s));;
136 Moment *end_mom = unsmob<Moment> (scm_caar (s));
139 programming_error ("busyGrobs invalid");
143 // It's a bit irritating that we just have the length and
144 // duration of the Grob.
145 Moment end_from_now =
146 get_event_length (unsmob<Stream_event> (g->get_property ("cause")), now)
148 // We cannot actually include more than a single grace note
149 // using busyGrobs on ungraced lyrics since a grob ending on
150 // grace time will just have disappeared from busyGrobs by the
151 // time our ungraced lyrics appear. At best we may catch a
152 // single grace note.
154 // However, a single grace note ending on a non-grace time is
155 // indistinguishable from a proper note ending on a non-grace
156 // time. So we really have no way to obey includeGraceNotes
157 // here. Not with this mechanism.
158 if ((*end_mom == end_from_now)
159 && dynamic_cast<Item *> (g)
160 && has_interface<Note_head> (g))
170 Lyric_engraver::stop_translation_timestep ()
174 Context *voice = get_voice_to_lyrics (context ());
178 Grob *head = get_current_note_head (voice);
182 text_->set_parent (head->get_parent(X_AXIS), X_AXIS);
183 if (melisma_busy (voice)
184 && !to_boolean (get_property ("ignoreMelismata")))
185 text_->set_property ("self-alignment-X",
186 get_property ("lyricMelismaAlignment"));
197 Lyric_engraver::boot ()
199 ADD_LISTENER (Lyric_engraver, lyric);
202 ADD_TRANSLATOR (Lyric_engraver,
204 "Engrave text for lyrics.",
211 "lyricMelismaAlignment "