]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeated-music.cc
550424c5d0718caedd92cf740629d6f816b2785e
[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--2001 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
15 Music *
16 Repeated_music::body ()const
17 {
18   return unsmob_music (get_mus_property ("element"));
19 }
20
21 SCM
22 Repeated_music::alternatives ()const
23 {
24   return get_mus_property ("elements");
25 }
26
27
28 Repeated_music::Repeated_music (SCM l)
29   : Music (l)
30 {
31   set_mus_property ("type", ly_symbol2scm ("repeated-music"));
32 }
33
34
35 Pitch
36 Repeated_music::to_relative_octave (Pitch p)
37 {
38   if (body ())
39     p = body ()->to_relative_octave (p);
40
41   Pitch last = p ; 
42   if (alternatives ())
43     for (SCM s = alternatives (); gh_pair_p (s);  s = ly_cdr (s))
44       unsmob_music (ly_car (s))->to_relative_octave (p);
45      
46
47   return last;
48 }
49
50 void
51 Repeated_music::transpose (Pitch p)
52 {
53   if (body ())
54     body ()->transpose (p);
55
56   Music_sequence::transpose_list (get_mus_property ("elements"), p);
57 }
58
59 void
60 Repeated_music::compress (Moment p)
61 {
62   if (body ())
63     body ()->compress (p);
64
65   Music_sequence::compress_list (alternatives (), p);
66 }
67
68 Moment
69 Repeated_music::alternatives_length_mom (bool fold) const
70 {
71   if (!alternatives ())
72     return 0;
73   
74   if (fold)
75     return Music_sequence::maximum_length (alternatives ());
76
77   Moment m =0;
78   int done =0;
79
80   SCM p = alternatives ();
81   while (gh_pair_p (p) && done < repeat_count ())
82     {
83       m = m + unsmob_music (ly_car (p))->length_mom ();
84       done ++;
85       if (repeat_count () - done < scm_ilength (alternatives ()))
86         p = ly_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_length_mom () const
97 {
98   if (!alternatives ())
99     return 0;
100
101   Moment m;
102   SCM p = alternatives ();
103   while (gh_pair_p (p))
104     {
105       m = m + unsmob_music (ly_car (p))->length_mom ();
106       p = ly_cdr (p);
107     }
108   return m;
109 }
110
111
112 /*
113   Length of the body in THIS. Disregards REPEAT-COUNT. 
114  */
115 Moment
116 Repeated_music::body_length_mom () const
117 {
118   Moment m = 0;
119   if (body ())
120     {
121       m = body ()->length_mom ();
122     }
123   return m;
124 }
125
126 int
127 Repeated_music::repeat_count () const
128 {
129   return gh_scm2int (get_mus_property ("repeat-count"));
130 }
131
132
133 MAKE_SCHEME_CALLBACK (Repeated_music,unfolded_music_length, 1);
134 MAKE_SCHEME_CALLBACK (Repeated_music,folded_music_length, 1);
135 MAKE_SCHEME_CALLBACK (Repeated_music,volta_music_length, 1);
136
137 SCM
138 Repeated_music::unfolded_music_length (SCM m)
139 {
140   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
141   
142   Moment l = Moment (r->repeat_count ()) * r->body_length_mom () + r->alternatives_length_mom (false);
143   return l.smobbed_copy ();
144 }
145
146 SCM
147 Repeated_music::folded_music_length (SCM m)
148 {
149   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
150  
151   Moment l =  r->body_length_mom () + r->alternatives_length_mom (true);
152   return l.smobbed_copy ();
153 }
154
155 SCM
156 Repeated_music::volta_music_length (SCM m)
157 {
158   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
159   Moment l =  r->body_length_mom () + r->alternatives_volta_length_mom ();
160   return l.smobbed_copy ();
161 }
162
163 ADD_MUSIC (Repeated_music);
164
165 Repeated_music::Repeated_music ()
166   : Music (SCM_EOL)
167 {
168  set_mus_property ("type", ly_symbol2scm ("repeated-music"));
169 }