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