lilypond-1.1.5
[lilypond.git] / src / score.cc
blob0dd16ecbf729cb28f82a248b6d91e3141f245899
1 /*
2 score.cc -- implement Score
4 source file of the LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8 #include "tex-stream.hh"
9 #include "score.hh"
10 #include "score-column.hh"
11 #include "pscore.hh"
12 #include "staff.hh"
13 #include "debug.hh"
14 #include "paper-def.hh"
15 #include "main.hh"
16 #include "source.hh"
17 #include "source-file.hh"
18 #include "score-walker.hh"
19 #include "midi-output.hh"
20 #include "midi-def.hh"
22 extern String default_out_fn;
24 void
25 Score::setup_music()
27 *mlog << "\nSetting up music ..." << flush;
28 if (last() == Moment(0)) {
29 errorlevel_i_ |= 1;
30 error("Need to have music in a score.", defined_ch_c_l_);
33 for (iter_top(staffs_,i); i.ok(); i++) {
34 i->setup_staffcols();
35 i->OK();
39 void
40 Score::process_music()
42 *mlog << "Processing music ..." << flush;
43 for (Score_walker w(this); w.ok(); w++) {
44 w.process();
48 void
49 Score::process()
51 setup_music();
53 paper();
54 midi();
57 void
58 Score::paper()
60 if (!paper_p_)
61 return;
63 pscore_p_ = new PScore(paper_p_);
65 find_col(0, false)->set_breakable(); // ugh
66 find_col(last(), false)->set_breakable();
67 do_cols();
69 for (iter_top(staffs_,i); i.ok(); i++)
70 i->set_output(pscore_p_);
73 process_music();
74 clean_cols(); // can't move clean_cols() farther up.
75 print();
76 calc_idealspacing();
78 // debugging
79 OK();
80 *mlog << endl;
81 pscore_p_->process();
83 // output
84 paper_output();
88 /**
89 Remove empty cols, preprocess other columns.
91 void
92 Score::clean_cols()
94 for (iter_top(staffs_,i); i.ok(); i++)
95 i->clean_cols();
97 for (iter_top(cols_,c); c.ok(); ) {
98 if (!c->pcol_l_->used_b()) {
99 delete c.get_p();
100 } else {
101 c->preprocess();
102 c++;
108 Create columns at time #w#.
109 this sux. We should have Score_column create the appropriate PCol.
110 Unfortunately, PCols don't know about their position.
112 @return cursor pointing to the nonmusical (first) column
114 PCursor<Score_column*>
115 Score::create_cols(Moment w)
117 Score_column* c1 = new Score_column(w);
118 Score_column* c2 = new Score_column(w);
120 c1->musical_b_ = false;
121 c2->musical_b_ = true;
123 iter_top(cols_,i);
125 for (; i.ok(); i++) {
126 assert(i->when() != w);
127 if (i->when() > w)
128 break;
131 if (!i.ok()) {
132 cols_.bottom().add(c1);
133 cols_.bottom().add(c2);
134 i = cols_.bottom();
135 i --;
136 } else {
137 i.insert(c1);
138 i.insert(c2);
139 i -= 2;
141 return i;
144 PCursor<Score_column*>
145 Score::find_col(Moment w, bool mus)
147 iter_top( cols_,i);
149 for (; i.ok(); i++) {
150 if (i->when() == w && i->musical_b_ == mus)
151 return i;
152 if (i->when() > w)
153 break;
155 i = create_cols(w);
156 if (mus)
157 i++;
158 return i;
161 void
162 Score::do_cols()
164 iter_top(cols_,i);
165 for (; i.ok(); i++) {
166 pscore_p_->add(i->pcol_l_);
170 Moment
171 Score::last() const
173 Moment l = 0;
174 for (iter_top(staffs_,i); i.ok(); i++) {
175 l = l>? i->last();
177 return l;
180 void
181 Score::set(Paper_def *pap_p)
183 delete paper_p_;
184 paper_p_ = pap_p;
187 void
188 Score::set(Midi_def* midi_p)
190 delete midi_p_;
191 midi_p_ = midi_p;
194 void
195 Score::OK() const
197 #ifndef NDEBUG
198 for (iter_top(staffs_,i); i.ok(); i++) {
199 i->OK();
200 assert(i->score_l_ == this);
202 staffs_.OK();
203 cols_.OK();
204 for (iter_top(cols_,cc); cc.ok() && (cc+1).ok(); cc++) {
205 assert(cc->when() <= (cc+1)->when());
207 #endif
211 void
212 Score::print() const
214 #ifndef NPRINT
215 mtor << "score {\n";
216 for (iter_top(staffs_,i); i.ok(); i++) {
217 i->print();
219 for (iter_top(cols_,i); i.ok(); i++) {
220 i->print();
222 if (pscore_p_)
223 pscore_p_->print();
224 if (midi_p_)
225 midi_p_->print();
227 mtor << "}\n";
228 #endif
231 Score::Score()
233 pscore_p_=0;
234 paper_p_ = 0;
235 midi_p_ = 0;
236 errorlevel_i_ = 0;
237 defined_ch_c_l_ = 0;
240 Score::~Score()
242 delete pscore_p_;
243 delete paper_p_;
244 delete midi_p_;
247 void
248 Score::paper_output()
250 OK();
251 if (paper_p_->outfile=="")
252 paper_p_->outfile = default_out_fn + ".out";
254 if ( errorlevel_i_ ) {
255 *mlog << "lilypond: warning: no output to: " << paper_p_->outfile
256 << " (errorlevel=" << errorlevel_i_ << ")" << endl;
257 return;
260 *mlog << "TeX output to " << paper_p_->outfile << " ...\n";
262 Tex_stream the_output(paper_p_->outfile);
264 the_output << "% outputting Score, defined at: " <<
265 source_l_g->
266 sourcefile_l (defined_ch_c_l_)->file_line_no_str(defined_ch_c_l_) << "\n";
267 pscore_p_->output(the_output);
270 void
271 Score::midi()
273 if (!midi_p_)
274 return;
276 if (midi_p_->outfile_str_ == "")
277 midi_p_->outfile_str_ = default_out_fn + ".midi";
279 *mlog << "midi output to " << midi_p_->outfile_str_ << " ...\n";
280 Midi_output(this, midi_p_);
283 void
284 Score::add(Staff*s)
286 s->score_l_ = this;
287 staffs_.bottom().add(s);