]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
(ly_module_define): only define variable if
[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 #include "cpu-timer.hh"
13 #include "global-context.hh"
14 #include "ly-module.hh"
15 #include "ly-smobs.icc"
16 #include "main.hh"
17 #include "music-iterator.hh"
18 #include "music-output-def.hh"
19 #include "music-output.hh"
20 #include "music.hh"
21 #include "paper-book.hh"
22 #include "paper-def.hh"
23 #include "paper-score.hh"
24 #include "scm-hash.hh"
25 #include "score.hh"
26 #include "warn.hh"
27
28 /*
29   TODO: junkme.
30  */
31 Score::Score ()
32   : Input ()
33 {
34   header_ = SCM_EOL;
35   music_ = SCM_EOL;
36   smobify_self ();
37 }
38
39 Score::~Score ()
40 {
41 }
42
43 IMPLEMENT_SMOBS (Score);
44 IMPLEMENT_DEFAULT_EQUAL_P (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
66
67 /*
68   store point & click locations.
69   Global to save some time. (Sue us!)
70  */
71
72 Score::Score (Score const &s)
73   : Input (s)
74 {
75   music_ = SCM_EOL;
76
77   // FIXME: SCM_EOL?
78   header_ = 0;
79
80   smobify_self ();
81
82   Music *m =unsmob_music (s.music_);
83   music_ = m ? m->clone ()->self_scm () : SCM_EOL;
84   scm_gc_unprotect_object (music_);
85   
86   for (int i = 0; i < s.defs_.size (); i++)
87     defs_.push (s.defs_[i]->clone ());
88
89   header_ = ly_make_anonymous_module (safe_global_b);
90   if (is_module (s.header_))
91     ly_import_module (header_, s.header_);
92 }
93
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.")
100 {
101   Music_output_def *odef = unsmob_music_output_def (output_def);
102   Music *music = unsmob_music (mus);
103
104   SCM_ASSERT_TYPE (music, mus, SCM_ARG1, __FUNCTION__, "Music");
105   SCM_ASSERT_TYPE (odef, output_def, SCM_ARG2, __FUNCTION__, "Output definition");
106   
107   Cpu_timer timer;
108   
109   Global_context * trans = new Global_context (odef, music->get_length ());
110   
111   if (!trans)
112     {
113       programming_error ("no toplevel translator");
114       return SCM_BOOL_F;
115     }
116   progress_indication (_ ("Interpreting music... "));
117   
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);
121
122   iter->construct_children ();
123
124   if (!iter->ok ())
125     {
126       warning (_ ("Need music in a score"));
127       /* todo: should throw exception. */
128       return SCM_BOOL_F;
129     }
130
131   trans->run_iterator_on_me (iter);
132   iter->quit ();
133   scm_remember_upto_here_1 (protected_iter);
134   trans->finish ();
135
136   if (verbose_global_b)
137     progress_indication (_f ("elapsed time: %.2f seconds",  timer.read ()));
138   
139   return scm_gc_unprotect_object (trans->self_scm ());
140 }
141
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.")
146 {
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");
150
151   Music_output *output = g->get_output ();
152   progress_indication ("\n");
153   // ugh, midi still wants outname
154   return output->process (ly_scm2string (outname));
155 }
156
157 void
158 default_rendering (SCM music, SCM outdef, SCM header, SCM outname)
159 {
160   SCM context = ly_run_translator (music, outdef);
161
162   if (Global_context *g = dynamic_cast<Global_context*>
163       (unsmob_context (context)))
164     {
165       SCM systems = ly_format_output (context, outname);
166       Music_output *output = g->get_output ();
167       if (systems != SCM_UNDEFINED)
168         {
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);
174           
175           paper_book->classic_output (ly_scm2string (outname));
176           scm_gc_unprotect_object (paper_book->self_scm ());
177         }
178       delete output;
179     }
180 }
181
182 SCM
183 Score::book_rendering (String outname, Music_output_def *default_def,
184                        Paper_def **paper)
185 {
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++)
190     {
191       Music_output_def *def = outdef_count ? defs_[i] : default_def;
192       if (!(no_paper_global_b && dynamic_cast<Paper_def*> (def)))
193         {
194           SCM context = ly_run_translator (music_, def->self_scm ());
195           if (Global_context *g = dynamic_cast<Global_context*>
196               (unsmob_context (context)))
197             {
198               SCM s = ly_format_output (context, out);
199               if (s != SCM_UNDEFINED)
200                 {
201                   systems = s;
202                   /* Ugh. */
203                   Music_output *output = g->get_output ();
204                   if (Paper_score *ps = dynamic_cast<Paper_score*> (output))
205                     *paper = ps->paper_;
206                 }
207             }
208         }
209     }
210   return systems;
211 }
212
213 LY_DEFINE (ly_score_bookify, "ly:score-bookify",
214            1, 0, 0,
215            (SCM score_smob),
216            "Return SCORE encapsulated in a BOOK.")
217 {
218   SCM_ASSERT_TYPE (unsmob_score (score_smob), score_smob, SCM_ARG1, __FUNCTION__, "score_smob");
219   
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 ();
225 }