2 score.cc -- implement Score
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include "lily-parser.hh"
13 #include "cpu-timer.hh"
14 #include "global-context.hh"
15 #include "ly-module.hh"
16 #include "ly-smobs.icc"
18 #include "music-iterator.hh"
19 #include "output-def.hh"
20 #include "music-output.hh"
22 #include "paper-book.hh"
23 #include "output-def.hh"
24 #include "paper-score.hh"
25 #include "scm-hash.hh"
42 IMPLEMENT_SMOBS (Score);
43 IMPLEMENT_DEFAULT_EQUAL_P (Score);
44 IMPLEMENT_TYPE_P (Score, "ly:score?");
47 Score::mark_smob (SCM s)
49 Score *sc = (Score*) SCM_CELL_WORD_1 (s);
51 scm_gc_mark (sc->header_);
52 for (int i = sc->defs_.size (); i--;)
53 scm_gc_mark (sc->defs_[i]->self_scm ());
58 Score::print_smob (SCM , SCM p, scm_print_state*)
60 scm_puts ("#<Score>", p);
65 Score::Score (Score const &s)
69 error_found_ = s.error_found_;
76 Music *m =unsmob_music (s.music_);
77 music_ = m ? m->clone ()->self_scm () : SCM_EOL;
78 scm_gc_unprotect_object (music_);
80 for (int i = 0; i < s.defs_.size (); i++)
81 defs_.push (s.defs_[i]->clone ());
83 header_ = ly_make_anonymous_module (false);
84 if (ly_c_module_p (s.header_))
85 ly_import_module (header_, s.header_);
89 LY_DEFINE (ly_run_translator, "ly:run-translator",
90 2, 0, 0, (SCM mus, SCM output_def),
91 "Process @var{mus} according to @var{output_def}. "
92 "An interpretation context is set up, "
93 "and @var{mus} is interpreted with it. "
94 "The context is returned in its final state.")
96 Output_def *odef = unsmob_output_def (output_def);
97 Music *music = unsmob_music (mus);
100 || !music->get_length ().to_bool ())
102 warning (_ ("Need music in a score"));
106 SCM_ASSERT_TYPE (music, mus, SCM_ARG1, __FUNCTION__, "Music");
107 SCM_ASSERT_TYPE (odef, output_def, SCM_ARG2, __FUNCTION__, "Output definition");
111 Global_context *trans = new Global_context (odef, music->get_length ());
115 programming_error ("no toplevel translator");
118 progress_indication (_ ("Interpreting music... "));
120 SCM protected_iter = Music_iterator::get_static_get_iterator (music);
121 Music_iterator * iter = unsmob_iterator (protected_iter);
122 iter->init_translator (music, trans);
124 iter->construct_children ();
128 warning (_ ("Need music in a score"));
129 /* todo: should throw exception. */
133 trans->run_iterator_on_me (iter);
135 scm_remember_upto_here_1 (protected_iter);
138 if (verbose_global_b)
139 progress_indication (_f ("elapsed time: %.2f seconds", timer.read ()));
141 return scm_gc_unprotect_object (trans->self_scm ());
144 LY_DEFINE (ly_format_output, "ly:format-output",
145 2, 0, 0, (SCM context, SCM outname),
146 "Given a Score context in its final state,"
147 "process it and return the (rendered) result.")
149 Global_context *g = dynamic_cast<Global_context*> (unsmob_context (context));
150 SCM_ASSERT_TYPE (g, context, SCM_ARG1, __FUNCTION__, "Global context");
151 SCM_ASSERT_TYPE (scm_is_string (outname), outname, SCM_ARG2, __FUNCTION__, "output filename");
153 Music_output *output = g->get_output ();
154 progress_indication ("\n");
155 /* ugh, midi still wants outname */
156 return output->process (ly_scm2string (outname));
160 default_rendering (SCM music, SCM outdef,
162 SCM header, SCM outname)
164 SCM scaled_def = outdef;
165 SCM scaled_bookdef = book_outputdef;
167 Output_def *bpd = unsmob_output_def (book_outputdef);
170 if (bpd->c_variable ("is-bookpaper") == SCM_BOOL_T)
172 Real scale = scm_to_double (bpd->c_variable ("outputscale"));
174 Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
175 scaled_def = def->self_scm ();
177 scaled_bookdef = scale_output_def (bpd, scale)->self_scm ();
178 unsmob_output_def (scaled_def)->parent_
179 = unsmob_output_def (scaled_bookdef);
181 scm_gc_unprotect_object (scaled_bookdef);
182 scm_gc_unprotect_object (scaled_def);
185 SCM context = ly_run_translator (music, scaled_def);
186 if (Global_context *g = dynamic_cast<Global_context*>
187 (unsmob_context (context)))
189 SCM systems = ly_format_output (context, outname);
190 Music_output *output = g->get_output ();
191 if (systems != SCM_UNDEFINED)
193 /* ugh, this is strange, Paper_book without a Book object. */
194 Paper_book *paper_book = new Paper_book ();
195 paper_book->header_ = header;
196 paper_book->bookpaper_ = unsmob_output_def (scaled_bookdef);
199 sc.systems_ = systems;
202 paper_book->score_systems_.push (sc);
204 paper_book->classic_output (ly_scm2string (outname));
205 scm_gc_unprotect_object (paper_book->self_scm ());
210 scm_remember_upto_here_1 (scaled_def);
211 scm_remember_upto_here_1 (scaled_bookdef);
215 Format score, return systems. OUTNAME is still passed to create a midi
218 PAPERBOOK should be scaled already.
222 Score::book_rendering (String outname,
223 Output_def *paperbook,
224 Output_def *default_def)
229 SCM scaled_bookdef = SCM_EOL;
232 if (paperbook && paperbook->c_variable ("is-bookpaper") == SCM_BOOL_T)
233 scale = scm_to_double (paperbook->c_variable ("outputscale"));
235 SCM out = scm_makfrom0str (outname.to_str0 ());
236 SCM systems = SCM_EOL;
237 int outdef_count = defs_.size ();
238 for (int i = 0; !i || i < outdef_count; i++)
240 Output_def *def = outdef_count ? defs_[i] : default_def;
241 SCM scaled = SCM_EOL;
242 if (def->c_variable ("is-paper") == SCM_BOOL_T)
244 def = scale_output_def (def, scale);
245 def->parent_ = paperbook;
246 scaled = def->self_scm ();
247 scm_gc_unprotect_object (scaled);
250 /* TODO: fix or junk --no-paper. */
251 SCM context = ly_run_translator (music_, def->self_scm ());
252 if (dynamic_cast<Global_context*> (unsmob_context (context)))
254 SCM s = ly_format_output (context, out);
255 if (s != SCM_UNDEFINED)
259 scm_remember_upto_here_1 (scaled);
262 scm_remember_upto_here_1 (scaled_bookdef);
269 LY_DEFINE (ly_score_embedded_format, "ly:score-embedded-format",
270 2, 0, 0, (SCM score, SCM paper),
271 "Run @var{score} through @var{paper}, an output definition, "
272 "scaled to correct outputscale already, "
273 "return a list of paper-lines.")
275 Score * sc = unsmob_score (score);
276 Output_def *od = unsmob_output_def (paper);
278 if (sc->error_found_)
283 SCM_ASSERT_TYPE (sc, score, SCM_ARG1, __FUNCTION__, "Score");
284 SCM_ASSERT_TYPE (od, paper, SCM_ARG2, __FUNCTION__, "Output_def");
286 Output_def * score_def = 0;
288 /* UGR, FIXME, these are default \paper blocks once again. They
290 for (int i = 0; !score_def && i < sc->defs_.size (); i++)
291 if (sc->defs_[i]->c_variable ("is-paper") == SCM_BOOL_T)
292 score_def = sc->defs_[i];
295 return scm_c_make_vector (0, SCM_EOL);
297 score_def = score_def->clone ();
298 SCM prot = score_def->self_scm ();
299 scm_gc_unprotect_object (prot);
301 /* TODO: SCORE_DEF should be scaled according to OD->parent_ or OD
303 score_def->parent_ = od;
305 SCM context = ly_run_translator (sc->get_music (), score_def->self_scm ());
306 SCM lines = ly_format_output (context, scm_makfrom0str ("<embedded>"));
308 scm_remember_upto_here_1 (prot);
313 Score::set_music (SCM music, SCM parser)
316 SCM check_funcs = ly_scheme_function ("toplevel-music-functions");
317 for (; scm_is_pair (check_funcs); check_funcs = scm_cdr (check_funcs))
318 music = scm_call_2 (scm_car (check_funcs), music, parser);
320 if (unsmob_music (music_))
322 unsmob_music (music)->origin ()->error (_("Already have music in score"));
323 unsmob_music (music_)->origin ()->error (_("This is the previous music"));
325 Music * m = unsmob_music (music);
326 if (m && to_boolean (m->get_property ("error-found")))
328 m->origin()->error (_("Error found in this music expression. Ignoring it"));
330 this->error_found_ = this->error_found_ || to_boolean (m->get_property ("error-found"));
334 if (this->error_found_)
335 this->music_ = SCM_EOL;
337 this->music_ = music;
342 Score::get_music () const