]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
release: 0.0.72pre
[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-grav.hh"
21 #include "music-iterator.hh"
22 #include "music.hh"
23 #include "music-list.hh"
24 #include "input-engraver.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_translator(Global_translator * 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_engraver * score_grav=  
76         (Score_engraver*)lookup_grav("Score_engraver")->get_group_engraver_p();
77     run_translator( score_grav );
78     delete score_grav;
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 void
142 Score::set(Paper_def *pap_p)
143 {
144     delete paper_p_;
145     paper_p_ = pap_p;
146 }
147
148 void
149 Score::set(Midi_def* midi_p)
150 {    
151     delete midi_p_;
152     midi_p_ = midi_p;
153 }
154
155 void
156 Score::OK() const
157 {
158 #ifndef NDEBUG
159     cols_.OK();
160     for (iter_top(cols_,cc); cc.ok() && (cc+1).ok(); cc++) {
161         assert(cc->when() <= (cc+1)->when());
162     }
163 #endif    
164 }
165
166
167 void
168 Score::print() const
169 {
170 #ifndef NPRINT
171     mtor << "score {\n"; 
172     music_p_->print();
173     for (iter_top(cols_,i); i.ok(); i++) {
174         i->print();
175     }
176     if (pscore_p_)
177         pscore_p_->print();
178     if (midi_p_)
179         midi_p_->print();
180     
181     mtor << "}\n";
182 #endif
183 }
184
185 Score::Score()
186 {
187     pscore_p_=0;
188     paper_p_ = 0;
189     midi_p_ = 0;
190     errorlevel_i_ = 0;
191 }
192
193 Score::~Score()
194 {
195     delete music_p_;
196     delete pscore_p_;
197     delete paper_p_;
198     delete midi_p_;
199 }
200
201 void
202 Score::paper_output()
203 {
204     if (paper_p_->outfile_str_=="")
205         paper_p_->outfile_str_ = default_out_fn + ".out";
206
207     if ( errorlevel_i_ ) { 
208         *mlog << "lilypond: warning: no output to: " << paper_p_->outfile_str_ 
209         << " (errorlevel=" << errorlevel_i_ << ")" << endl;
210         return;
211     }
212
213     *mlog << "TeX output to " << paper_p_->outfile_str_ << " ...\n";
214     
215     Tex_stream the_output(paper_p_->outfile_str_);
216     
217     the_output << "% outputting Score, defined at: " <<
218         location_str() << "\n";
219     pscore_p_->output(the_output);
220     
221 }
222
223 void
224 Score::midi()
225 {
226 #if 0
227     if (!midi_p_)
228         return;
229
230     if (midi_p_->outfile_str_ == "")
231         midi_p_->outfile_str_ = default_out_fn + ".midi";
232     
233     *mlog << "midi output to " << midi_p_->outfile_str_ << " ...\n";    
234     Midi_output(this, midi_p_);
235 #endif
236 }
237