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