]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
80057719c2c14d44bc519ba7e33b26f009ad4c07
[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-def.hh"
19 #include "midi-stream.hh"
20 #include "audio-score.hh"
21 #include "p-col.hh"
22 #include "music-iterator.hh"
23 #include "music.hh"
24 #include "global-translator.hh"
25 #include "header.hh"
26
27 extern String default_out_fn;
28
29 Score::Score()
30 {
31   header_p_ = 0;
32   music_p_ = 0;
33   pscore_p_ = 0;
34   audio_score_p_ = 0;
35   paper_p_ = 0;
36   midi_p_ = 0;
37   errorlevel_i_ = 0;
38 }
39
40 Score::Score (Score const &s)
41 {
42   assert (!pscore_p_);
43   music_p_ = s.music_p_->clone();
44   midi_p_ = new Midi_def (*s.midi_p_);
45   paper_p_ = new Paper_def (*s.paper_p_);
46   header_p_ = new Header (*s.header_p_);
47 }
48
49 Score::~Score()
50 {
51   delete header_p_;
52   delete music_p_;
53   delete pscore_p_;
54   delete audio_score_p_;
55   delete paper_p_;
56   delete midi_p_;
57 }
58
59 void
60 Score::run_translator (Global_translator * trans_l)
61 {
62   trans_l->set_score (this);
63   Music_iterator * iter = Music_iterator::static_get_iterator_p (music_p_, 
64                                                                   trans_l);
65   iter->construct_children();
66
67   if ( ! iter->ok()) 
68     {
69         delete iter;
70         warning ("Need music in a score");
71         errorlevel_i_ =1;
72         return ;
73     }
74   
75   trans_l->start();
76   
77   while ( iter->ok() || trans_l->moments_left_i ()) 
78     {
79         Moment w = infinity_mom;
80         if (iter->ok()) 
81           {
82             w = iter->next_moment();
83             DOUT << w;
84             iter->print();
85           }
86         trans_l->modify_next (w);
87         trans_l->prepare (w);
88         trans_l->print();
89
90         iter->process_and_next (w);
91         trans_l->process();
92     }
93   delete iter;
94   trans_l->finish();
95 }
96
97 void
98 Score::process()
99 {
100   print();
101   paper();
102   midi();
103 }
104
105 void
106 Score::midi()
107 {
108   if ( !midi_p_)
109         return;
110   
111   *mlog << "\nCreating MIDI elements ..." << flush;
112   audio_score_p_ = new Audio_score (this);
113   
114   Global_translator* score_trans=  midi_p_->get_global_translator_p();
115   run_translator (score_trans);
116   delete score_trans;
117   
118   if (errorlevel_i_)
119     {
120         // should we? hampers debugging. 
121         warning ("Errors found, /*not processing score*/");
122     }
123   *mlog << endl;
124
125   midi_output();
126 }
127   
128 void
129 Score::paper()
130 {
131   if (!paper_p_)
132         return;
133   
134   *mlog << "\nCreating elements ..." << flush;
135   pscore_p_ = new Paper_score (paper_p_);
136   
137   Global_translator * score_trans=  paper_p_->get_global_translator_p();
138   run_translator (score_trans);
139   delete score_trans;
140   
141   if (errorlevel_i_) 
142     {
143         // should we? hampers debugging. 
144         warning ("Errors found, /*not processing score*/");
145     }
146   
147   *mlog << endl;
148   pscore_p_->process();
149
150   // output
151   paper_output();
152 }
153
154 void
155 Score::midi_output()
156 {
157   if ( midi_p_->outfile_str_ == "")
158         midi_p_->outfile_str_ = default_out_fn + ".midi";
159
160   Midi_stream midi_stream (midi_p_->outfile_str_);    
161   *mlog << "MIDI output to " << midi_p_->outfile_str_ << " ..." << endl;    
162
163   audio_score_p_->output (midi_stream);
164   *mlog << endl;
165 }
166
167 void
168 Score::paper_output()
169 {
170   if (paper_p_->outfile_str_=="")
171         paper_p_->outfile_str_ = default_out_fn + ".tex";
172
173   if ( errorlevel_i_) 
174     {
175         *mlog << "lilypond: warning: no output to: " << paper_p_->outfile_str_ 
176         << " (errorlevel=" << errorlevel_i_ << ")" << endl;
177       return;
178     }
179
180   *mlog << "TeX output to " << paper_p_->outfile_str_ << " ...\n";
181   
182   Tex_stream the_output (paper_p_->outfile_str_);
183   
184   the_output << "% outputting Score, defined at: " <<
185         location_str() << "\n";
186   if (header_p_) {
187     the_output << header_p_->TeX_string();
188   }
189   pscore_p_->output (the_output);
190 }
191
192 void
193 Score::print() const
194 {
195 #ifndef NPRINT
196   DOUT << "score {\n"; 
197   music_p_->print();
198   if (midi_p_)
199         midi_p_->print();
200   
201   DOUT << "}\n";
202 #endif
203 }
204
205 void
206 Score::set (Paper_def *pap_p)
207 {
208   delete paper_p_;
209   paper_p_ = pap_p;
210 }
211
212 void
213 Score::set (Midi_def* midi_p)
214 {    
215   delete midi_p_;
216   midi_p_ = midi_p;
217 }
218