]> git.donarmstrong.com Git - lilypond.git/blob - lily/repeated-music.cc
6436be9bfa7adaea4d34997cece84660e3866b0e
[lilypond.git] / lily / repeated-music.cc
1 /*   
2   new-repeated-music.cc --  implement Repeated_music
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "new-repeated-music.hh"
11 #include "music-list.hh"
12 #include "musical-pitch.hh"
13 #include "debug.hh"
14
15 Repeated_music::Repeated_music(Music *beg, int times, Music_sequence * alts)
16 {
17   repeat_body_p_ = beg;
18   fold_b_ = false;
19   repeats_i_ = times;
20   alternatives_p_ = alts;
21   semi_fold_b_ = true;
22   if (alts)
23     alts->music_p_list_p_->truncate (times);
24 }
25
26 Repeated_music::Repeated_music (Repeated_music const &s)
27   : Music (s)
28 {
29   repeats_i_ = s.repeats_i_;
30   fold_b_ = s.fold_b_;
31   semi_fold_b_ = s.semi_fold_b_;
32   
33   repeat_body_p_ = s.repeat_body_p_ ? s.repeat_body_p_->clone () : 0;
34   alternatives_p_ = s.alternatives_p_
35     ? dynamic_cast<Music_sequence*> (s.alternatives_p_->clone ()):0;
36 }
37
38 Repeated_music::~Repeated_music ()
39 {
40   delete repeat_body_p_;
41   delete alternatives_p_;
42 }
43
44 void
45 Repeated_music::do_print () const
46 {
47 #ifndef NPRINT
48   DOUT << "Fold = " << fold_b_ << " reps: " << repeats_i_;
49
50   if (repeat_body_p_)
51     repeat_body_p_->print();
52   
53   if (alternatives_p_)
54     alternatives_p_->print();
55 #endif
56 }
57
58 Musical_pitch
59 Repeated_music::to_relative_octave (Musical_pitch p)
60 {
61   if (repeat_body_p_)
62     p = repeat_body_p_->to_relative_octave (p);
63
64   if (alternatives_p_)
65     p = alternatives_p_->do_relative_octave (p, true);
66   return p;
67 }
68
69 Music_iterator*
70 Repeated_music::to_rhythm (Music_iterator* r)
71 {
72   if (repeat_body_p_)
73     r = repeat_body_p_->to_rhythm (r);
74
75   if (alternatives_p_)
76     r = alternatives_p_->do_rhythm (r);
77   return r;
78 }
79
80
81 void
82 Repeated_music::transpose (Musical_pitch p)
83 {
84   if (repeat_body_p_)
85     repeat_body_p_->transpose (p);
86
87   if (alternatives_p_)
88     alternatives_p_->transpose (p);  
89 }
90
91 void
92 Repeated_music::compress (Moment p)
93 {
94   if (repeat_body_p_)
95     repeat_body_p_->compress (p);
96
97   if (alternatives_p_)
98     alternatives_p_->compress (p);  
99 }
100
101 Moment
102 Repeated_music::alternatives_length_mom () const
103 {
104   if (!alternatives_p_ )
105     return 0;
106   
107   if  (fold_b_)
108     alternatives_p_->maximum_length ();
109
110   Moment m =0;
111   int done =0;
112   Cons<Music> *p = alternatives_p_->music_p_list_p_->head_;
113   while (p && done < repeats_i_)
114     {
115       m = m + p->car_->length_mom ();
116       done ++;
117       if (repeats_i_ - done < alternatives_p_->length_i ())
118         p = p->next_;
119     }
120   return m;
121 }
122
123 Moment
124 Repeated_music::length_mom () const
125 {
126   Moment m =0;
127   if (fold_b_)
128     {
129       if (repeat_body_p_)
130         m += repeat_body_p_->length_mom ();
131     }
132   else
133     {
134       Moment beg = (repeat_body_p_) ? repeat_body_p_->length_mom () : Rational(0);
135       if (!semi_fold_b_)
136         beg *=  Rational (repeats_i_);
137       m += beg;
138     }
139
140   m += alternatives_length_mom ();
141   return m;
142 }
143