]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context-scheme.cc
Web-ja: update introduction
[lilypond.git] / lily / global-context-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "cpu-timer.hh"
21 #include "global-context.hh"
22 #include "international.hh"
23 #include "main.hh"
24 #include "music-iterator.hh"
25 #include "music-output.hh"
26 #include "music.hh"
27 #include "output-def.hh"
28 #include "translator-group.hh"
29 #include "warn.hh"
30
31 LY_DEFINE (ly_format_output, "ly:format-output",
32            1, 0, 0, (SCM context),
33            "Given a global context in its final state,"
34            " process it and return the @code{Music_output} object"
35            " in its final state.")
36 {
37   Global_context *g = unsmob<Global_context> (context);
38
39   LY_ASSERT_SMOB (Global_context, context, 1);
40
41   SCM output = g->get_output ();
42   progress_indication ("\n");
43
44   if (Music_output *od = unsmob<Music_output> (output))
45     od->process ();
46
47   return output;
48 }
49
50 LY_DEFINE (ly_make_global_translator, "ly:make-global-translator",
51            1, 0, 0, (SCM global),
52            "Create a translator group and connect it to the global context"
53            " @var{global}.  The translator group is returned.")
54 {
55   Global_context *g = unsmob<Global_context> (global);
56   LY_ASSERT_SMOB (Global_context, global, 1);
57
58   Translator_group *tg = new Translator_group ();
59   tg->connect_to_context (g);
60   g->implementation_ = tg;
61
62   return tg->unprotect ();
63 }
64
65 LY_DEFINE (ly_make_global_context, "ly:make-global-context",
66            1, 0, 0, (SCM output_def),
67            "Set up a global interpretation context, using the output"
68            " block @var{output-def}.  The context is returned.")
69 {
70   LY_ASSERT_SMOB (Output_def, output_def, 1);
71   Output_def *odef = unsmob<Output_def> (output_def);
72
73   Global_context *glob = new Global_context (odef);
74
75   if (!glob)
76     {
77       programming_error ("no toplevel translator");
78       return SCM_BOOL_F;
79     }
80
81   return glob->unprotect ();
82 }
83
84 LY_DEFINE (ly_interpret_music_expression, "ly:interpret-music-expression",
85            2, 0, 0, (SCM mus, SCM ctx),
86            "Interpret the music expression @var{mus} in the global context"
87            " @var{ctx}.  The context is returned in its final state.")
88 {
89   LY_ASSERT_SMOB (Music, mus, 1);
90   LY_ASSERT_SMOB (Global_context, ctx, 2);
91
92   Music *music = unsmob<Music> (mus);
93   if (!music)
94     {
95       warning (_ ("no music found in score"));
96       return SCM_BOOL_F;
97     }
98
99   Global_context *g = unsmob<Global_context> (ctx);
100
101   Cpu_timer timer;
102
103   message (_ ("Interpreting music..."));
104
105   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
106   Music_iterator *iter = unsmob<Music_iterator> (protected_iter);
107
108   iter->init_context (music, g);
109   iter->construct_children ();
110
111   if (!iter->ok ())
112     {
113       warning (_ ("no music found in score"));
114       /* todo: should throw exception. */
115       return SCM_BOOL_F;
116     }
117
118   g->run_iterator_on_me (iter);
119
120   iter->quit ();
121   scm_remember_upto_here_1 (protected_iter);
122
123   send_stream_event (g, "Finish", 0);
124
125   debug_output (_f ("elapsed time: %.2f seconds", timer.read ()));
126
127   return ctx;
128 }
129
130 LY_DEFINE (ly_run_translator, "ly:run-translator",
131            2, 1, 0, (SCM mus, SCM output_def),
132            "Process @var{mus} according to @var{output-def}.  An"
133            " interpretation context is set up, and @var{mus} is"
134            " interpreted with it.  The context is returned in its"
135            " final state.\n"
136            "\n"
137            "Optionally, this routine takes an object-key to"
138            " to uniquely identify the score block containing it.")
139 {
140   LY_ASSERT_SMOB (Music, mus, 1);
141   LY_ASSERT_SMOB (Output_def, output_def, 2);
142
143   SCM glob = ly_make_global_context (output_def);
144   ly_make_global_translator (glob);
145   ly_interpret_music_expression (mus, glob);
146   return glob;
147 }