]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
release: 0.0.64
[lilypond.git] / lily / score.cc
1 /*
2   score.cc -- implement Score
3
4   source file of the LilyPond music typesetter
5
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 "p-score.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"
21 #include "pulk-voices.hh"
22 #include "request-column.hh"
23
24 extern String default_out_fn;
25
26 void
27 Score::setup_music()
28 {
29     *mlog << "\nSetting up requests..." << flush;
30     
31     Pulk_voices pulk(staffs_); 
32
33     Moment l_mom = pulk.last_;
34     if (l_mom == Moment(0)) {
35         errorlevel_i_ |= 1;
36         input_.error("Need to have music in a score.");
37     }
38     
39     Moment previous_mom = -1;
40     while (pulk.ok()) {
41
42         Moment w= pulk.next_mom();
43         assert(w > previous_mom);
44         Request_column* rcol_p = new Request_column( staffs_ );
45
46         Score_column* c1 = new Score_column(w);
47         Score_column* c2 = new Score_column(w);
48         if (w == Moment(0) || w == l_mom) {
49             c1->set_breakable();
50         }
51                 
52         c1->musical_b_ = false;
53         c2->musical_b_ = true;
54         
55         cols_.bottom().add(c1);
56         cols_.bottom().add(c2);
57         rcol_p->set_score_cols(c1, c2);
58         rcols_.bottom().add(rcol_p);
59         pulk.get_aligned_request( rcol_p );
60         previous_mom =w;
61     }
62
63     errorlevel_i_ |= pulk.time_checks_failed_b(); 
64 }
65
66 void
67 Score::process_music()
68 {
69     *mlog << "Processing requests ..." << flush;
70     for (Score_walker w(this); w.ok(); w++) {
71         w.process();
72    }
73 }
74
75 void
76 Score::process()
77 {
78     setup_music();
79
80     paper();
81     midi();
82 }
83
84 void
85 Score::paper()
86 {
87     if (!paper_p_)
88         return;
89     if( errorlevel_i_){
90         // should we? hampers debugging. 
91         warning("Errors found, /*not processing score*/");
92 //      return;
93     }
94     pscore_p_ = new PScore(paper_p_);
95     do_cols();
96     
97     for (iter_top(staffs_,i); i.ok(); i++) 
98         i->set_output(pscore_p_);
99
100     
101     process_music();
102     clean_cols();    // can't move clean_cols() farther up.
103     print();
104     calc_idealspacing();
105
106     // debugging
107     OK();
108     *mlog << endl;
109     pscore_p_->process();
110
111     // output
112     paper_output();
113     
114 }
115
116 /**
117   Remove empty cols, preprocess other columns.
118   */
119 void
120 Score::clean_cols()
121 {
122 #if 1
123     for (iter_top(staffs_,i); i.ok(); i++)
124         i->clean_cols();
125
126     for (iter_top(rcols_,i); i.ok(); i++) {
127         i->when();              // update cache, ugh
128         if (!i->command_column_l_->used_b()) {
129             i->command_column_l_ = 0;
130         }
131         if (!i->musical_column_l_->used_b())
132             i->musical_column_l_ = 0;
133     }
134     
135     for (iter_top(cols_,c); c.ok(); ) {
136         if (!c->pcol_l_->used_b()) {
137             delete c.remove_p();
138         } else {
139             c->preprocess();
140             c++;
141         }
142     }
143 #endif
144 }
145
146 PCursor<Score_column*>
147 Score::find_col(Moment w, bool mus)
148 {
149     iter_top( cols_,i);
150     
151     for (; i.ok(); i++) {
152         if (i->when() == w && i->musical_b_ == mus)
153             return i;
154         if (i->when() > w)
155             break;
156     }
157     assert(false);
158     return i;
159 }
160
161 void
162 Score::do_cols()    
163 {
164     iter_top(cols_,i);
165     for (; i.ok(); i++) {
166         pscore_p_->add(i->pcol_l_);
167     }
168 }
169
170 Moment
171 Score::last() const
172 {    
173     Moment l = 0;
174     for (iter_top(staffs_,i); i.ok(); i++) {
175         l = l>? i->last();
176     }
177     return l;
178 }
179
180 void
181 Score::set(Paper_def *pap_p)
182 {
183     delete paper_p_;
184     paper_p_ = pap_p;
185 }
186
187 void
188 Score::set(Midi_def* midi_p)
189 {    
190     delete midi_p_;
191     midi_p_ = midi_p;
192 }
193
194 void
195 Score::OK() const
196 {
197 #ifndef NDEBUG
198     for (iter_top(staffs_,i); i.ok(); i++) {
199         i->OK();
200         assert(i->score_l_ == this);
201     }
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());
206     }
207 #endif    
208 }
209
210
211 void
212 Score::print() const
213 {
214 #ifndef NPRINT
215     mtor << "score {\n"; 
216     for (iter_top(staffs_,i); i.ok(); i++) {
217         i->print();
218     }
219     for (iter_top(cols_,i); i.ok(); i++) {
220         i->print();
221     }
222     if (pscore_p_)
223         pscore_p_->print();
224     if (midi_p_)
225         midi_p_->print();
226     
227     mtor << "}\n";
228 #endif
229 }
230
231 Score::Score()
232 {
233     pscore_p_=0;
234     paper_p_ = 0;
235     midi_p_ = 0;
236     errorlevel_i_ = 0;
237 }
238
239 Score::~Score()
240 {
241     delete pscore_p_;
242     delete paper_p_;
243     delete midi_p_;
244 }
245
246 void
247 Score::paper_output()
248 {
249     if (paper_p_->outfile=="")
250         paper_p_->outfile = default_out_fn + ".out";
251
252     if ( errorlevel_i_ ) { 
253         *mlog << "lilypond: warning: no output to: " << paper_p_->outfile 
254         << " (errorlevel=" << errorlevel_i_ << ")" << endl;
255         return;
256     }
257
258     *mlog << "TeX output to " << paper_p_->outfile << " ...\n";
259     
260     Tex_stream the_output(paper_p_->outfile);
261     
262     the_output << "% outputting Score, defined at: " <<
263         input_.location_str() << "\n";
264     pscore_p_->output(the_output);
265     
266 }
267
268 void
269 Score::midi()
270 {
271     if (!midi_p_)
272         return;
273
274     if (midi_p_->outfile_str_ == "")
275         midi_p_->outfile_str_ = default_out_fn + ".midi";
276     
277     *mlog << "midi output to " << midi_p_->outfile_str_ << " ...\n";    
278     Midi_output(this, midi_p_);
279 }
280
281 void
282 Score::add(Staff*s)
283 {
284     s->score_l_ = this;
285     staffs_.bottom().add(s);
286 }