]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
* scripts/lilypond-book.py (Compile_error.process_include): catch
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <stdio.h>
10
11 #include "lily-parser.hh"
12 #include "book.hh"
13 #include "cpu-timer.hh"
14 #include "global-context.hh"
15 #include "ly-module.hh"
16 #include "ly-smobs.icc"
17 #include "main.hh"
18 #include "music-iterator.hh"
19 #include "output-def.hh"
20 #include "music-output.hh"
21 #include "music.hh"
22 #include "paper-book.hh"
23 #include "output-def.hh"
24 #include "paper-score.hh"
25 #include "scm-hash.hh"
26 #include "score.hh"
27 #include "warn.hh"
28
29 Score::Score ()
30   : Input ()
31 {
32   header_ = SCM_EOL;
33   music_ = SCM_EOL;
34   error_found_ = false;
35   smobify_self ();
36 }
37
38 Score::~Score ()
39 {
40 }
41
42 IMPLEMENT_SMOBS (Score);
43 IMPLEMENT_DEFAULT_EQUAL_P (Score);
44 IMPLEMENT_TYPE_P (Score, "ly:score?");
45
46 SCM
47 Score::mark_smob (SCM s)
48 {
49   Score *sc = (Score*) SCM_CELL_WORD_1 (s);
50   if (sc->header_)
51     scm_gc_mark (sc->header_);
52   for (int i = sc->defs_.size (); i--;)
53     scm_gc_mark (sc->defs_[i]->self_scm ());
54   return sc->music_;
55 }
56
57 int
58 Score::print_smob (SCM , SCM p, scm_print_state*)
59 {
60   scm_puts ("#<Score>", p);
61
62   return 1;
63 }
64
65 Score::Score (Score const &s)
66   : Input (s)
67 {
68   music_ = SCM_EOL;
69
70   /* FIXME: SCM_EOL? */
71   header_ = 0;
72
73   smobify_self ();
74
75   Music *m =unsmob_music (s.music_);
76   music_ = m ? m->clone ()->self_scm () : SCM_EOL;
77   scm_gc_unprotect_object (music_);
78   
79   for (int i = 0; i < s.defs_.size (); i++)
80     defs_.push (s.defs_[i]->clone ());
81
82   header_ = ly_make_anonymous_module (false);
83   if (ly_c_module_p (s.header_))
84     ly_import_module (header_, s.header_);
85 }
86
87
88 LY_DEFINE (ly_run_translator, "ly:run-translator", 
89            2, 0, 0, (SCM mus, SCM output_def),
90            "Process @var{mus} according to @var{output_def}. "
91            "An interpretation context is set up, "
92            "and @var{mus} is interpreted with it.  "
93            "The context is returned in its final state.")
94 {
95   Output_def *odef = unsmob_output_def (output_def);
96   Music *music = unsmob_music (mus);
97
98   if (!music
99       || !music->get_length ().to_bool ())
100     {
101       warning (_ ("Need music in a score"));
102       return SCM_BOOL_F;
103     }
104   
105   SCM_ASSERT_TYPE (music, mus, SCM_ARG1, __FUNCTION__, "Music");
106   SCM_ASSERT_TYPE (odef, output_def, SCM_ARG2, __FUNCTION__, "Output definition");
107   
108   Cpu_timer timer;
109   
110   Global_context *trans = new Global_context (odef, music->get_length ());
111   
112   if (!trans)
113     {
114       programming_error ("no toplevel translator");
115       return SCM_BOOL_F;
116     }
117   progress_indication (_ ("Interpreting music... "));
118   
119   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
120   Music_iterator * iter = unsmob_iterator (protected_iter);
121   iter->init_translator (music, trans);
122
123   iter->construct_children ();
124
125   if (!iter->ok ())
126     {
127       warning (_ ("Need music in a score"));
128       /* todo: should throw exception. */
129       return SCM_BOOL_F;
130     }
131
132   trans->run_iterator_on_me (iter);
133   iter->quit ();
134   scm_remember_upto_here_1 (protected_iter);
135   trans->finish ();
136
137   if (verbose_global_b)
138     progress_indication (_f ("elapsed time: %.2f seconds",  timer.read ()));
139   
140   return scm_gc_unprotect_object (trans->self_scm ());
141 }
142
143 LY_DEFINE (ly_format_output, "ly:format-output",
144            2, 0, 0, (SCM context, SCM outname),
145            "Given a Score context in its final state,"
146            "process it and return the (rendered) result.")
147 {
148   Global_context *g = dynamic_cast<Global_context*> (unsmob_context (context));
149   SCM_ASSERT_TYPE (g, context, SCM_ARG1, __FUNCTION__, "Global context");
150   SCM_ASSERT_TYPE (scm_is_string (outname), outname, SCM_ARG2, __FUNCTION__, "output filename");
151
152   Music_output *output = g->get_output ();
153   progress_indication ("\n");
154   /* ugh, midi still wants outname  */
155   return output->process (ly_scm2string (outname));
156 }
157
158 void
159 default_rendering (SCM music, SCM outdef,
160                    SCM book_outputdef,
161                    SCM header, SCM outname)
162 {
163   SCM scaled_def = outdef;
164   SCM scaled_bookdef = book_outputdef;
165   
166   Output_def *bpd = unsmob_output_def (book_outputdef);
167
168   /* ugh.  */
169   if (bpd->c_variable ("is-bookpaper") == SCM_BOOL_T)
170     {
171       Real scale = scm_to_double (bpd->c_variable ("outputscale"));
172       
173       Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
174       scaled_def = def->self_scm ();
175
176       scaled_bookdef = scale_output_def (bpd, scale)->self_scm ();
177       unsmob_output_def (scaled_def)->parent_
178         = unsmob_output_def (scaled_bookdef);
179       
180       scm_gc_unprotect_object (scaled_bookdef);
181       scm_gc_unprotect_object (scaled_def);
182     }
183   
184   SCM context = ly_run_translator (music, scaled_def);
185   if (Global_context *g = dynamic_cast<Global_context*>
186       (unsmob_context (context)))
187     {
188       SCM systems = ly_format_output (context, outname);
189       Music_output *output = g->get_output ();
190       if (systems != SCM_UNDEFINED)
191         {
192           /* ugh, this is strange, Paper_book without a Book object. */
193           Paper_book *paper_book = new Paper_book ();
194           paper_book->header_ = header;
195           paper_book->bookpaper_ = unsmob_output_def (scaled_bookdef);
196           
197           Score_systems sc;
198           sc.systems_ = systems;
199           sc.header_ = header;
200
201           paper_book->score_systems_.push (sc);
202           
203           paper_book->classic_output (ly_scm2string (outname));
204           scm_gc_unprotect_object (paper_book->self_scm ());
205         }
206       delete output;
207     }
208
209   scm_remember_upto_here_1 (scaled_def);
210   scm_remember_upto_here_1 (scaled_bookdef);
211 }
212
213 /*
214 Format score, return systems. OUTNAME is still passed to create a midi
215 file.
216
217 PAPERBOOK should be scaled already.
218
219 */
220 SCM
221 Score::book_rendering (String outname,
222                        Output_def *paperbook,
223                        Output_def *default_def)
224 {
225   if (error_found_)
226     return SCM_EOL;
227    
228   SCM scaled_bookdef = SCM_EOL;
229   Real scale = 1.0;
230
231   if (paperbook && paperbook->c_variable ("is-bookpaper") == SCM_BOOL_T)
232     scale = scm_to_double (paperbook->c_variable ("outputscale"));
233   
234   SCM out = scm_makfrom0str (outname.to_str0 ());
235   SCM systems = SCM_EOL;
236   int outdef_count = defs_.size ();
237   for (int i = 0; !i || i < outdef_count; i++)
238     {
239       Output_def *def = outdef_count ? defs_[i] : default_def;
240       SCM scaled= SCM_EOL;
241       if (def->c_variable ("is-paper") == SCM_BOOL_T)
242         {
243           def = scale_output_def (def, scale);
244           def->parent_ = paperbook;
245           scaled = def->self_scm ();
246           scm_gc_unprotect_object (scaled);
247         }
248
249       /* TODO: fix or junk --no-paper.  */
250       SCM context = ly_run_translator (music_, def->self_scm ());
251       if (dynamic_cast<Global_context*> (unsmob_context (context)))
252         {
253           SCM s = ly_format_output (context, out);
254           if (s != SCM_UNDEFINED)
255             systems = s;
256         }
257
258       scm_remember_upto_here_1 (scaled);
259     }
260   
261   scm_remember_upto_here_1 (scaled_bookdef);
262   return systems;
263 }
264
265
266
267
268 LY_DEFINE (ly_score_embedded_format, "ly:score-embedded-format",
269            2, 0, 0, (SCM score, SCM paper),
270            "Run @var{score} through @var{paper}, an output definition, "
271            "scaled to correct outputscale already, "
272            "return a list of paper-lines.")
273 {
274   Score * sc = unsmob_score (score);
275   Output_def *od = unsmob_output_def (paper);
276
277   if (sc->error_found_)
278     {
279       return SCM_EOL;
280     }
281   
282   SCM_ASSERT_TYPE (sc, score, SCM_ARG1, __FUNCTION__, "Score");
283   SCM_ASSERT_TYPE (od, paper, SCM_ARG2, __FUNCTION__, "Output_def");
284
285   Output_def * score_def  = 0;
286
287   /* UGR, FIXME, these are default \paper blocks once again.  They
288      suck. */
289   for (int i = 0; !score_def && i < sc->defs_.size (); i++)
290     if (sc->defs_[i]->c_variable ("is-paper") == SCM_BOOL_T)
291       score_def = sc->defs_[i];
292
293   if (!score_def)
294     return scm_c_make_vector (0, SCM_EOL);
295       
296   score_def = score_def->clone ();
297   SCM prot = score_def->self_scm ();
298   scm_gc_unprotect_object (prot);
299
300   /* TODO: SCORE_DEF should be scaled according to OD->parent_ or OD
301      itself. */
302   score_def->parent_ = od;
303   
304   SCM context = ly_run_translator (sc->get_music (), score_def->self_scm ());
305   SCM lines = ly_format_output (context, scm_makfrom0str ("<embedded>"));
306   
307   scm_remember_upto_here_1 (prot);
308   return lines;
309 }
310
311 void
312 Score::set_music (SCM music, SCM parser)
313 {
314   /* URG? */
315   SCM check_funcs = ly_scheme_function ("toplevel-music-functions");
316   for (; ly_c_pair_p (check_funcs); check_funcs = ly_cdr (check_funcs))
317     music = scm_call_2 (ly_car (check_funcs), music, parser);
318
319   if (unsmob_music (music_))
320     {
321       unsmob_music (music)->origin ()->error (_("Already have music in score"));
322       unsmob_music (music_)->origin ()->error (_("This is the previous music"));
323     }
324
325   if (Music * m = unsmob_music (music))
326     {
327       m->origin()->error (_("Error found in this music expression. Ignoring it"));
328       
329       this->error_found_ = this->error_found_ || to_boolean (m->get_property ("error-found"));
330       
331     }
332
333   this->music_ = music;
334   if (this->error_found_)
335     this->music_ = SCM_EOL; 
336 }
337
338 SCM
339 Score::get_music () const
340 {
341   return music_;
342 }