]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
patch::: 0.0.74pre.jcn1: pats
[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 (!midi_p_)
70         return;
71     
72     *mlog << "\nCreating elements ..." << endl; //flush;
73 //    pscore_p_ = new PScore(paper_p_);
74     
75     Global_translator* score_trans=  midi_p_->get_global_translator_p();
76     run_translator( score_trans );
77     delete score_trans;
78     
79     if( errorlevel_i_){
80         // should we? hampers debugging. 
81         warning("Errors found, /*not processing score*/");
82 //      return;
83     }
84     print();
85     *mlog << endl;
86 //    pscore_p_->process();
87
88     // output
89     midi_output();
90 }
91     
92 void
93 Score::paper()
94 {
95     if (!paper_p_)
96         return;
97     
98     *mlog << "\nCreating elements ..." << flush;
99     pscore_p_ = new PScore(paper_p_);
100     
101     Global_translator * score_trans=  paper_p_->get_global_translator_p();
102     run_translator( score_trans );
103     delete score_trans;
104     
105     if( errorlevel_i_){
106         // should we? hampers debugging. 
107         warning("Errors found, /*not processing score*/");
108 //      return;
109     }
110     do_cols();
111     
112     clean_cols();    // can't move clean_cols() farther up.
113     print();
114     calc_idealspacing();
115
116     // debugging
117     OK();
118     *mlog << endl;
119     pscore_p_->process();
120
121     // output
122     paper_output();
123 }
124
125 /**
126   Remove empty cols, preprocess other columns.
127   */
128 void
129 Score::clean_cols()
130 {
131     for (iter_top(cols_,c); c.ok(); ) {
132         if (!c->pcol_l_->used_b()) {
133             delete c.remove_p();
134         } else {
135             c->preprocess();
136             c++;
137         }
138     }
139 }
140
141 PCursor<Score_column*>
142 Score::find_col(Moment w, bool mus)
143 {
144     iter_top( cols_,i);
145     
146     for (; i.ok(); i++) {
147         if (i->when() == w && i->musical_b_ == mus)
148             return i;
149         if (i->when() > w)
150             break;
151     }
152     assert(false);
153     return i;
154 }
155
156 void
157 Score::do_cols()    
158 {
159     iter_top(cols_,i);
160     for (; i.ok(); i++) {
161         pscore_p_->add(i->pcol_l_);
162     }
163 }
164
165 void
166 Score::set(Paper_def *pap_p)
167 {
168     delete paper_p_;
169     paper_p_ = pap_p;
170 }
171
172 void
173 Score::set(Midi_def* midi_p)
174 {    
175     delete midi_p_;
176     midi_p_ = midi_p;
177 }
178
179 void
180 Score::OK() const
181 {
182 #ifndef NDEBUG
183     cols_.OK();
184     for (iter_top(cols_,cc); cc.ok() && (cc+1).ok(); cc++) {
185         assert(cc->when() <= (cc+1)->when());
186     }
187 #endif    
188 }
189
190
191 void
192 Score::print() const
193 {
194 #ifndef NPRINT
195     mtor << "score {\n"; 
196     music_p_->print();
197     for (iter_top(cols_,i); i.ok(); i++) {
198         i->print();
199     }
200     if (pscore_p_)
201         pscore_p_->print();
202     if (midi_p_)
203         midi_p_->print();
204     
205     mtor << "}\n";
206 #endif
207 }
208
209 Score::Score()
210 {
211     pscore_p_=0;
212     paper_p_ = 0;
213     midi_p_ = 0;
214     errorlevel_i_ = 0;
215 }
216
217 Score::~Score()
218 {
219     delete music_p_;
220     delete pscore_p_;
221     delete paper_p_;
222     delete midi_p_;
223 }
224
225 void
226 Score::paper_output()
227 {
228     if (paper_p_->outfile_str_=="")
229         paper_p_->outfile_str_ = default_out_fn + ".out";
230
231     if ( errorlevel_i_ ) { 
232         *mlog << "lilypond: warning: no output to: " << paper_p_->outfile_str_ 
233         << " (errorlevel=" << errorlevel_i_ << ")" << endl;
234         return;
235     }
236
237     *mlog << "TeX output to " << paper_p_->outfile_str_ << " ...\n";
238     
239     Tex_stream the_output(paper_p_->outfile_str_);
240     
241     the_output << "% outputting Score, defined at: " <<
242         location_str() << "\n";
243     pscore_p_->output(the_output);
244     
245 }
246
247 void
248 Score::midi_output()
249 {
250 #if 0
251     if (!midi_p_)
252         return;
253
254     if (midi_p_->outfile_str_ == "")
255         midi_p_->outfile_str_ = default_out_fn + ".midi";
256     
257     *mlog << "midi output to " << midi_p_->outfile_str_ << " ...\n";    
258     Midi_output(this, midi_p_);
259 #endif
260 }
261