]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
2003 -> 2004
[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 "ly-smobs.icc"
12
13 #include "score.hh"
14 #include "warn.hh"
15 #include "music-output-def.hh"
16 #include "music-output.hh"
17 #include "music-iterator.hh"
18 #include "music.hh"
19 #include "global-translator.hh"
20 #include "scm-hash.hh"
21 #include "cpu-timer.hh"
22 #include "main.hh"
23 #include "paper-def.hh"
24 #include "ly-modules.hh"
25
26
27
28 /*
29   TODO: junkme.
30  */
31 Score::Score ()
32   : Input ()
33 {
34   header_ = SCM_EOL;
35   music_ = SCM_EOL;
36
37   smobify_self ();
38 }
39
40 Score::~Score ()
41 {
42   
43 }
44
45
46
47
48 IMPLEMENT_SMOBS (Score);
49 IMPLEMENT_DEFAULT_EQUAL_P (Score);
50
51
52 SCM
53 Score::mark_smob (SCM s)
54 {
55   Score * sc = (Score*) SCM_CELL_WORD_1 (s);
56
57   if (sc->header_)
58     scm_gc_mark (sc->header_);
59   for (int i = sc->defs_.size (); i--;)
60     scm_gc_mark (sc->defs_[i]->self_scm ());
61   
62   return sc->music_;
63 }
64
65 int
66 Score::print_smob (SCM , SCM p, scm_print_state*)
67 {
68   scm_puts ("#<Score>", p);
69
70   return 1;
71 }
72
73
74
75 /*
76   store point & click locations.
77   Global to save some time. (Sue us!)
78  */
79
80 Score::Score (Score const &s)
81   : Input (s)
82 {
83   music_ = SCM_EOL;
84   header_ = 0;
85   smobify_self ();
86
87   Music * m =unsmob_music (s.music_);
88   music_ =  m?m->clone ()->self_scm () : SCM_EOL;
89   scm_gc_unprotect_object (music_);
90   
91   for (int i=0; i < s.defs_.size (); i++)
92     defs_.push (s.defs_[i]->clone ());
93
94   header_ = ly_make_anonymous_module ();
95   if (ly_module_p (s.header_))
96     ly_copy_module_variables (header_, s.header_);
97 }
98
99
100
101 LY_DEFINE(ly_run_translator, "ly:run-translator", 
102           2, 0, 0,
103           (SCM mus, SCM output_def),
104           "Process @var{mus} according to @var{output_def}. A interpretation "
105 "context is set up, and @var{mus} is interpreted with it. The  "
106 "context is returned in its final state." )
107 {
108   Music_output_def *odef = unsmob_music_output_def (output_def);
109   Music * music = unsmob_music (mus);
110
111   SCM_ASSERT_TYPE (music, mus, SCM_ARG1, __FUNCTION__, "Music");
112   SCM_ASSERT_TYPE (odef, output_def, SCM_ARG2, __FUNCTION__, "Output definition");
113   
114   Cpu_timer timer;
115   Global_translator * trans = odef->get_global_translator ();
116   if (!trans)
117     {
118       programming_error ("no toplevel translator");
119       return SCM_BOOL_F;
120     }
121   progress_indication (_ ("Interpreting music..."));
122   
123   trans->final_mom_ = music->get_length ();
124   SCM protected_iter =  Music_iterator::get_static_get_iterator (music);
125   Music_iterator * iter = unsmob_iterator (protected_iter);
126   iter->init_translator (music, trans);
127
128   iter->construct_children ();
129
130   if (! iter->ok ())
131     {
132       warning (_ ("Need music in a score"));
133       return SCM_BOOL_F;        // todo: shoudl throw exception.
134     }
135
136   trans->start ();
137   trans->run_iterator_on_me (iter);
138   iter->quit();
139   scm_remember_upto_here_1 (protected_iter);
140   trans->finish ();
141
142   if (verbose_global_b)
143     progress_indication (_f ("elapsed time: %.2f seconds",  timer.read ()));
144
145   
146   return scm_gc_unprotect_object (trans->self_scm ());
147 }
148
149
150 LY_DEFINE(ly_render_output, "ly:render-output",
151           3,0,0,
152           (SCM context, SCM header, SCM out_filename),
153           "Given a Score context in its final state, calculate the output, "
154           "and  dump the result to @var{out-filename}, using "
155           "@var{header} for the bibliographic information.")
156 {
157   Translator *tr = unsmob_translator (context);
158   Global_translator * gt = dynamic_cast<Global_translator*> (tr);
159   
160   SCM_ASSERT_TYPE(gt, context, SCM_ARG1, __FUNCTION__,
161                   "Score context");
162   SCM_ASSERT_TYPE(ly_module_p(header), header, SCM_ARG2, __FUNCTION__,
163                   "module");
164   SCM_ASSERT_TYPE(gh_string_p (out_filename), out_filename, SCM_ARG3, __FUNCTION__,
165                   "output filename");
166
167   Music_output * output = gt->get_output ();
168
169   output->header_ = header;
170   
171   progress_indication ("\n");
172   output->process (ly_scm2string (out_filename));
173   
174   delete output ;
175
176   return SCM_UNDEFINED ;
177 }
178
179 void
180 default_rendering (SCM mus, SCM outdef, SCM head, SCM outname)
181 {
182   SCM context = ly_run_translator (mus, outdef);
183   
184   if (unsmob_translator (context))
185     ly_render_output (context,  head, outname);
186 }