]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
* lily/paper-score.cc (process): Do not show progress newline.
[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 /*  TODO: junkme.  */
30 Score::Score ()
31   : Input ()
32 {
33   header_ = SCM_EOL;
34   music_ = SCM_EOL;
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 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.")
93 {
94   Output_def *odef = unsmob_output_def (output_def);
95   Music *music = unsmob_music (mus);
96
97   if (!music)
98     return SCM_BOOL_F;
99
100   SCM_ASSERT_TYPE (music, mus, SCM_ARG1, __FUNCTION__, "Music");
101   SCM_ASSERT_TYPE (odef, output_def, SCM_ARG2, __FUNCTION__, "Output definition");
102   
103   Cpu_timer timer;
104   
105   Global_context *trans = new Global_context (odef, music->get_length ());
106   
107   if (!trans)
108     {
109       programming_error ("no toplevel translator");
110       return SCM_BOOL_F;
111     }
112   progress_indication (_ ("Interpreting music... "));
113   
114   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
115   Music_iterator * iter = unsmob_iterator (protected_iter);
116   iter->init_translator (music, trans);
117
118   iter->construct_children ();
119
120   if (!iter->ok ())
121     {
122       warning (_ ("Need music in a score"));
123       /* todo: should throw exception. */
124       return SCM_BOOL_F;
125     }
126
127   trans->run_iterator_on_me (iter);
128   iter->quit ();
129   scm_remember_upto_here_1 (protected_iter);
130   trans->finish ();
131
132   if (verbose_global_b)
133     progress_indication (_f ("elapsed time: %.2f seconds",  timer.read ()));
134   
135   return scm_gc_unprotect_object (trans->self_scm ());
136 }
137
138 LY_DEFINE (ly_format_output, "ly:format-output",
139            2, 0, 0, (SCM context, SCM outname),
140            "Given a Score context in its final state,"
141            "process it and return the (rendered) result.")
142 {
143   Global_context *g = dynamic_cast<Global_context*> (unsmob_context (context));
144   SCM_ASSERT_TYPE (g, context, SCM_ARG1, __FUNCTION__, "Global context");
145   SCM_ASSERT_TYPE (ly_c_string_p (outname), outname, SCM_ARG2, __FUNCTION__, "output filename");
146
147   Music_output *output = g->get_output ();
148   progress_indication ("\n");
149   /* ugh, midi still wants outname  */
150   return output->process (ly_scm2string (outname));
151 }
152
153 void
154 default_rendering (SCM music, SCM outdef,
155                    SCM book_outputdef,
156                    SCM header, SCM outname)
157 {
158   SCM scaled_def = outdef;
159   SCM scaled_bookdef = book_outputdef;
160   
161   Output_def *bpd = unsmob_output_def (book_outputdef);
162
163   /* ugh.  */
164   if (bpd->c_variable ("is-bookpaper") == SCM_BOOL_T)
165     {
166       Real scale = ly_scm2double (bpd->c_variable ("outputscale"));
167       
168       Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
169       scaled_def = def->self_scm ();
170
171       scaled_bookdef = scale_output_def (bpd, scale)->self_scm ();
172       unsmob_output_def (scaled_def)->parent_
173         = unsmob_output_def (scaled_bookdef);
174       
175       scm_gc_unprotect_object (scaled_bookdef);
176       scm_gc_unprotect_object (scaled_def);
177     }
178   
179   SCM context = ly_run_translator (music, scaled_def);
180   if (Global_context *g = dynamic_cast<Global_context*>
181       (unsmob_context (context)))
182     {
183       SCM systems = ly_format_output (context, outname);
184       Music_output *output = g->get_output ();
185       if (systems != SCM_UNDEFINED)
186         {
187           /* ugh, this is strange, Paper_book without a Book object. */
188           Paper_book *paper_book = new Paper_book ();
189           paper_book->header_ = header;
190           paper_book->bookpaper_ = unsmob_output_def (scaled_bookdef);
191           
192           Score_lines sc;
193           sc.lines_ = systems;
194           sc.header_ = header;
195
196           paper_book->score_lines_.push (sc);
197           
198           paper_book->classic_output (ly_scm2string (outname));
199           scm_gc_unprotect_object (paper_book->self_scm ());
200         }
201       delete output;
202     }
203
204   scm_remember_upto_here_1 (scaled_def);
205   scm_remember_upto_here_1 (scaled_bookdef);
206 }
207
208 /*  PAPERBOOK should be scaled already.  */
209 SCM
210 Score::book_rendering (String outname,
211                        Output_def *paperbook,
212                        Output_def *default_def)
213 {
214   SCM scaled_bookdef = SCM_EOL;
215   Real scale = 1.0;
216
217   if (paperbook && paperbook->c_variable ("is-bookpaper") == SCM_BOOL_T)
218     scale = ly_scm2double (paperbook->c_variable ("outputscale"));
219   
220   SCM out = scm_makfrom0str (outname.to_str0 ());
221   SCM systems = SCM_EOL;
222   int outdef_count = defs_.size ();
223   for (int i = 0; !i || i < outdef_count; i++)
224     {
225       Output_def *def = outdef_count ? defs_[i] : default_def;
226       SCM scaled= SCM_EOL;
227       if (def->c_variable ("is-paper") == SCM_BOOL_T)
228         {
229           def = scale_output_def (def, scale);
230           def->parent_ = paperbook;
231           scaled = def->self_scm ();
232           scm_gc_unprotect_object (scaled);
233         }
234
235       /* TODO: fix or junk --no-paper.  */
236       SCM context = ly_run_translator (music_, def->self_scm ());
237       if (dynamic_cast<Global_context*> (unsmob_context (context)))
238         {
239           SCM s = ly_format_output (context, out);
240           if (s != SCM_UNDEFINED)
241             systems = s;
242         }
243
244       scm_remember_upto_here_1 (scaled);
245     }
246   
247   scm_remember_upto_here_1 (scaled_bookdef);
248   return systems;
249 }
250
251 LY_DEFINE (ly_score_bookify, "ly:score-bookify",
252            2, 0, 0,
253            (SCM score_smob, SCM header),
254            "Return @var{score_smob} encapsulated in a Book object. Set "
255            "@var{header} as book level header.")
256 {
257   SCM_ASSERT_TYPE (unsmob_score (score_smob), score_smob, SCM_ARG1, __FUNCTION__, "score_smob");
258   
259   Score *score = unsmob_score (score_smob);
260   Book *book = new Book;
261   book->scores_.push (score);
262   book->header_ = header;
263   scm_gc_unprotect_object (book->self_scm ());
264   return book->self_scm ();
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   SCM_ASSERT_TYPE (sc, score, SCM_ARG1, __FUNCTION__, "Score");
278   SCM_ASSERT_TYPE (od, paper, SCM_ARG2, __FUNCTION__, "Output_def");
279
280   SCM lines = SCM_EOL;
281   Output_def * score_def  = 0;
282
283   /* UGR, FIXME, these are default \paper blocks once again.  They
284      suck. */
285   for (int i = 0; !score_def && i < sc->defs_.size (); i++)
286     if (sc->defs_[i]->c_variable ("is-paper") == SCM_BOOL_T)
287       score_def = sc->defs_[i];
288
289   if (!score_def)
290     return lines;
291       
292   score_def = score_def->clone ();
293   SCM prot = score_def->self_scm ();
294   scm_gc_unprotect_object (prot);
295
296   /* TODO: SCORE_DEF should be scaled according to OD->parent_ or OD
297      itself. */
298   score_def->parent_ = od;
299   
300   SCM context = ly_run_translator (sc->music_, score_def->self_scm ());
301   lines = ly_format_output (context, scm_makfrom0str ("<embedded>"));
302   
303   scm_remember_upto_here_1 (prot);
304   return lines;
305 }