]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
* lily/lexer.ll: change is_string -> ly_c_string_p
[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-context.hh"
20 #include "scm-hash.hh"
21 #include "cpu-timer.hh"
22 #include "main.hh"
23 #include "paper-def.hh"
24 #include "ly-module.hh"
25 #include "paper-book.hh"
26 #include "paper-score.hh"
27
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
47 SCM
48 Score::mark_smob (SCM s)
49 {
50   Score *sc = (Score*) SCM_CELL_WORD_1 (s);
51   if (sc->header_)
52     scm_gc_mark (sc->header_);
53   for (int i = sc->defs_.size (); i--;)
54     scm_gc_mark (sc->defs_[i]->self_scm ());
55   return sc->music_;
56 }
57
58 int
59 Score::print_smob (SCM , SCM p, scm_print_state*)
60 {
61   scm_puts ("#<Score>", p);
62
63   return 1;
64 }
65
66
67
68 /*
69   store point & click locations.
70   Global to save some time. (Sue us!)
71  */
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 ();
91   if (is_module (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   Music_output_def *odef = unsmob_music_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,
111                                                music->get_length ()
112                                                );
113   
114   if (!trans)
115     {
116       programming_error ("no toplevel translator");
117       return SCM_BOOL_F;
118     }
119   progress_indication (_ ("Interpreting music... "));
120   
121   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
122   Music_iterator * iter = unsmob_iterator (protected_iter);
123   iter->init_translator (music, trans);
124
125   iter->construct_children ();
126
127   if (! iter->ok ())
128     {
129       warning (_ ("Need music in a score"));
130       return SCM_BOOL_F;        // todo: should throw exception.
131     }
132
133   trans->run_iterator_on_me (iter);
134   iter->quit ();
135   scm_remember_upto_here_1 (protected_iter);
136   trans->finish ();
137
138   if (verbose_global_b)
139     progress_indication (_f ("elapsed time: %.2f seconds",  timer.read ()));
140
141   
142   return scm_gc_unprotect_object (trans->self_scm ());
143 }
144
145 LY_DEFINE (ly_format_output, "ly:format-output",
146            2, 0, 0, (SCM context, SCM outname),
147            "Given a Score context in its final state,"
148            "process it and return the (rendered) result.")
149 {
150   Global_context *g = dynamic_cast<Global_context*> (unsmob_context (context));
151   SCM_ASSERT_TYPE (g, context, SCM_ARG1, __FUNCTION__, "Global context");
152   SCM_ASSERT_TYPE (ly_c_string_p (outname), outname, SCM_ARG2, __FUNCTION__, "output filename");
153
154   Music_output *output = g->get_output ();
155   progress_indication ("\n");
156   // ugh, midi still wants outname
157   return output->process (ly_scm2string (outname));
158 }
159
160 void
161 default_rendering (SCM music, SCM outdef, SCM header, SCM outname)
162 {
163   SCM context = ly_run_translator (music, outdef);
164
165   if (Global_context *g = dynamic_cast<Global_context*>
166       (unsmob_context (context)))
167     {
168       SCM systems = ly_format_output (context, outname);
169       Music_output *output = g->get_output ();
170       if (systems != SCM_UNDEFINED)
171         {
172           Paper_book *paper_book = new Paper_book ();
173           Paper_score *ps = dynamic_cast<Paper_score*> (output);
174           paper_book->papers_.push (ps->paper_);
175           paper_book->scores_.push (systems);
176           paper_book->headers_.push (header);
177           
178           paper_book->classic_output (ly_scm2string (outname));
179           scm_gc_unprotect_object (paper_book->self_scm ());
180         }
181       delete output;
182     }
183 }
184
185 SCM
186 Score::book_rendering (String outname, Music_output_def *default_def,
187                        Paper_def **paper)
188 {
189   SCM out = scm_makfrom0str (outname.to_str0 ());
190   SCM systems = SCM_EOL;
191   int outdef_count = defs_.size ();
192   for (int i = 0; !i || i < outdef_count; i++)
193     {
194       Music_output_def *def = outdef_count ? defs_[i] : default_def;
195       SCM context = ly_run_translator (music_, def->self_scm ());
196       if (Global_context *g = dynamic_cast<Global_context*>
197           (unsmob_context (context)))
198         {
199           SCM s = ly_format_output (context, out);
200           if (s != SCM_UNDEFINED)
201             {
202               systems = s;
203               /* Ugh. */
204               Music_output *output = g->get_output ();
205               if (Paper_score *ps = dynamic_cast<Paper_score*> (output))
206                 *paper = ps->paper_;
207             }
208         }
209     }
210   return systems;
211 }