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