]> git.donarmstrong.com Git - lilypond.git/blob - src/score.cc
release: 0.0.34
[lilypond.git] / src / 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 "tstream.hh"
9 #include "score.hh"
10 #include "sccol.hh"
11 #include "pscore.hh"
12 #include "staff.hh"
13 #include "debug.hh"
14 #include "paperdef.hh"
15 #include "main.hh"
16 #include "source.hh"
17 #include "sourcefile.hh"
18 #include "scorewalker.hh"
19 #include "midioutput.hh"
20 #include "mididef.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_l_);
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.get();
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 {
165
166     iter_top(cols_,i);
167     for (; i.ok(); i++) {
168         pscore_p_->add(i->pcol_l_);
169     }
170
171 }
172
173 Moment
174 Score::last() const
175 {    
176     Moment l = 0;
177     for (iter_top(staffs_,i); i.ok(); i++) {
178         l = l>? i->last();
179     }
180     return l;
181 }
182
183 void
184 Score::set(Paperdef *pap_p)
185 {
186     delete paper_p_;
187     paper_p_ = pap_p;
188 }
189
190 void
191 Score::set(Midi_def* midi_p)
192 {    
193     delete midi_p_;
194     midi_p_ = midi_p;
195 }
196
197 void
198 Score::OK() const
199 {
200 #ifndef NDEBUG
201     for (iter_top(staffs_,i); i.ok(); i++) {
202         i->OK();
203         assert(i->score_l_ == this);
204     }
205     staffs_.OK();
206     cols_.OK();
207     for (iter_top(cols_,cc); cc.ok() && (cc+1).ok(); cc++) {
208         assert(cc->when() <= (cc+1)->when());
209     }
210 #endif    
211 }
212
213
214 void
215 Score::print() const
216 {
217 #ifndef NPRINT
218     mtor << "score {\n"; 
219     for (iter_top(staffs_,i); i.ok(); i++) {
220         i->print();
221     }
222     for (iter_top(cols_,i); i.ok(); i++) {
223         i->print();
224     }
225     if (pscore_p_)
226         pscore_p_->print();
227     if (midi_p_)
228         midi_p_->print();
229     
230     mtor << "}\n";
231 #endif
232 }
233
234 Score::Score()
235 {
236     pscore_p_=0;
237     paper_p_ = 0;
238     midi_p_ = 0;
239     errorlevel_i_ = 0;
240     defined_ch_c_l_ = 0;
241 }
242
243 Score::~Score()
244 {
245     delete pscore_p_;
246     delete paper_p_;
247     delete midi_p_;
248 }
249
250 void
251 Score::paper_output()
252 {
253     OK();
254     if (paper_p_->outfile=="")
255         paper_p_->outfile = default_out_fn + ".out";
256
257     if ( errorlevel_i_ ) { 
258         *mlog << "lilypond: warning: no output to: " << paper_p_->outfile 
259         << " (errorlevel=" << errorlevel_i_ << ")" << endl;
260         return;
261     }
262
263     *mlog << "TeX output to " << paper_p_->outfile << " ...\n";
264     
265     Tex_stream the_output(paper_p_->outfile);
266     
267     the_output << "% outputting Score, defined at: " <<
268         source_l_g->
269         sourcefile_l (defined_ch_c_l_)->file_line_no_str(defined_ch_c_l_) << "\n";
270     pscore_p_->output(the_output);
271 }
272
273 void
274 Score::midi()
275 {
276     if (!midi_p_)
277         return;
278
279     if (midi_p_->outfile_str_ == "")
280         midi_p_->outfile_str_ = default_out_fn + ".midi";
281     
282     *mlog << "midi output to " << midi_p_->outfile_str_ << " ...\n";    
283     Midi_output(this, midi_p_);
284 }
285
286 void
287 Score::add(Staff*s)
288 {
289     s->score_l_ = this;
290     staffs_.bottom().add(s);
291 }