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>
12 #include "cpu-timer.hh"
13 #include "global-context.hh"
14 #include "ly-module.hh"
15 #include "ly-smobs.icc"
17 #include "music-iterator.hh"
18 #include "music-output-def.hh"
19 #include "music-output.hh"
21 #include "paper-book.hh"
22 #include "paper-def.hh"
23 #include "paper-score.hh"
24 #include "scm-hash.hh"
43 IMPLEMENT_SMOBS (Score);
44 IMPLEMENT_DEFAULT_EQUAL_P (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);
68 store point & click locations.
69 Global to save some time. (Sue us!)
72 Score::Score (Score const &s)
82 Music *m =unsmob_music (s.music_);
83 music_ = m ? m->clone ()->self_scm () : SCM_EOL;
84 scm_gc_unprotect_object (music_);
86 for (int i = 0; i < s.defs_.size (); i++)
87 defs_.push (s.defs_[i]->clone ());
89 header_ = ly_make_anonymous_module ();
90 if (is_module (s.header_))
91 ly_import_module (header_, s.header_);
94 LY_DEFINE (ly_run_translator, "ly:run-translator",
95 2, 0, 0, (SCM mus, SCM output_def),
96 "Process @var{mus} according to @var{output_def}. "
97 "An interpretation context is set up, "
98 "and @var{mus} is interpreted with it. "
99 "The context is returned in its final state.")
101 Music_output_def *odef = unsmob_music_output_def (output_def);
102 Music *music = unsmob_music (mus);
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 (ly_c_string_p (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, SCM header, SCM outname)
160 SCM context = ly_run_translator (music, outdef);
162 if (Global_context *g = dynamic_cast<Global_context*>
163 (unsmob_context (context)))
165 SCM systems = ly_format_output (context, outname);
166 Music_output *output = g->get_output ();
167 if (systems != SCM_UNDEFINED)
169 Paper_book *paper_book = new Paper_book ();
170 Paper_score *ps = dynamic_cast<Paper_score*> (output);
171 paper_book->papers_.push (ps->paper_);
172 paper_book->scores_.push (systems);
173 paper_book->headers_.push (header);
175 paper_book->classic_output (ly_scm2string (outname));
176 scm_gc_unprotect_object (paper_book->self_scm ());
183 Score::book_rendering (String outname, Music_output_def *default_def,
186 SCM out = scm_makfrom0str (outname.to_str0 ());
187 SCM systems = SCM_EOL;
188 int outdef_count = defs_.size ();
189 for (int i = 0; !i || i < outdef_count; i++)
191 Music_output_def *def = outdef_count ? defs_[i] : default_def;
192 SCM context = ly_run_translator (music_, def->self_scm ());
193 if (Global_context *g = dynamic_cast<Global_context*>
194 (unsmob_context (context)))
196 SCM s = ly_format_output (context, out);
197 if (s != SCM_UNDEFINED)
201 Music_output *output = g->get_output ();
202 if (Paper_score *ps = dynamic_cast<Paper_score*> (output))
210 LY_DEFINE (ly_score_bookify, "ly:score-bookify",
213 "Return SCORE encapsulated in a BOOK.")
216 SCM_ASSERT_TYPE (ly_c_parser_p (parser_smob), parser_smobd, SCM_ARG1, __FUNCTION__, "parser_smob");
217 SCM_ASSERT_TYPE (ly_c_score_p (score_smob), score_smob, SCM_ARG1, __FUNCTION__, "score_smob");
220 Score *score = unsmob_score (score_smob);
221 Book *book = new Book;
222 book->scores_.push (score);
223 scm_gc_unprotect_object (book->self_scm ());
224 return book->self_scm ();