]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context-scheme.cc
* lily/ various: Introduce stream events of types Prepare,
[lilypond.git] / lily / global-context-scheme.cc
1 /*
2   global-context-scheme.cc -- implement Global_context bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "cpu-timer.hh"
10 #include "global-context.hh"
11 #include "international.hh"
12 #include "main.hh"
13 #include "music-iterator.hh"
14 #include "music-output.hh"
15 #include "music.hh"
16 #include "object-key.hh"
17 #include "output-def.hh"
18 #include "translator-group.hh"
19 #include "warn.hh"
20
21 LY_DEFINE (ly_format_output, "ly:format-output",
22            1, 0, 0, (SCM context),
23            "Given a Global context in its final state, "
24            "process it and return the @code{Music_output} object in its final state.")
25 {
26   Global_context *g = dynamic_cast<Global_context *> (unsmob_context (context));
27   SCM_ASSERT_TYPE (g, context, SCM_ARG1, __FUNCTION__, "Global context");
28
29   SCM output = g->get_output ();
30   progress_indication ("\n");
31   unsmob_music_output (output)->process ();
32   return output;
33 }
34
35 LY_DEFINE (ly_make_global_translator, "ly:make-global-translator",
36           1, 0, 0, (SCM global),
37           "Create a translator group and connect it to the global context\n"
38           "@var{global}. The translator group is returned.")
39 {
40   Global_context *g = dynamic_cast<Global_context *> (unsmob_context (global));
41   SCM_ASSERT_TYPE (g, global, SCM_ARG1, __FUNCTION__, "Global context");
42
43   Translator_group *tg = new Translator_group ();
44   tg->connect_to_context (g);
45   g->implementation_ = tg;
46
47   return tg->unprotect ();
48 }
49
50 LY_DEFINE (ly_run_translator, "ly:run-translator",
51            2, 1, 0, (SCM mus, SCM output_def, SCM key),
52            "Process @var{mus} according to @var{output_def}. \n"
53            "An interpretation context is set up,\n"
54            "and @var{mus} is interpreted with it.  \n"
55            "The context is returned in its final state.\n"
56            "\n\n"
57            "Optionally, this routine takes an Object-key to\n"
58            "to uniquely identify the Score block containing it.\n")
59 {
60   Output_def *odef = unsmob_output_def (output_def);
61   Music *music = unsmob_music (mus);
62
63   if (!music
64       || !music->get_length ().to_bool ())
65     {
66       warning (_ ("no music found in score"));
67       return SCM_BOOL_F;
68     }
69
70   SCM_ASSERT_TYPE (music, mus, SCM_ARG1, __FUNCTION__, "Music");
71   SCM_ASSERT_TYPE (odef, output_def, SCM_ARG2, __FUNCTION__,
72                    "Output definition");
73
74   Cpu_timer timer;
75
76   Global_context *glob = new Global_context (odef, unsmob_key (key));
77   if (!glob)
78     {
79       programming_error ("Couldn't create Global context");
80       return SCM_BOOL_F;
81     }
82
83   SCM tgs = ly_make_global_translator (glob->self_scm ());
84   Translator_group *tg = unsmob_translator_group (tgs);
85
86   message (_ ("Interpreting music... "));
87
88   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
89   Music_iterator *iter = unsmob_iterator (protected_iter);
90   iter->init_translator (music, glob);
91   iter->construct_children ();
92
93   if (!iter->ok ())
94     {
95       warning (_ ("no music found in score"));
96       /* todo: should throw exception. */
97       return SCM_BOOL_F;
98     }
99
100   glob->run_iterator_on_me (iter);
101   iter->quit ();
102   scm_remember_upto_here_1 (protected_iter);
103   send_stream_event (glob, "Finish", 0, 0);
104
105   if (be_verbose_global)
106     message (_f ("elapsed time: %.2f seconds", timer.read ()));
107
108   return glob->unprotect ();
109 }