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