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