]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeated-music.cc
32e8202e81b883ed0963bb45c292722fa9022e82
[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--2000 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   SCM p = alternatives ()->music_list ();
100   Moment m;
101   while (gh_pair_p (p))
102     {
103       m = m + unsmob_music (gh_car (p))->length_mom ();
104       p = gh_cdr (p);
105     }
106   return m;
107 }
108
109 Moment
110 Repeated_music::body_length_mom () const
111 {
112   Moment m = 0;
113   if (body ())
114     {
115       m = body ()->length_mom ();
116     }
117   return m;
118 }
119
120 int
121 Repeated_music::repeat_count () const
122 {
123   return gh_scm2int (get_mus_property ("repeat-count"));
124 }
125
126
127 MAKE_SCHEME_CALLBACK(Repeated_music,unfolded_music_length, 1);
128 MAKE_SCHEME_CALLBACK(Repeated_music,folded_music_length, 1);
129 MAKE_SCHEME_CALLBACK(Repeated_music,volta_music_length, 1);
130
131 SCM
132 Repeated_music::unfolded_music_length (SCM m)
133 {
134   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
135   
136   Moment l = Moment (r->repeat_count ()) * r->body_length_mom () + r->alternatives_length_mom (false);
137   return l.smobbed_copy ();
138 }
139
140 SCM
141 Repeated_music::folded_music_length (SCM m)
142 {
143   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
144  
145   Moment l =  r->body_length_mom () + r->alternatives_length_mom (true);
146   return l.smobbed_copy ();
147 }
148
149 SCM
150 Repeated_music::volta_music_length (SCM m)
151 {
152   Repeated_music* r = dynamic_cast<Repeated_music*> (unsmob_music (m));
153   Moment l =  r->body_length_mom () + r->alternatives_volta_length_mom ();
154   return l.smobbed_copy ();
155 }