]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
release: 0.0.63
[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         i->when();              // update cache, ugh
118         if (!i->command_column_l_->used_b()) {
119             i->command_column_l_ = 0;
120         }
121         if (!i->musical_column_l_->used_b())
122             i->musical_column_l_ = 0;
123     }
124     
125     for (iter_top(cols_,c); c.ok(); ) {
126         if (!c->pcol_l_->used_b()) {
127             delete c.remove_p();
128         } else {
129             c->preprocess();
130             c++;
131         }
132     }
133 #endif
134 }
135
136 PCursor<Score_column*>
137 Score::find_col(Moment w, bool mus)
138 {
139     iter_top( cols_,i);
140     
141     for (; i.ok(); i++) {
142         if (i->when() == w && i->musical_b_ == mus)
143             return i;
144         if (i->when() > w)
145             break;
146     }
147     assert(false);
148     return i;
149 }
150
151 void
152 Score::do_cols()    
153 {
154     iter_top(cols_,i);
155     for (; i.ok(); i++) {
156         pscore_p_->add(i->pcol_l_);
157     }
158 }
159
160 Moment
161 Score::last() const
162 {    
163     Moment l = 0;
164     for (iter_top(staffs_,i); i.ok(); i++) {
165         l = l>? i->last();
166     }
167     return l;
168 }
169
170 void
171 Score::set(Paper_def *pap_p)
172 {
173     delete paper_p_;
174     paper_p_ = pap_p;
175 }
176
177 void
178 Score::set(Midi_def* midi_p)
179 {    
180     delete midi_p_;
181     midi_p_ = midi_p;
182 }
183
184 void
185 Score::OK() const
186 {
187 #ifndef NDEBUG
188     for (iter_top(staffs_,i); i.ok(); i++) {
189         i->OK();
190         assert(i->score_l_ == this);
191     }
192     staffs_.OK();
193     cols_.OK();
194     for (iter_top(cols_,cc); cc.ok() && (cc+1).ok(); cc++) {
195         assert(cc->when() <= (cc+1)->when());
196     }
197 #endif    
198 }
199
200
201 void
202 Score::print() const
203 {
204 #ifndef NPRINT
205     mtor << "score {\n"; 
206     for (iter_top(staffs_,i); i.ok(); i++) {
207         i->print();
208     }
209     for (iter_top(cols_,i); i.ok(); i++) {
210         i->print();
211     }
212     if (pscore_p_)
213         pscore_p_->print();
214     if (midi_p_)
215         midi_p_->print();
216     
217     mtor << "}\n";
218 #endif
219 }
220
221 Score::Score()
222 {
223     pscore_p_=0;
224     paper_p_ = 0;
225     midi_p_ = 0;
226     errorlevel_i_ = 0;
227 }
228
229 Score::~Score()
230 {
231     delete pscore_p_;
232     delete paper_p_;
233     delete midi_p_;
234 }
235
236 void
237 Score::paper_output()
238 {
239     if (paper_p_->outfile=="")
240         paper_p_->outfile = default_out_fn + ".out";
241
242     if ( errorlevel_i_ ) { 
243         *mlog << "lilypond: warning: no output to: " << paper_p_->outfile 
244         << " (errorlevel=" << errorlevel_i_ << ")" << endl;
245         return;
246     }
247
248     *mlog << "TeX output to " << paper_p_->outfile << " ...\n";
249     
250     Tex_stream the_output(paper_p_->outfile);
251     
252     the_output << "% outputting Score, defined at: " <<
253         input_.location_str() << "\n";
254     pscore_p_->output(the_output);
255     
256 }
257
258 void
259 Score::midi()
260 {
261     if (!midi_p_)
262         return;
263
264     if (midi_p_->outfile_str_ == "")
265         midi_p_->outfile_str_ = default_out_fn + ".midi";
266     
267     *mlog << "midi output to " << midi_p_->outfile_str_ << " ...\n";    
268     Midi_output(this, midi_p_);
269 }
270
271 void
272 Score::add(Staff*s)
273 {
274     s->score_l_ = this;
275     staffs_.bottom().add(s);
276 }