]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeated-music.cc
28b2c0aca6ef56ec39510c83d2dfa25a27cc20db
[lilypond.git] / lily / repeated-music.cc
1 /*   
2   repeated-music.cc --  implement Repeated_music
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "repeated-music.hh"
11 #include "music-list.hh"
12 #include "pitch.hh"
13 #include "warn.hh"
14 #include "music-sequence.hh"
15
16 Music *
17 Repeated_music::body ()const
18 {
19   return unsmob_music (get_mus_property ("element"));
20 }
21
22 SCM
23 Repeated_music::alternatives ()const
24 {
25   return get_mus_property ("elements");
26 }
27
28
29
30
31 Pitch
32 Repeated_music::to_relative_octave (Pitch p)
33 {
34   if (body ())
35     p = body ()->to_relative_octave (p);
36
37   Pitch last = p ; 
38   if (alternatives ())
39     for (SCM s = alternatives (); gh_pair_p (s);  s = ly_cdr (s))
40       unsmob_music (ly_car (s))->to_relative_octave (p);
41      
42
43   return last;
44 }
45
46 void
47 Repeated_music::transpose (Pitch p)
48 {
49   if (body ())
50     body ()->transpose (p);
51
52   Music_sequence::transpose_list (get_mus_property ("elements"), p);
53 }
54
55 void
56 Repeated_music::compress (Moment p)
57 {
58   if (body ())
59     body ()->compress (p);
60
61   Music_sequence::compress_list (alternatives (), p);
62 }
63
64 Moment
65 Repeated_music::alternatives_get_length (bool fold) const
66 {
67   if (!alternatives ())
68     return 0;
69   
70   if (fold)
71     return Music_sequence::maximum_length (alternatives ());
72
73   Moment m =0;
74   int done =0;
75
76   SCM p = alternatives ();
77   while (gh_pair_p (p) && done < repeat_count ())
78     {
79       m = m + unsmob_music (ly_car (p))->get_length ();
80       done ++;
81       if (repeat_count () - done < scm_ilength (alternatives ()))
82         p = ly_cdr (p);
83     }
84   return m;
85 }
86
87 /*
88   Sum all duration of all available alternatives. This is for the case
89   of volta repeats, where the alternatives are iterated just as they
90   were entered.  */
91 Moment
92 Repeated_music::alternatives_volta_get_length () const
93 {
94   if (!alternatives ())
95     return 0;
96
97   Moment m;
98   SCM p = alternatives ();
99   while (gh_pair_p (p))
100     {
101       m = m + unsmob_music (ly_car (p))->get_length ();
102       p = ly_cdr (p);
103     }
104   return m;
105 }
106
107
108 /*
109   Length of the body in THIS. Disregards REPEAT-COUNT. 
110  */
111 Moment
112 Repeated_music::body_get_length () const
113 {
114   Moment m = 0;
115   if (body ())
116     {
117       m = body ()->get_length ();
118     }
119   return m;
120 }
121
122 int
123 Repeated_music::repeat_count () const
124 {
125   return gh_scm2int (get_mus_property ("repeat-count"));
126 }
127
128
129 MAKE_SCHEME_CALLBACK (Repeated_music,unfolded_music_length, 1);
130 MAKE_SCHEME_CALLBACK (Repeated_music,folded_music_length, 1);
131 MAKE_SCHEME_CALLBACK (Repeated_music,volta_music_length, 1);
132
133 SCM
134 Repeated_music::unfolded_music_length (SCM m)
135 {
136   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
137   
138   Moment l = Moment (r->repeat_count ()) * r->body_get_length () + r->alternatives_get_length (false);
139   return l.smobbed_copy ();
140 }
141
142 SCM
143 Repeated_music::folded_music_length (SCM m)
144 {
145   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
146  
147   Moment l =  r->body_get_length () + r->alternatives_get_length (true);
148   return l.smobbed_copy ();
149 }
150
151 SCM
152 Repeated_music::volta_music_length (SCM m)
153 {
154   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
155   Moment l =  r->body_get_length () + r->alternatives_volta_get_length ();
156   return l.smobbed_copy ();
157 }
158
159 ADD_MUSIC (Repeated_music);
160
161 Repeated_music::Repeated_music ()
162   : Music ()
163 {
164 }
165
166
167 MAKE_SCHEME_CALLBACK (Repeated_music,minimum_start, 1);
168 MAKE_SCHEME_CALLBACK (Repeated_music,first_start, 1);
169
170 SCM
171 Repeated_music::minimum_start (SCM m)
172 {
173   Music * me = unsmob_music (m);
174   Music * body = unsmob_music (me->get_mus_property ("element"));
175
176   if (body)
177     return body->start_mom ().smobbed_copy();
178   else
179     {
180       return Music_sequence::minimum_start (me->get_mus_property ("elements")).smobbed_copy();
181     }
182 }
183
184 SCM
185 Repeated_music::first_start (SCM m)
186 {
187   Music * me = unsmob_music (m);
188   Music * body = unsmob_music (me->get_mus_property ("element"));
189
190   Moment rv =  (body) ? body->start_mom () :
191     Music_sequence::first_start (me->get_mus_property ("elements"));
192
193   return rv.smobbed_copy ();
194 }