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>
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"
41 IMPLEMENT_SMOBS (Score);
42 IMPLEMENT_DEFAULT_EQUAL_P (Score);
43 IMPLEMENT_TYPE_P (Score, "ly:score?");
46 Score::mark_smob (SCM s)
48 Score *sc = (Score*) SCM_CELL_WORD_1 (s);
50 scm_gc_mark (sc->header_);
51 for (int i = sc->defs_.size (); i--;)
52 scm_gc_mark (sc->defs_[i]->self_scm ());
57 Score::print_smob (SCM , SCM p, scm_print_state*)
59 scm_puts ("#<Score>", p);
64 Score::Score (Score const &s)
74 Music *m =unsmob_music (s.music_);
75 music_ = m ? m->clone ()->self_scm () : SCM_EOL;
76 scm_gc_unprotect_object (music_);
78 for (int i = 0; i < s.defs_.size (); i++)
79 defs_.push (s.defs_[i]->clone ());
81 header_ = ly_make_anonymous_module (false);
82 if (ly_c_module_p (s.header_))
83 ly_import_module (header_, s.header_);
87 LY_DEFINE (ly_run_translator, "ly:run-translator",
88 2, 0, 0, (SCM mus, SCM output_def),
89 "Process @var{mus} according to @var{output_def}. "
90 "An interpretation context is set up, "
91 "and @var{mus} is interpreted with it. "
92 "The context is returned in its final state.")
94 Output_def *odef = unsmob_output_def (output_def);
95 Music *music = unsmob_music (mus);
98 || !music->get_length ().to_bool ())
100 warning (_ ("Need music in a score"));
104 SCM_ASSERT_TYPE (music, mus, SCM_ARG1, __FUNCTION__, "Music");
105 SCM_ASSERT_TYPE (odef, output_def, SCM_ARG2, __FUNCTION__, "Output definition");
109 Global_context *trans = new Global_context (odef, music->get_length ());
113 programming_error ("no toplevel translator");
116 progress_indication (_ ("Interpreting music... "));
118 SCM protected_iter = Music_iterator::get_static_get_iterator (music);
119 Music_iterator * iter = unsmob_iterator (protected_iter);
120 iter->init_translator (music, trans);
122 iter->construct_children ();
126 warning (_ ("Need music in a score"));
127 /* todo: should throw exception. */
131 trans->run_iterator_on_me (iter);
133 scm_remember_upto_here_1 (protected_iter);
136 if (verbose_global_b)
137 progress_indication (_f ("elapsed time: %.2f seconds", timer.read ()));
139 return scm_gc_unprotect_object (trans->self_scm ());
142 LY_DEFINE (ly_format_output, "ly:format-output",
143 2, 0, 0, (SCM context, SCM outname),
144 "Given a Score context in its final state,"
145 "process it and return the (rendered) result.")
147 Global_context *g = dynamic_cast<Global_context*> (unsmob_context (context));
148 SCM_ASSERT_TYPE (g, context, SCM_ARG1, __FUNCTION__, "Global context");
149 SCM_ASSERT_TYPE (scm_is_string (outname), outname, SCM_ARG2, __FUNCTION__, "output filename");
151 Music_output *output = g->get_output ();
152 progress_indication ("\n");
153 /* ugh, midi still wants outname */
154 return output->process (ly_scm2string (outname));
158 default_rendering (SCM music, SCM outdef,
160 SCM header, SCM outname)
162 SCM scaled_def = outdef;
163 SCM scaled_bookdef = book_outputdef;
165 Output_def *bpd = unsmob_output_def (book_outputdef);
168 if (bpd->c_variable ("is-bookpaper") == SCM_BOOL_T)
170 Real scale = ly_scm2double (bpd->c_variable ("outputscale"));
172 Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
173 scaled_def = def->self_scm ();
175 scaled_bookdef = scale_output_def (bpd, scale)->self_scm ();
176 unsmob_output_def (scaled_def)->parent_
177 = unsmob_output_def (scaled_bookdef);
179 scm_gc_unprotect_object (scaled_bookdef);
180 scm_gc_unprotect_object (scaled_def);
183 SCM context = ly_run_translator (music, scaled_def);
184 if (Global_context *g = dynamic_cast<Global_context*>
185 (unsmob_context (context)))
187 SCM systems = ly_format_output (context, outname);
188 Music_output *output = g->get_output ();
189 if (systems != SCM_UNDEFINED)
191 /* ugh, this is strange, Paper_book without a Book object. */
192 Paper_book *paper_book = new Paper_book ();
193 paper_book->header_ = header;
194 paper_book->bookpaper_ = unsmob_output_def (scaled_bookdef);
197 sc.systems_ = systems;
200 paper_book->score_systems_.push (sc);
202 paper_book->classic_output (ly_scm2string (outname));
203 scm_gc_unprotect_object (paper_book->self_scm ());
208 scm_remember_upto_here_1 (scaled_def);
209 scm_remember_upto_here_1 (scaled_bookdef);
213 Format score, return systems. OUTNAME is still passed to create a midi
216 PAPERBOOK should be scaled already.
220 Score::book_rendering (String outname,
221 Output_def *paperbook,
222 Output_def *default_def)
224 SCM scaled_bookdef = SCM_EOL;
227 if (paperbook && paperbook->c_variable ("is-bookpaper") == SCM_BOOL_T)
228 scale = ly_scm2double (paperbook->c_variable ("outputscale"));
230 SCM out = scm_makfrom0str (outname.to_str0 ());
231 SCM systems = SCM_EOL;
232 int outdef_count = defs_.size ();
233 for (int i = 0; !i || i < outdef_count; i++)
235 Output_def *def = outdef_count ? defs_[i] : default_def;
237 if (def->c_variable ("is-paper") == SCM_BOOL_T)
239 def = scale_output_def (def, scale);
240 def->parent_ = paperbook;
241 scaled = def->self_scm ();
242 scm_gc_unprotect_object (scaled);
245 /* TODO: fix or junk --no-paper. */
246 SCM context = ly_run_translator (music_, def->self_scm ());
247 if (dynamic_cast<Global_context*> (unsmob_context (context)))
249 SCM s = ly_format_output (context, out);
250 if (s != SCM_UNDEFINED)
254 scm_remember_upto_here_1 (scaled);
257 scm_remember_upto_here_1 (scaled_bookdef);
264 LY_DEFINE (ly_score_embedded_format, "ly:score-embedded-format",
265 2, 0, 0, (SCM score, SCM paper),
266 "Run @var{score} through @var{paper}, an output definition, "
267 "scaled to correct outputscale already, "
268 "return a list of paper-lines.")
270 Score * sc = unsmob_score (score);
271 Output_def *od = unsmob_output_def (paper);
273 SCM_ASSERT_TYPE (sc, score, SCM_ARG1, __FUNCTION__, "Score");
274 SCM_ASSERT_TYPE (od, paper, SCM_ARG2, __FUNCTION__, "Output_def");
276 Output_def * score_def = 0;
278 /* UGR, FIXME, these are default \paper blocks once again. They
280 for (int i = 0; !score_def && i < sc->defs_.size (); i++)
281 if (sc->defs_[i]->c_variable ("is-paper") == SCM_BOOL_T)
282 score_def = sc->defs_[i];
285 return scm_c_make_vector (0, SCM_EOL);
287 score_def = score_def->clone ();
288 SCM prot = score_def->self_scm ();
289 scm_gc_unprotect_object (prot);
291 /* TODO: SCORE_DEF should be scaled according to OD->parent_ or OD
293 score_def->parent_ = od;
295 SCM context = ly_run_translator (sc->music_, score_def->self_scm ());
296 SCM lines = ly_format_output (context, scm_makfrom0str ("<embedded>"));
298 scm_remember_upto_here_1 (prot);