lilypond-1.5.1
[lilypond.git] / lily / music-sequence.cc
blob35c8bb16b3174c6e515058c38615a6dff005151f
1 /*
2 music-sequence.cc -- implement Music_sequence
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9 #include "music-list.hh"
10 #include "debug.hh"
11 #include "pitch.hh"
14 void
15 Music_sequence::truncate (int k)
17 SCM l = get_mus_property ("elements");
18 if (k == 0)
20 l = SCM_EOL;
22 else
24 SCM s = l;
25 k--;
26 for (; gh_pair_p (s) && k--; s = gh_cdr (s))
29 if (gh_pair_p (s))
31 gh_set_cdr_x (s, SCM_EOL);
34 set_mus_property ("elements", l);
37 SCM
38 Music_sequence::music_list ()const
40 return get_mus_property ("elements");
44 Ugh this sucks. Linear. do not use.
46 void
47 Music_sequence::append_music (Music *m)
49 set_mus_property ("elements",
50 gh_append2 (music_list (), gh_cons (m->self_scm (), SCM_EOL)));
51 scm_unprotect_object (m->self_scm ());
54 Music_sequence::Music_sequence (SCM l)
55 : Music (l)
59 void
60 Music_sequence::transpose (Pitch rq)
62 for (SCM s = music_list (); gh_pair_p (s); s = gh_cdr (s))
63 unsmob_music (gh_car (s))->transpose (rq);
69 Moment
70 Music_sequence::cumulative_length () const
72 Moment cumulative;
74 Moment last_len ;
75 for (SCM s = music_list (); gh_pair_p (s); s = gh_cdr (s))
77 Moment l = unsmob_music (gh_car (s))->length_mom ();
78 if (last_len.grace_mom_ && l.main_part_)
80 last_len.grace_mom_ = Rational (0);
82 cumulative += last_len;
83 last_len = l;
86 last_len.grace_mom_ = Rational (0);
87 cumulative += last_len;
89 return cumulative;
92 Pitch
93 Music_sequence::to_relative_octave (Pitch p)
95 return do_relative_octave (p, false);
99 Moment
100 Music_sequence::maximum_length () const
102 Moment dur = 0;
103 for (SCM s = music_list (); gh_pair_p (s); s = gh_cdr (s))
104 dur = dur >? unsmob_music (gh_car (s))->length_mom ();
106 return dur;
109 Music_sequence::length_i () const
111 return scm_ilength (music_list ());
114 Pitch
115 Music_sequence::do_relative_octave (Pitch p, bool ret_first)
117 Pitch retval;
118 int count=0;
120 Pitch last = p;
121 for (SCM s = music_list (); gh_pair_p (s); s = gh_cdr (s))
123 last = unsmob_music (gh_car (s))->to_relative_octave (last);
124 if (!count ++)
125 retval = last;
128 if (!ret_first)
129 retval = last;
131 return retval;
134 void
135 Music_sequence::compress (Moment m)
137 for (SCM s = music_list (); gh_pair_p (s); s = gh_cdr (s))
138 unsmob_music (gh_car (s))->compress (m);
141 ADD_MUSIC (Music_sequence);
143 Music_sequence::Music_sequence ()
144 : Music (SCM_EOL)
149 Moment
150 Music_sequence::minimum_start () const
152 Moment m;
154 for (SCM s = music_list (); gh_pair_p (s); s = gh_cdr (s))
156 m = m <? unsmob_music (gh_car (s))->start_mom ();
158 return m;
161 Moment
162 Music_sequence::first_start () const
164 Moment m;
166 for (SCM s = music_list (); gh_pair_p (s); s = gh_cdr (s))
168 Music * mus = unsmob_music (gh_car (s));
169 Moment l = mus->length_mom ();
171 if (l.main_part_)
172 return mus->start_mom ();
173 else if (l.grace_mom_)
175 m.grace_mom_ = - l.grace_mom_;
176 return m;
179 return m;