]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeated-music.cc
release: 1.3.108
[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(Music *beg, int times, Music_sequence * alts)
28 {
29   if (beg)
30     {
31       set_mus_property ("body", beg->self_scm ());
32       scm_unprotect_object (beg->self_scm ());
33     }
34   set_mus_property ("repeat-count", gh_int2scm (times));
35
36   if (alts)
37     {
38       alts->truncate (times);
39       set_mus_property ("alternatives", alts->self_scm ());
40       scm_unprotect_object (alts->self_scm ());  
41     }
42   set_mus_property ("type", ly_symbol2scm ("repeated-music"));
43 }
44
45 Repeated_music::Repeated_music (Repeated_music const &s)
46   : Music (s)
47 {
48 }
49
50
51 Pitch
52 Repeated_music::to_relative_octave (Pitch p)
53 {
54   if (body ())
55     p = body ()->to_relative_octave (p);
56
57   Pitch last = p ; 
58   if (alternatives ())
59     for (SCM s = alternatives ()->music_list (); gh_pair_p (s);  s = gh_cdr (s))
60       unsmob_music (gh_car (s))->to_relative_octave (p);
61      
62
63   return last;
64 }
65
66 void
67 Repeated_music::transpose (Pitch p)
68 {
69   if (body ())
70     body ()->transpose (p);
71
72   if (alternatives ())
73     alternatives ()->transpose (p);  
74 }
75
76 void
77 Repeated_music::compress (Moment p)
78 {
79   if (body ())
80     body ()->compress (p);
81
82   if (alternatives ())
83     alternatives ()->compress (p);  
84 }
85
86 Moment
87 Repeated_music::alternatives_length_mom (bool fold) const
88 {
89   if (!alternatives () )
90     return 0;
91   
92   if  (fold)
93     return alternatives ()->maximum_length ();
94
95   Moment m =0;
96   int done =0;
97
98   SCM p = alternatives ()->music_list ();
99   while (gh_pair_p (p) && done < repeat_count ())
100     {
101       m = m + unsmob_music (gh_car (p))->length_mom ();
102       done ++;
103       if (repeat_count () - done < alternatives ()->length_i ())
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_length_mom (false);
154   return l.smobbed_copy ();
155 }