]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeated-music.cc
use trap to remove tmp directory on failure to avoid hiding mpost
[lilypond.git] / lily / repeated-music.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2012 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 "repeated-music.hh"
21 #include "music-sequence.hh"
22 #include "pitch.hh"
23 #include "warn.hh"
24 #include "program-option.hh"
25
26 Music *
27 Repeated_music::body (Music *me)
28 {
29   return unsmob_music (me->get_property ("element"));
30 }
31
32 SCM
33 Repeated_music::alternatives (Music *me)
34 {
35   return me->get_property ("elements");
36 }
37
38 MAKE_SCHEME_CALLBACK (Repeated_music, relative_callback, 2);
39 SCM
40 Repeated_music::relative_callback (SCM music, SCM pitch)
41 {
42   Pitch p = *unsmob_pitch (pitch);
43   Music *me = unsmob_music (music);
44   if (lily_1_8_relative)
45     {
46       Music *body = unsmob_music (me->get_property ("element"));
47       if (body)
48         p = body->to_relative_octave (p);
49
50       Pitch last = p;
51       SCM alternatives = me->get_property ("elements");
52
53       for (SCM s = alternatives; scm_is_pair (s); s = scm_cdr (s))
54         {
55           lily_1_8_compatibility_used = true;
56           unsmob_music (scm_car (s))->to_relative_octave (p);
57         }
58
59       return last.smobbed_copy ();
60     }
61   else
62     return me->generic_to_relative_octave (p).smobbed_copy ();
63 }
64
65 Moment
66 Repeated_music::alternatives_get_length (Music *me, bool fold)
67 {
68   SCM alternative_list = alternatives (me);
69   int len = scm_ilength (alternative_list);
70   if (len <= 0)
71     return 0;
72
73   if (fold)
74     return Music_sequence::maximum_length (alternative_list);
75
76   Moment m = 0;
77   int done = 0;
78   int count = robust_scm2int (me->get_property ("repeat-count"), 0);
79
80   SCM p = alternative_list;
81   while (scm_is_pair (p) && done < count)
82     {
83       m = m + unsmob_music (scm_car (p))->get_length ();
84       done++;
85       if (count - done < len)
86         p = scm_cdr (p);
87     }
88   return m;
89 }
90
91 /*
92   Sum all duration of all available alternatives. This is for the case
93   of volta repeats, where the alternatives are iterated just as they
94   were entered.  */
95 Moment
96 Repeated_music::alternatives_volta_get_length (Music *me)
97 {
98   return Music_sequence::cumulative_length (alternatives (me));
99 }
100
101 /*
102   Length of the body in THIS. Disregards REPEAT-COUNT.
103 */
104 Moment
105 Repeated_music::body_get_length (Music *me)
106 {
107   Moment m = 0;
108   if (Music *body = unsmob_music (me->get_property ("element")))
109     m = body->get_length ();
110   return m;
111 }
112
113 MAKE_SCHEME_CALLBACK (Repeated_music, unfolded_music_length, 1);
114
115 SCM
116 Repeated_music::unfolded_music_length (SCM m)
117 {
118   Music *me = unsmob_music (m);
119
120   Moment l = Moment (repeat_count (me)) * body_get_length (me) + alternatives_get_length (me, false);
121   return l.smobbed_copy ();
122 }
123
124 MAKE_SCHEME_CALLBACK (Repeated_music, folded_music_length, 1);
125 SCM
126 Repeated_music::folded_music_length (SCM m)
127 {
128   Music *me = unsmob_music (m);
129
130   Moment l = body_get_length (me) + alternatives_get_length (me, true);
131   return l.smobbed_copy ();
132 }
133
134 int
135 Repeated_music::repeat_count (Music *me)
136 {
137   return scm_to_int (me->get_property ("repeat-count"));
138 }
139
140 MAKE_SCHEME_CALLBACK (Repeated_music, volta_music_length, 1);
141 SCM
142 Repeated_music::volta_music_length (SCM m)
143 {
144   Music *me = unsmob_music (m);
145   Moment l = body_get_length (me) + alternatives_volta_get_length (me);
146   return l.smobbed_copy ();
147 }
148
149 MAKE_SCHEME_CALLBACK (Repeated_music, minimum_start, 1);
150 SCM
151 Repeated_music::minimum_start (SCM m)
152 {
153   Music *me = unsmob_music (m);
154   Music *body = unsmob_music (me->get_property ("element"));
155
156   if (body)
157     return body->start_mom ().smobbed_copy ();
158   else
159     return Music_sequence::minimum_start (me->get_property ("elements")).smobbed_copy ();
160 }
161
162 MAKE_SCHEME_CALLBACK (Repeated_music, first_start, 1);
163 SCM
164 Repeated_music::first_start (SCM m)
165 {
166   Music *me = unsmob_music (m);
167   Music *body = unsmob_music (me->get_property ("element"));
168
169   Moment rv = (body) ? body->start_mom ()
170               : Music_sequence::first_start (me->get_property ("elements"));
171
172   return rv.smobbed_copy ();
173 }