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