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