]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeated-music.cc
release: 1.3.83
[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 "musical-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   fold_b_ = false;
35   repeats_i_ = times;
36   volta_fold_b_ = true;
37   if (alts)
38     {
39       alts->truncate (times);
40       set_mus_property ("alternatives", alts->self_scm ());
41       scm_unprotect_object (alts->self_scm ());  
42     }
43 }
44
45 Repeated_music::Repeated_music (Repeated_music const &s)
46   : Music (s)
47 {
48   repeats_i_ = s.repeats_i_;
49   fold_b_ = s.fold_b_;
50   volta_fold_b_ = s.volta_fold_b_;
51   type_ = s.type_;
52 }
53
54
55 Musical_pitch
56 Repeated_music::to_relative_octave (Musical_pitch p)
57 {
58   if (body ())
59     p = body ()->to_relative_octave (p);
60
61   Musical_pitch last = p ; 
62   if (alternatives ())
63     for (SCM s = alternatives ()->music_list (); gh_pair_p (s);  s = gh_cdr (s))
64       unsmob_music (gh_car (s))->to_relative_octave (p);
65      
66
67   return last;
68 }
69
70 void
71 Repeated_music::transpose (Musical_pitch p)
72 {
73   if (body ())
74     body ()->transpose (p);
75
76   if (alternatives ())
77     alternatives ()->transpose (p);  
78 }
79
80 void
81 Repeated_music::compress (Moment p)
82 {
83   if (body ())
84     body ()->compress (p);
85
86   if (alternatives ())
87     alternatives ()->compress (p);  
88 }
89
90 Moment
91 Repeated_music::alternatives_length_mom () const
92 {
93   if (!alternatives () )
94     return 0;
95   
96   if  (fold_b_)
97     return alternatives ()->maximum_length ();
98
99   Moment m =0;
100   int done =0;
101
102   SCM p = alternatives ()->music_list ();
103    while (gh_pair_p (p) && done < repeats_i_)
104     {
105       m = m + unsmob_music (gh_car (p))->length_mom ();
106       done ++;
107       if (volta_fold_b_
108           || repeats_i_ - done < alternatives ()->length_i ())
109       p = gh_cdr (p);
110     }
111   return m;
112 }
113
114 Moment
115 Repeated_music::body_length_mom () const
116 {
117   Moment m = 0;
118   if (body ())
119     {
120       m = body ()->length_mom ();
121       if (!fold_b_ && !volta_fold_b_)
122         m *= Rational (repeats_i_);
123     }
124   return m;
125 }
126
127 Moment
128 Repeated_music::length_mom () const
129 {
130   return body_length_mom () + alternatives_length_mom ();
131 }
132