]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeated-music.cc
665b00e1c6cc1b6b76ecd45341aada4082201555
[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 Repeated_music::Repeated_music(SCM l)
28   : Music (l)
29 {
30   set_mus_property ("type", ly_symbol2scm ("repeated-music"));
31 }
32
33
34 Pitch
35 Repeated_music::to_relative_octave (Pitch p)
36 {
37   if (body ())
38     p = body ()->to_relative_octave (p);
39
40   Pitch last = p ; 
41   if (alternatives ())
42     for (SCM s = alternatives ()->music_list (); gh_pair_p (s);  s = gh_cdr (s))
43       unsmob_music (gh_car (s))->to_relative_octave (p);
44      
45
46   return last;
47 }
48
49 void
50 Repeated_music::transpose (Pitch p)
51 {
52   if (body ())
53     body ()->transpose (p);
54
55   if (alternatives ())
56     alternatives ()->transpose (p);  
57 }
58
59 void
60 Repeated_music::compress (Moment p)
61 {
62   if (body ())
63     body ()->compress (p);
64
65   if (alternatives ())
66     alternatives ()->compress (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 alternatives ()->maximum_length ();
77
78   Moment m =0;
79   int done =0;
80
81   SCM p = alternatives ()->music_list ();
82   while (gh_pair_p (p) && done < repeat_count ())
83     {
84       m = m + unsmob_music (gh_car (p))->length_mom ();
85       done ++;
86       if (repeat_count () - done < alternatives ()->length_i ())
87         p = gh_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 ()->music_list ();
104   while (gh_pair_p (p))
105     {
106       m = m + unsmob_music (gh_car (p))->length_mom ();
107       p = gh_cdr (p);
108     }
109   return m;
110 }
111
112 Moment
113 Repeated_music::body_length_mom () const
114 {
115   Moment m = 0;
116   if (body ())
117     {
118       m = body ()->length_mom ();
119     }
120   return m;
121 }
122
123 int
124 Repeated_music::repeat_count () const
125 {
126   return gh_scm2int (get_mus_property ("repeat-count"));
127 }
128
129
130 MAKE_SCHEME_CALLBACK(Repeated_music,unfolded_music_length, 1);
131 MAKE_SCHEME_CALLBACK(Repeated_music,folded_music_length, 1);
132 MAKE_SCHEME_CALLBACK(Repeated_music,volta_music_length, 1);
133
134 SCM
135 Repeated_music::unfolded_music_length (SCM m)
136 {
137   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
138   
139   Moment l = Moment (r->repeat_count ()) * r->body_length_mom () + r->alternatives_length_mom (false);
140   return l.smobbed_copy ();
141 }
142
143 SCM
144 Repeated_music::folded_music_length (SCM m)
145 {
146   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
147  
148   Moment l =  r->body_length_mom () + r->alternatives_length_mom (true);
149   return l.smobbed_copy ();
150 }
151
152 SCM
153 Repeated_music::volta_music_length (SCM m)
154 {
155   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
156   Moment l =  r->body_length_mom () + r->alternatives_volta_length_mom ();
157   return l.smobbed_copy ();
158 }