]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeated-music.cc
release: 1.5.46
[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 "debug.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 Repeated_music::Repeated_music (SCM l)
30   : Music (l)
31 {
32   set_mus_property ("type", ly_symbol2scm ("repeated-music"));
33 }
34
35
36 Pitch
37 Repeated_music::to_relative_octave (Pitch p)
38 {
39   if (body ())
40     p = body ()->to_relative_octave (p);
41
42   Pitch last = p ; 
43   if (alternatives ())
44     for (SCM s = alternatives (); gh_pair_p (s);  s = ly_cdr (s))
45       unsmob_music (ly_car (s))->to_relative_octave (p);
46      
47
48   return last;
49 }
50
51 void
52 Repeated_music::transpose (Pitch p)
53 {
54   if (body ())
55     body ()->transpose (p);
56
57   Music_sequence::transpose_list (get_mus_property ("elements"), p);
58 }
59
60 void
61 Repeated_music::compress (Moment p)
62 {
63   if (body ())
64     body ()->compress (p);
65
66   Music_sequence::compress_list (alternatives (), p);
67 }
68
69 Moment
70 Repeated_music::alternatives_length_mom (bool fold) const
71 {
72   if (!alternatives ())
73     return 0;
74   
75   if (fold)
76     return Music_sequence::maximum_length (alternatives ());
77
78   Moment m =0;
79   int done =0;
80
81   SCM p = alternatives ();
82   while (gh_pair_p (p) && done < repeat_count ())
83     {
84       m = m + unsmob_music (ly_car (p))->length_mom ();
85       done ++;
86       if (repeat_count () - done < scm_ilength (alternatives ()))
87         p = ly_cdr (p);
88     }
89   return m;
90 }
91
92 /*
93   Sum all duration of all available alternatives. This is for the case
94   of volta repeats, where the alternatives are iterated just as they
95   were entered.  */
96 Moment
97 Repeated_music::alternatives_volta_length_mom () const
98 {
99   if (!alternatives ())
100     return 0;
101
102   Moment m;
103   SCM p = alternatives ();
104   while (gh_pair_p (p))
105     {
106       m = m + unsmob_music (ly_car (p))->length_mom ();
107       p = ly_cdr (p);
108     }
109   return m;
110 }
111
112
113 /*
114   Length of the body in THIS. Disregards REPEAT-COUNT. 
115  */
116 Moment
117 Repeated_music::body_length_mom () const
118 {
119   Moment m = 0;
120   if (body ())
121     {
122       m = body ()->length_mom ();
123     }
124   return m;
125 }
126
127 int
128 Repeated_music::repeat_count () const
129 {
130   return gh_scm2int (get_mus_property ("repeat-count"));
131 }
132
133
134 MAKE_SCHEME_CALLBACK (Repeated_music,unfolded_music_length, 1);
135 MAKE_SCHEME_CALLBACK (Repeated_music,folded_music_length, 1);
136 MAKE_SCHEME_CALLBACK (Repeated_music,volta_music_length, 1);
137
138 SCM
139 Repeated_music::unfolded_music_length (SCM m)
140 {
141   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
142   
143   Moment l = Moment (r->repeat_count ()) * r->body_length_mom () + r->alternatives_length_mom (false);
144   return l.smobbed_copy ();
145 }
146
147 SCM
148 Repeated_music::folded_music_length (SCM m)
149 {
150   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
151  
152   Moment l =  r->body_length_mom () + r->alternatives_length_mom (true);
153   return l.smobbed_copy ();
154 }
155
156 SCM
157 Repeated_music::volta_music_length (SCM m)
158 {
159   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
160   Moment l =  r->body_length_mom () + r->alternatives_volta_length_mom ();
161   return l.smobbed_copy ();
162 }
163
164 ADD_MUSIC (Repeated_music);
165
166 Repeated_music::Repeated_music ()
167   : Music (SCM_EOL)
168 {
169  set_mus_property ("type", ly_symbol2scm ("repeated-music"));
170 }
171
172
173 MAKE_SCHEME_CALLBACK (Repeated_music,minimum_start, 1);
174 MAKE_SCHEME_CALLBACK (Repeated_music,first_start, 1);
175
176 SCM
177 Repeated_music::minimum_start (SCM m)
178 {
179   Music * me = unsmob_music (m);
180   Music * body = unsmob_music (me->get_mus_property ("element"));
181
182   if (body)
183     return body->start_mom ().smobbed_copy();
184   else
185     {
186       return Music_sequence::minimum_start (me->get_mus_property ("elements")).smobbed_copy();
187     }
188 }
189
190 SCM
191 Repeated_music::first_start (SCM m)
192 {
193   Music * me = unsmob_music (m);
194   Music * body = unsmob_music (me->get_mus_property ("element"));
195
196   Moment rv =  (body) ? body->start_mom () :
197     Music_sequence::first_start (me->get_mus_property ("elements"));
198
199   return rv.smobbed_copy ();
200 }