]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context-scheme.cc
* lily/include/main.hh: lose _b hungarian suffixes for global
[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 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "warn.hh"
11 #include "music-output.hh"
12 #include "output-def.hh"
13 #include "music-iterator.hh"
14 #include "music.hh"
15 #include "cpu-timer.hh"
16 #include "global-context.hh"
17 #include "object-key.hh"
18 #include "main.hh"
19
20 LY_DEFINE (ly_format_output, "ly:format-output",
21            2, 0, 0, (SCM context, SCM outname),
22            "Given a Score context in its final state,"
23            "process it and return the (rendered) result.")
24 {
25   Global_context *g = dynamic_cast<Global_context*> (unsmob_context (context));
26   SCM_ASSERT_TYPE (g, context, SCM_ARG1, __FUNCTION__, "Global context");
27   SCM_ASSERT_TYPE (scm_is_string (outname), outname, SCM_ARG2, __FUNCTION__, "output file name");
28
29   Music_output *output = g->get_output ();
30   progress_indication ("\n");
31   /* ugh, midi still wants outname  */
32   return output->process (ly_scm2string (outname));
33 }
34
35
36 LY_DEFINE (ly_run_translator, "ly:run-translator", 
37            2, 1, 0, (SCM mus, SCM output_def, SCM key),
38            "Process @var{mus} according to @var{output_def}. \n"
39            "An interpretation context is set up,\n"
40            "and @var{mus} is interpreted with it.  \n"
41            "The context is returned in its final state.\n"
42
43            "\n\nOptionally, this routine takes an Object-key to\n"
44            "to uniquely identify the Score block containing it.\n")
45 {
46   Output_def *odef = unsmob_output_def (output_def);
47   Music *music = unsmob_music (mus);
48
49   if (!music
50       || !music->get_length ().to_bool ())
51     {
52       warning (_ ("Need music in a score"));
53       return SCM_BOOL_F;
54     }
55   
56   SCM_ASSERT_TYPE (music, mus, SCM_ARG1,
57                    __FUNCTION__, "Music");
58   SCM_ASSERT_TYPE (odef, output_def, SCM_ARG2, __FUNCTION__,
59                    "Output definition");
60   
61   Cpu_timer timer;
62   
63   Global_context *trans = new Global_context (odef, music->get_length (), unsmob_key (key) );
64   if (!trans)
65     {
66       programming_error ("no toplevel translator");
67       return SCM_BOOL_F;
68     }
69
70   progress_indication (_ ("Interpreting music... "));
71   
72   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
73   Music_iterator * iter = unsmob_iterator (protected_iter);
74   iter->init_translator (music, trans);
75
76   iter->construct_children ();
77
78   if (!iter->ok ())
79     {
80       warning (_ ("Need music in a score"));
81       /* todo: should throw exception. */
82       return SCM_BOOL_F;
83     }
84
85   trans->run_iterator_on_me (iter);
86   iter->quit ();
87   scm_remember_upto_here_1 (protected_iter);
88   trans->finish ();
89
90   if (be_verbose_global)
91     progress_indication (_f ("elapsed time: %.2f seconds",  timer.read ()));
92   
93   return scm_gc_unprotect_object (trans->self_scm ());
94 }