]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
patch::: 0.0.73pre.jcn1: epsilon performance
[lilypond.git] / lily / score.cc
1 /*
2   score.cc -- implement Score
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include "tex-stream.hh"
10 #include "score.hh"
11 #include "score-column.hh"
12 #include "p-score.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 "midi-output.hh"
19 #include "midi-def.hh"
20 #include "p-col.hh"
21 #include "music-iterator.hh"
22 #include "music.hh"
23 #include "global-translator.hh"
24
25 extern String default_out_fn;
26
27 Score::Score(Score const &s)
28 {
29     assert(!pscore_p_);
30     music_p_ = s.music_p_->clone();
31     midi_p_ = new Midi_def(*s.midi_p_);
32     paper_p_ = new Paper_def(*s.paper_p_);
33 }
34
35 void
36 Score::run_translator(Global_translator * trans_l)
37 {
38     trans_l->set_score (this);
39     Music_iterator * iter = Music_iterator::static_get_iterator_p(music_p_, 
40                                                                   trans_l);
41     iter->construct_children();
42
43     while ( iter->ok() || trans_l->moments_left_i() ) {
44         Moment w = INFTY;
45         if (iter->ok() ) {
46             w = iter->next_moment();
47             iter->print();
48         }
49         trans_l->modify_next( w );
50         trans_l->prepare(w);
51         iter->process_and_next( w );
52         trans_l->process();
53     }
54     delete iter;
55     trans_l->finish();
56 }
57
58
59 void
60 Score::process()
61 {
62     paper();
63     midi();
64 }
65
66 void
67 Score::midi()
68 {
69 #if 0
70     if (!midi_p_)
71         return;
72     
73     *mlog << "\nCreating elements ..." << flush;
74 //    pscore_p_ = new PScore(paper_p_);
75     
76     Global_translator * score_trans=  paper_p_->get_global_translator_p();
77     run_translator( score_trans );
78     delete score_trans;
79     
80     if( errorlevel_i_){
81         // should we? hampers debugging. 
82         warning("Errors found, /*not processing score*/");
83 //      return;
84     }
85     print();
86     *mlog << endl;
87 //    pscore_p_->process();
88
89     // output
90     midi_output();
91 #endif
92 }
93     
94 void
95 Score::paper()
96 {
97     if (!paper_p_)
98         return;
99     
100     *mlog << "\nCreating elements ..." << flush;
101     pscore_p_ = new PScore(paper_p_);
102     
103     Global_translator * score_trans=  paper_p_->get_global_translator_p();
104     run_translator( score_trans );
105     delete score_trans;
106     
107     if( errorlevel_i_){
108         // should we? hampers debugging. 
109         warning("Errors found, /*not processing score*/");
110 //      return;
111     }
112     do_cols();
113     
114     clean_cols();    // can't move clean_cols() farther up.
115     print();
116     calc_idealspacing();
117
118     // debugging
119     OK();
120     *mlog << endl;
121     pscore_p_->process();
122
123     // output
124     paper_output();
125 }
126
127 /**
128   Remove empty cols, preprocess other columns.
129   */
130 void
131 Score::clean_cols()
132 {
133     for (iter_top(cols_,c); c.ok(); ) {
134         if (!c->pcol_l_->used_b()) {
135             delete c.remove_p();
136         } else {
137             c->preprocess();
138             c++;
139         }
140     }
141 }
142
143 PCursor<Score_column*>
144 Score::find_col(Moment w, bool mus)
145 {
146     iter_top( cols_,i);
147     
148     for (; i.ok(); i++) {
149         if (i->when() == w && i->musical_b_ == mus)
150             return i;
151         if (i->when() > w)
152             break;
153     }
154     assert(false);
155     return i;
156 }
157
158 void
159 Score::do_cols()    
160 {
161     iter_top(cols_,i);
162     for (; i.ok(); i++) {
163         pscore_p_->add(i->pcol_l_);
164     }
165 }
166
167 void
168 Score::set(Paper_def *pap_p)
169 {
170     delete paper_p_;
171     paper_p_ = pap_p;
172 }
173
174 void
175 Score::set(Midi_def* midi_p)
176 {    
177     delete midi_p_;
178     midi_p_ = midi_p;
179 }
180
181 void
182 Score::OK() const
183 {
184 #ifndef NDEBUG
185     cols_.OK();
186     for (iter_top(cols_,cc); cc.ok() && (cc+1).ok(); cc++) {
187         assert(cc->when() <= (cc+1)->when());
188     }
189 #endif    
190 }
191
192
193 void
194 Score::print() const
195 {
196 #ifndef NPRINT
197     mtor << "score {\n"; 
198     music_p_->print();
199     for (iter_top(cols_,i); i.ok(); i++) {
200         i->print();
201     }
202     if (pscore_p_)
203         pscore_p_->print();
204     if (midi_p_)
205         midi_p_->print();
206     
207     mtor << "}\n";
208 #endif
209 }
210
211 Score::Score()
212 {
213     pscore_p_=0;
214     paper_p_ = 0;
215     midi_p_ = 0;
216     errorlevel_i_ = 0;
217 }
218
219 Score::~Score()
220 {
221     delete music_p_;
222     delete pscore_p_;
223     delete paper_p_;
224     delete midi_p_;
225 }
226
227 void
228 Score::paper_output()
229 {
230     if (paper_p_->outfile_str_=="")
231         paper_p_->outfile_str_ = default_out_fn + ".out";
232
233     if ( errorlevel_i_ ) { 
234         *mlog << "lilypond: warning: no output to: " << paper_p_->outfile_str_ 
235         << " (errorlevel=" << errorlevel_i_ << ")" << endl;
236         return;
237     }
238
239     *mlog << "TeX output to " << paper_p_->outfile_str_ << " ...\n";
240     
241     Tex_stream the_output(paper_p_->outfile_str_);
242     
243     the_output << "% outputting Score, defined at: " <<
244         location_str() << "\n";
245     pscore_p_->output(the_output);
246     
247 }
248
249 void
250 Score::midi_output()
251 {
252 #if 0
253     if (!midi_p_)
254         return;
255
256     if (midi_p_->outfile_str_ == "")
257         midi_p_->outfile_str_ = default_out_fn + ".midi";
258     
259     *mlog << "midi output to " << midi_p_->outfile_str_ << " ...\n";    
260     Midi_output(this, midi_p_);
261 #endif
262 }
263