lilypond-1.3.16
[lilypond.git] / lily / repeated-music.cc
blobf7cfefac53022388397f8d5d04ab4cfa55d0dfe0
1 /*
2 repeated-music.cc -- implement Repeated_music
4 source file of the GNU LilyPond music typesetter
6 (c) 1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "repeated-music.hh"
11 #include "music-list.hh"
12 #include "musical-pitch.hh"
13 #include "debug.hh"
15 Repeated_music::Repeated_music(Music *beg, int times, Music_sequence * alts)
17 repeat_body_p_ = beg;
18 fold_b_ = false;
19 repeats_i_ = times;
20 alternatives_p_ = alts;
21 volta_fold_b_ = true;
22 if (alts)
23 alts->music_p_list_p_->truncate (times);
26 Repeated_music::Repeated_music (Repeated_music const &s)
27 : Music (s)
29 repeats_i_ = s.repeats_i_;
30 fold_b_ = s.fold_b_;
31 volta_fold_b_ = s.volta_fold_b_;
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;
38 Repeated_music::~Repeated_music ()
40 delete repeat_body_p_;
41 delete alternatives_p_;
44 void
45 Repeated_music::do_print () const
47 #ifndef NPRINT
48 DEBUG_OUT << "Fold = " << fold_b_ << " reps: " << repeats_i_;
50 if (repeat_body_p_)
51 repeat_body_p_->print();
53 if (alternatives_p_)
54 alternatives_p_->print();
55 #endif
58 Musical_pitch
59 Repeated_music::to_relative_octave (Musical_pitch p)
61 if (repeat_body_p_)
62 p = repeat_body_p_->to_relative_octave (p);
64 if (alternatives_p_)
65 p = alternatives_p_->do_relative_octave (p, false);
66 return p;
71 void
72 Repeated_music::transpose (Musical_pitch p)
74 if (repeat_body_p_)
75 repeat_body_p_->transpose (p);
77 if (alternatives_p_)
78 alternatives_p_->transpose (p);
81 void
82 Repeated_music::compress (Moment p)
84 if (repeat_body_p_)
85 repeat_body_p_->compress (p);
87 if (alternatives_p_)
88 alternatives_p_->compress (p);
91 Moment
92 Repeated_music::alternatives_length_mom () const
94 if (!alternatives_p_ )
95 return 0;
97 if (fold_b_)
98 return alternatives_p_->maximum_length ();
100 Moment m =0;
101 int done =0;
102 Cons<Music> *p = alternatives_p_->music_p_list_p_->head_;
103 while (p && done < repeats_i_)
105 m = m + p->car_->length_mom ();
106 done ++;
107 if (volta_fold_b_
108 || repeats_i_ - done < alternatives_p_->length_i ())
109 p = p->next_;
111 return m;
114 Moment
115 Repeated_music::length_mom () const
117 Moment m =0;
118 if (fold_b_)
120 if (repeat_body_p_)
121 m += repeat_body_p_->length_mom ();
123 else
125 Moment beg = (repeat_body_p_) ? repeat_body_p_->length_mom () : Rational(0);
126 if (!volta_fold_b_)
127 beg *= Rational (repeats_i_);
128 m += beg;
131 m += alternatives_length_mom ();
132 return m;