]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
release: 0.0.46.jcn1
[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
22 extern String default_out_fn;
23
24 void
25 Score::setup_music()
26 {
27     *mlog << "\nSetting up music ..." << flush;
28     if (last() == Moment(0)) {
29         errorlevel_i_ |= 1;
30         input_.error("Need to have music in a score.");
31     }
32
33     for (iter_top(staffs_,i); i.ok(); i++) {
34         i->setup_staffcols();
35     }
36 }
37
38 void
39 Score::process_music()
40 {
41     *mlog << "Processing music ..." << flush;
42     for (Score_walker w(this); w.ok(); w++) {
43         w.process();
44    }
45 }
46
47 void
48 Score::process()
49 {
50     setup_music();
51
52     paper();
53     midi();
54 }
55
56 void
57 Score::paper()
58 {
59     if (!paper_p_)
60         return;
61     
62     pscore_p_ = new PScore(paper_p_);
63
64     find_col(0, false)->set_breakable(); // ugh
65     find_col(last(), false)->set_breakable();
66     do_cols();
67     
68     for (iter_top(staffs_,i); i.ok(); i++) 
69         i->set_output(pscore_p_);
70
71     
72     process_music();
73     clean_cols();    // can't move clean_cols() farther up.
74     print();
75     calc_idealspacing();
76
77     // debugging
78     OK();
79     *mlog << endl;
80     pscore_p_->process();
81
82     // output
83     paper_output();
84     
85 }
86
87 /**
88   Remove empty cols, preprocess other columns.
89   */
90 void
91 Score::clean_cols()
92 {
93     for (iter_top(staffs_,i); i.ok(); i++)
94         i->clean_cols();
95
96     for (iter_top(cols_,c); c.ok(); ) {
97         if (!c->pcol_l_->used_b()) {
98             delete c.remove_p();
99         } else {
100             c->preprocess();
101             c++;
102         }
103     }
104 }
105
106 /** Create columns at time #w#.  This sux.  We should have
107   Score_column create the appropriate PCol.  Unfortunately, PCols
108   don't know about their position.
109
110   @return cursor pointing to the nonmusical (first) column */
111 PCursor<Score_column*>
112 Score::create_cols(Moment w, PCursor<Score_column*> &i)
113 {
114     Score_column* c1 = new Score_column(w);
115     Score_column* c2 = new Score_column(w);
116     
117     c1->musical_b_ = false;
118     c2->musical_b_ = true;
119
120     if (i.ok()) {
121         i --;
122     }
123     if ( !i.ok() ) {
124         i = cols_.top();
125     }
126     for (; i.ok(); i++) {
127         if (i->when() > w)
128             break;
129     }
130
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;
140     }
141     return i;
142 }
143
144 PCursor<Score_column*>
145 Score::find_col(Moment w, bool mus)
146 {
147     iter_top( cols_,i);
148     
149     for (; i.ok(); i++) {
150         if (i->when() == w && i->musical_b_ == mus)
151             return i;
152         if (i->when() > w)
153             break;
154     }
155     i = create_cols(w,i);
156     if (mus)
157         i++;
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 }