]> git.donarmstrong.com Git - lilypond.git/blob - lily/volta-repeat-iterator.cc
Doc: NR section 3.5.x MIDI file creation tidy up
[lilypond.git] / lily / volta-repeat-iterator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2002--2015 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
24 class Volta_repeat_iterator : public Sequential_iterator
25 {
26 public:
27   DECLARE_SCHEME_CALLBACK (constructor, ());
28   Volta_repeat_iterator ();
29
30   void add_repeat_command (SCM);
31 protected:
32   virtual SCM get_music_list () const;
33   virtual void next_element (bool);
34   virtual void construct_children ();
35   virtual void process (Moment);
36   virtual void derived_mark () const;
37
38   bool first_time_;
39   int alt_count_;
40   int rep_count_;
41   int done_count_;
42   SCM alt_restores_;
43 };
44
45 Volta_repeat_iterator::Volta_repeat_iterator ()
46 {
47   done_count_ = alt_count_ = rep_count_ = 0;
48   first_time_ = true;
49   alt_restores_ = SCM_EOL;
50 }
51
52 void
53 Volta_repeat_iterator::derived_mark () const
54 {
55   scm_gc_mark (alt_restores_);
56   Sequential_iterator::derived_mark ();
57 }
58
59 SCM
60 Volta_repeat_iterator::get_music_list ()const
61 {
62   return scm_cons (get_music ()->get_property ("element"),
63                    Sequential_iterator::get_music_list ());
64 }
65
66 void
67 Volta_repeat_iterator::construct_children ()
68 {
69   Sequential_iterator::construct_children ();
70
71   SCM alts = get_music ()->get_property ("elements");
72
73   alt_count_ = int (scm_ilength (alts));
74   rep_count_ = scm_to_int (get_music ()->get_property ("repeat-count"));
75   done_count_ = 0;
76 }
77
78 /*
79   TODO: add source information for debugging
80 */
81 void
82 Volta_repeat_iterator::add_repeat_command (SCM what)
83 {
84   SCM reps = ly_symbol2scm ("repeatCommands");
85   SCM current_reps = SCM_EOL;
86   Context *where = get_outlet ()->where_defined (reps, &current_reps);
87
88   if (where && ly_cheap_is_list (current_reps))
89     {
90       current_reps = scm_cons (what, current_reps);
91       where->set_property (reps, current_reps);
92     }
93 }
94
95 void
96 Volta_repeat_iterator::next_element (bool side_effect)
97 {
98   done_count_++;
99
100   Sequential_iterator::next_element (side_effect);
101
102   if (side_effect)
103     {
104       if (alt_count_)
105         {
106           string repstr = ::to_string (rep_count_ - alt_count_ + done_count_) + ".";
107           if (done_count_ <= 1)
108             {
109               alt_restores_ = SCM_EOL;
110               if (to_boolean (get_outlet ()->get_property ("timing")))
111                 {
112                   for (SCM lst = get_outlet ()->get_property ("alternativeRestores");
113                        scm_is_pair (lst);
114                        lst = scm_cdr (lst))
115                     {
116                       SCM res = SCM_EOL;
117                       Context *t = get_outlet ()->where_defined (scm_car (lst),
118                                                                  &res);
119                       if (t)
120                         {
121                           alt_restores_ = scm_cons
122                             (scm_list_3 (t->self_scm (), scm_car (lst), res),
123                              alt_restores_);
124                         }
125                     }
126                 }
127             }
128           else
129             {
130
131               add_repeat_command (scm_list_n (ly_symbol2scm ("volta"), SCM_BOOL_F, SCM_UNDEFINED));
132
133               if (done_count_ - 1 < alt_count_)
134                 {
135                   add_repeat_command (ly_symbol2scm ("end-repeat"));
136
137                   if (to_boolean (get_outlet ()->get_property ("timing")))
138                     {
139                       for (SCM p = alt_restores_; scm_is_pair (p); p = scm_cdr (p))
140                         scm_apply_0 (ly_lily_module_constant ("ly:context-set-property!"),
141                                      scm_car (p));
142                     }
143                 }
144             }
145
146           if (done_count_ == 1 && alt_count_ < rep_count_)
147             repstr = "1.--" + ::to_string (rep_count_ - alt_count_ + done_count_) + ".";
148
149           if (done_count_ <= alt_count_)
150             add_repeat_command (scm_list_n (ly_symbol2scm ("volta"),
151                                             ly_string2scm (repstr), SCM_UNDEFINED));
152         }
153       else
154         add_repeat_command (ly_symbol2scm ("end-repeat"));
155     }
156 }
157
158 void
159 Volta_repeat_iterator::process (Moment m)
160 {
161   if (first_time_)
162     {
163       add_repeat_command (ly_symbol2scm ("start-repeat"));
164       first_time_ = false;
165     }
166   Sequential_iterator::process (m);
167 }
168
169 IMPLEMENT_CTOR_CALLBACK (Volta_repeat_iterator);