]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
release: 0.0.44
[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         error("Need to have music in a score.", defined_ch_C_);
31     }
32
33     for (iter_top(staffs_,i); i.ok(); i++) {
34         i->setup_staffcols();
35         i->OK();
36     }
37 }
38
39 void
40 Score::process_music()
41 {
42     *mlog << "Processing music ..." << flush;
43     for (Score_walker w(this); w.ok(); w++) {
44         w.process();
45    }
46 }
47
48 void
49 Score::process()
50 {
51     setup_music();
52
53     paper();
54     midi();
55 }
56
57 void
58 Score::paper()
59 {
60     if (!paper_p_)
61         return;
62     
63     pscore_p_ = new PScore(paper_p_);
64
65     find_col(0, false)->set_breakable(); // ugh
66     find_col(last(), false)->set_breakable();
67     do_cols();
68     
69     for (iter_top(staffs_,i); i.ok(); i++) 
70         i->set_output(pscore_p_);
71
72     
73     process_music();
74     clean_cols();    // can't move clean_cols() farther up.
75     print();
76     calc_idealspacing();
77
78     // debugging
79     OK();
80     *mlog << endl;
81     pscore_p_->process();
82
83     // output
84     paper_output();
85     
86 }
87
88 /**
89   Remove empty cols, preprocess other columns.
90   */
91 void
92 Score::clean_cols()
93 {
94     for (iter_top(staffs_,i); i.ok(); i++)
95         i->clean_cols();
96
97     for (iter_top(cols_,c); c.ok(); ) {
98         if (!c->pcol_l_->used_b()) {
99             delete c.remove_p();
100         } else {
101             c->preprocess();
102             c++;
103         }
104     }
105 }
106
107 /**
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.
111
112   @return cursor pointing to the nonmusical (first) column
113   */
114 PCursor<Score_column*>
115 Score::create_cols(Moment w)
116 {
117     Score_column* c1 = new Score_column(w);
118     Score_column* c2 = new Score_column(w);
119     
120     c1->musical_b_ = false;
121     c2->musical_b_ = true;
122     
123     iter_top(cols_,i);
124
125     for (; i.ok(); i++) {
126         assert(i->when() != w);
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);
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     defined_ch_C_ = 0;
238 }
239
240 Score::~Score()
241 {
242     delete pscore_p_;
243     delete paper_p_;
244     delete midi_p_;
245 }
246
247 void
248 Score::paper_output()
249 {
250     OK();
251     if (paper_p_->outfile=="")
252         paper_p_->outfile = default_out_fn + ".out";
253
254     if ( errorlevel_i_ ) { 
255         *mlog << "lilypond: warning: no output to: " << paper_p_->outfile 
256         << " (errorlevel=" << errorlevel_i_ << ")" << endl;
257         return;
258     }
259
260     *mlog << "TeX output to " << paper_p_->outfile << " ...\n";
261     
262     Tex_stream the_output(paper_p_->outfile);
263     
264     the_output << "% outputting Score, defined at: " <<
265         source_l_g->
266         sourcefile_l (defined_ch_C_)->file_line_no_str(defined_ch_C_) << "\n";
267     pscore_p_->output(the_output);
268 }
269
270 void
271 Score::midi()
272 {
273     if (!midi_p_)
274         return;
275
276     if (midi_p_->outfile_str_ == "")
277         midi_p_->outfile_str_ = default_out_fn + ".midi";
278     
279     *mlog << "midi output to " << midi_p_->outfile_str_ << " ...\n";    
280     Midi_output(this, midi_p_);
281 }
282
283 void
284 Score::add(Staff*s)
285 {
286     s->score_l_ = this;
287     staffs_.bottom().add(s);
288 }