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