1 /*
2 This file is part of LilyPond, the GNU music typesetter.
3
4 Copyright (C) 2002--2020 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "music.hh"
21 #include "sequential-iterator.hh"
22 #include "context.hh"
23 #include "lily-imports.hh"
24
25 using std::string;
26
27 class Volta_repeat_iterator final : public Sequential_iterator
28 {
29 public:
30 DECLARE_SCHEME_CALLBACK (constructor, ());
31 Volta_repeat_iterator () = default;
32
33 void add_repeat_command (SCM);
34 protected:
35 SCM get_music_list () const override;
36 void next_element () override;
37 void create_children () override;
38 void process (Moment) override;
39 void derived_mark () const override;
40
41 private:
empty() const42 bool empty () const
43 {
44 return !music_get_length ().to_bool () && !music_start_mom ().grace_part_;
45 }
46
47 private:
48 bool first_time_ = true;
49 int alt_count_ = 0;
50 int rep_count_ = 0;
51 int done_count_ = 0;
52 SCM alt_restores_ = SCM_EOL;
53 };
54
55 void
derived_mark() const56 Volta_repeat_iterator::derived_mark () const
57 {
58 scm_gc_mark (alt_restores_);
59 Sequential_iterator::derived_mark ();
60 }
61
62 SCM
get_music_list() const63 Volta_repeat_iterator::get_music_list ()const
64 {
65 return scm_cons (get_property (get_music (), "element"),
66 Sequential_iterator::get_music_list ());
67 }
68
69 void
create_children()70 Volta_repeat_iterator::create_children ()
71 {
72 Sequential_iterator::create_children ();
73
74 SCM alts = get_property (get_music (), "elements");
75
76 alt_count_ = int (scm_ilength (alts));
77 rep_count_ = scm_to_int (get_property (get_music (), "repeat-count"));
78 done_count_ = 0;
79 }
80
81 /*
82 TODO: add source information for debugging
83 */
84 void
add_repeat_command(SCM what)85 Volta_repeat_iterator::add_repeat_command (SCM what)
86 {
87 SCM reps = ly_symbol2scm ("repeatCommands");
88 SCM current_reps = SCM_EOL;
89 Context *where = get_context ()->where_defined (reps, ¤t_reps);
90
91 if (where && ly_cheap_is_list (current_reps))
92 {
93 current_reps = scm_cons (what, current_reps);
94 set_property (where, reps, current_reps);
95 }
96 }
97
98 void
next_element()99 Volta_repeat_iterator::next_element ()
100 {
101 done_count_++;
102
103 Sequential_iterator::next_element ();
104
105 if (alt_count_)
106 {
107 string repstr = std::to_string (rep_count_ - alt_count_ + done_count_) + ".";
108 if (done_count_ <= 1)
109 {
110 alt_restores_ = SCM_EOL;
111 if (from_scm<bool> (get_property (get_context (), "timing")))
112 {
113 for (SCM lst = get_property (get_context (), "alternativeRestores");
114 scm_is_pair (lst);
115 lst = scm_cdr (lst))
116 {
117 SCM res = SCM_EOL;
118 Context *t = get_context ()->where_defined (scm_car (lst),
119 &res);
120 if (t)
121 {
122 alt_restores_ = scm_cons
123 (scm_list_3 (t->self_scm (), scm_car (lst), res),
124 alt_restores_);
125 }
126 }
127 }
128 }
129 else
130 {
131
132 add_repeat_command (scm_list_2 (ly_symbol2scm ("volta"), SCM_BOOL_F));
133
134 if (done_count_ - 1 < alt_count_)
135 {
136 add_repeat_command (ly_symbol2scm ("end-repeat"));
137
138 if (from_scm<bool> (get_property (get_context (), "timing")))
139 {
140 SCM mps = ly_symbol2scm ("measurePosition");
141 for (SCM p = alt_restores_; scm_is_pair (p); p = scm_cdr (p))
142 {
143 SCM ls = scm_car (p);
144 if (scm_is_eq (scm_cadr (ls), mps))
145 // Repeats may have different grace timing, so
146 // we need to adjust the measurePosition grace
147 // timing to that of the current alternative
148 // rather than that of the first. The
149 // Timing_translator does this already but is
150 // too late to avoid bad side-effects
151 {
152 Moment mp (unsmob<Moment> (scm_caddr (ls))->main_part_,
153 get_context ()->now_mom ().grace_part_);
154 Lily::ly_context_set_property_x (scm_car (ls),
155 mps,
156 mp.smobbed_copy ());
157 }
158 else
159 scm_apply_0 (Lily::ly_context_set_property_x, ls);
160 }
161 }
162 }
163 }
164
165 if (done_count_ == 1 && alt_count_ < rep_count_)
166 repstr = "1.--" + std::to_string (rep_count_ - alt_count_ + done_count_) + ".";
167
168 if (done_count_ <= alt_count_)
169 add_repeat_command (scm_list_2 (ly_symbol2scm ("volta"),
170 ly_string2scm (repstr)));
171 }
172 else
173 {
174 if (!empty ())
175 add_repeat_command (ly_symbol2scm ("end-repeat"));
176 }
177 }
178
179 void
process(Moment m)180 Volta_repeat_iterator::process (Moment m)
181 {
182 if (first_time_)
183 {
184 // Robustness: Avoid printing a misleading bar line for a zero-duration
185 // repeated section.
186 if (!empty ())
187 add_repeat_command (ly_symbol2scm ("start-repeat"));
188 first_time_ = false;
189 }
190 Sequential_iterator::process (m);
191 }
192
193 IMPLEMENT_CTOR_CALLBACK (Volta_repeat_iterator);
194