]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeated-music.cc
release: 1.3.149
[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 ("body"));
19 }
20
21 Music_sequence*
22 Repeated_music::alternatives ()const
23 {
24   return dynamic_cast<Music_sequence*> (unsmob_music (get_mus_property ("alternatives")));
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 ()->music_list (); gh_pair_p (s);  s = gh_cdr (s))
44       unsmob_music (gh_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   if (alternatives ())
57     alternatives ()->transpose (p);  
58 }
59
60 void
61 Repeated_music::compress (Moment p)
62 {
63   if (body ())
64     body ()->compress (p);
65
66   if (alternatives ())
67     alternatives ()->compress (p);  
68 }
69
70 Moment
71 Repeated_music::alternatives_length_mom (bool fold) const
72 {
73   if (!alternatives ())
74     return 0;
75   
76   if (fold)
77     return alternatives ()->maximum_length ();
78
79   Moment m =0;
80   int done =0;
81
82   SCM p = alternatives ()->music_list ();
83   while (gh_pair_p (p) && done < repeat_count ())
84     {
85       m = m + unsmob_music (gh_car (p))->length_mom ();
86       done ++;
87       if (repeat_count () - done < alternatives ()->length_i ())
88         p = gh_cdr (p);
89     }
90   return m;
91 }
92
93 /*
94   Sum all duration of all available alternatives. This is for the case
95   of volta repeats, where the alternatives are iterated just as they
96   were entered.  */
97 Moment
98 Repeated_music::alternatives_volta_length_mom () const
99 {
100   if (!alternatives ())
101     return 0;
102
103   Moment m;
104   SCM p = alternatives ()->music_list ();
105   while (gh_pair_p (p))
106     {
107       m = m + unsmob_music (gh_car (p))->length_mom ();
108       p = gh_cdr (p);
109     }
110   return m;
111 }
112
113
114 /*
115   Length of the body in THIS. Disregards REPEAT-COUNT. 
116  */
117 Moment
118 Repeated_music::body_length_mom () const
119 {
120   Moment m = 0;
121   if (body ())
122     {
123       m = body ()->length_mom ();
124     }
125   return m;
126 }
127
128 int
129 Repeated_music::repeat_count () const
130 {
131   return gh_scm2int (get_mus_property ("repeat-count"));
132 }
133
134
135 MAKE_SCHEME_CALLBACK (Repeated_music,unfolded_music_length, 1);
136 MAKE_SCHEME_CALLBACK (Repeated_music,folded_music_length, 1);
137 MAKE_SCHEME_CALLBACK (Repeated_music,volta_music_length, 1);
138
139 SCM
140 Repeated_music::unfolded_music_length (SCM m)
141 {
142   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
143   
144   Moment l = Moment (r->repeat_count ()) * r->body_length_mom () + r->alternatives_length_mom (false);
145   return l.smobbed_copy ();
146 }
147
148 SCM
149 Repeated_music::folded_music_length (SCM m)
150 {
151   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
152  
153   Moment l =  r->body_length_mom () + r->alternatives_length_mom (true);
154   return l.smobbed_copy ();
155 }
156
157 SCM
158 Repeated_music::volta_music_length (SCM m)
159 {
160   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
161   Moment l =  r->body_length_mom () + r->alternatives_volta_length_mom ();
162   return l.smobbed_copy ();
163 }
164
165 ADD_MUSIC (Repeated_music);
166
167 Repeated_music::Repeated_music ()
168   : Music (SCM_EOL)
169 {
170  set_mus_property ("type", ly_symbol2scm ("repeated-music"));
171 }