]> git.donarmstrong.com Git - lilypond.git/blob - lily/score-performer.cc
1ab23e096fbe06b8021531b3b40b27353d37e3cb
[lilypond.git] / lily / score-performer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2012 Jan Nieuwenhuizen <janneke@gnu.org>
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 "score-performer.hh"
21
22 #include "audio-column.hh"
23 #include "audio-item.hh"
24 #include "context-def.hh"
25 #include "dispatcher.hh"
26 #include "global-context.hh"
27 #include "performance.hh"
28 #include "midi-stream.hh"
29 #include "output-def.hh"
30 #include "string-convert.hh"
31 #include "warn.hh"
32 #include "audio-staff.hh"
33 #include "audio-item.hh"
34
35 ADD_TRANSLATOR_GROUP (Score_performer,
36                       /* doc */
37                       "",
38
39                       /* create */
40                       "",
41
42                       /* read */
43                       "",
44
45                       /* write */
46                       ""
47                      );
48
49 Score_performer::Score_performer ()
50 {
51   performance_ = 0;
52   skipping_ = false;
53   audio_column_ = 0;
54 }
55
56 Score_performer::~Score_performer ()
57 {
58 }
59
60 void
61 Score_performer::announce_element (Audio_element_info info)
62 {
63   announce_infos_.push_back (info);
64   if (Audio_staff *s = dynamic_cast<Audio_staff *> (info.elem_))
65     {
66       performance_->audio_staffs_.push_back (s);
67     }
68
69   performance_->add_element (info.elem_);
70 }
71
72 void
73 Score_performer::acknowledge_audio_elements ()
74 {
75   for (vsize i = 0; i < announce_infos_.size (); i++)
76     {
77       if (Audio_item *ai = dynamic_cast<Audio_item *> (announce_infos_[i].elem_))
78         audio_column_->add_audio_item (ai);
79     }
80   Performer_group::acknowledge_audio_elements ();
81 }
82
83 void
84 Score_performer::connect_to_context (Context *c)
85 {
86   Performer_group::connect_to_context (c);
87
88   Dispatcher *d = c->get_global_context ()->event_source ();
89   d->add_listener (GET_LISTENER (one_time_step), ly_symbol2scm ("OneTimeStep"));
90   d->add_listener (GET_LISTENER (prepare), ly_symbol2scm ("Prepare"));
91   d->add_listener (GET_LISTENER (finish), ly_symbol2scm ("Finish"));
92 }
93
94 void
95 Score_performer::disconnect_from_context ()
96 {
97   Dispatcher *d = context ()->get_global_context ()->event_source ();
98   d->remove_listener (GET_LISTENER (one_time_step), ly_symbol2scm ("OneTimeStep"));
99   d->remove_listener (GET_LISTENER (prepare), ly_symbol2scm ("Prepare"));
100   d->remove_listener (GET_LISTENER (finish), ly_symbol2scm ("Finish"));
101
102   Performer_group::disconnect_from_context ();
103 }
104
105 IMPLEMENT_LISTENER (Score_performer, prepare);
106 void
107 Score_performer::prepare (SCM sev)
108 {
109   Stream_event *ev = unsmob_stream_event (sev);
110   SCM sm = ev->get_property ("moment");
111   Moment *m = unsmob_moment (sm);
112   audio_column_ = new Audio_column (*m);
113   announce_element (Audio_element_info (audio_column_, 0));
114   precomputed_recurse_over_translators (context (), START_TRANSLATION_TIMESTEP, UP);
115 }
116
117 IMPLEMENT_LISTENER (Score_performer, finish);
118 void
119 Score_performer::finish (SCM)
120 {
121   SCM channel_mapping = context ()->get_property ("midiChannelMapping");
122   bool use_ports = channel_mapping == ly_symbol2scm ("voice");
123   performance_->ports_ = use_ports;
124   recurse_over_translators (context (),
125                             &Translator::finalize,
126                             &Translator_group::finalize,
127                             UP);
128 }
129
130 IMPLEMENT_LISTENER (Score_performer, one_time_step);
131 void
132 Score_performer::one_time_step (SCM)
133 {
134   // audio_column_ can be 0 when prepare has not been called.  The
135   // condition is triggered when Simple_music_iterator implicitly
136   // creates a Score context, like when writing
137   //
138   // \score { { | c4 c c c } \midi { } }
139   //
140   // The same situation happens with the Score_engraver group, but it
141   // would appear not to suffer any bad side effects.
142
143   if (!audio_column_)
144     audio_column_ = new Audio_column (context ()->now_mom ());
145   if (to_boolean (context ()->get_property ("skipTypesetting")))
146     {
147       if (!skipping_)
148         {
149           skip_start_mom_ = audio_column_->when ();
150           skipping_ = true;
151         }
152     }
153   else
154     {
155       if (skipping_)
156         {
157           offset_mom_ -= audio_column_->when () - skip_start_mom_;
158           skipping_ = false;
159         }
160
161       audio_column_->offset_when (offset_mom_);
162       precomputed_recurse_over_translators (context (), PROCESS_MUSIC, UP);
163       do_announces ();
164     }
165
166   precomputed_recurse_over_translators (context (), STOP_TRANSLATION_TIMESTEP, UP);
167 }
168
169 void
170 Score_performer::derived_mark () const
171 {
172   if (performance_)
173     scm_gc_mark (performance_->self_scm ());
174
175   Performer_group::derived_mark ();
176 }
177
178 void
179 Score_performer::initialize ()
180 {
181   performance_ = new Performance;
182   performance_->unprotect ();
183   context ()->set_property ("output", performance_->self_scm ());
184   performance_->midi_ = context ()->get_output_def ();
185
186   Translator_group::initialize ();
187 }