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