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