]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-performer.cc
* flower
[lilypond.git] / lily / staff-performer.cc
1 /*
2   staff-performer.cc -- implement Staff_performer
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "warn.hh"
10 #include "audio-column.hh"
11 #include "audio-item.hh"
12 #include "audio-staff.hh"
13 #include "performer-group-performer.hh"
14 #include "context.hh"
15
16 /** Perform a staff. Individual notes should have their instrument
17     (staff-wide) set, so we override play_element ()
18 */
19 class Staff_performer : public Performer_group_performer
20 {
21 public:
22   TRANSLATOR_DECLARATIONS (Staff_performer);
23   ~Staff_performer ();
24
25   String new_instrument_string ();
26   String instrument_string_;
27
28 protected:
29   virtual void play_element (Audio_element *p);
30   virtual void finalize ();
31   virtual void initialize ();
32   virtual void create_audio_elements ();
33   virtual void stop_translation_timestep ();
34
35 private:
36   Audio_staff *audio_staff_;
37   Audio_instrument *instrument_;
38   Audio_text *instrument_name_;
39   Audio_text *name_;
40   Audio_tempo *tempo_;
41 };
42
43 ADD_TRANSLATOR (Staff_performer, "", "",
44                 "",
45                 "", "", "");
46
47 Staff_performer::Staff_performer ()
48 {
49   audio_staff_ = 0;
50   instrument_ = 0;
51   instrument_name_ = 0;
52   name_ = 0;
53   tempo_ = 0;
54 }
55
56 Staff_performer::~Staff_performer ()
57 {
58 }
59
60 void
61 Staff_performer::initialize ()
62 {
63   audio_staff_ = new Audio_staff;
64   announce_element (Audio_element_info (audio_staff_, 0));
65
66   name_ = new Audio_text (Audio_text::TRACK_NAME, context ()->id_string ());
67   announce_element (Audio_element_info (name_, 0));
68
69   tempo_ = new Audio_tempo (get_tempo ());
70   announce_element (Audio_element_info (tempo_, 0));
71
72   Performer_group_performer::initialize ();
73 }
74
75 void
76 Staff_performer::create_audio_elements ()
77 {
78   String str = new_instrument_string ();
79   if (str.length ())
80     {
81       instrument_name_ = new Audio_text (Audio_text::INSTRUMENT_NAME, str);
82       announce_element (Audio_element_info (instrument_name_, 0));
83       instrument_ = new Audio_instrument (str);
84       announce_element (Audio_element_info (instrument_, 0));
85
86       /*
87         Have to be here before notes arrive into the staff.
88       */
89       play_element (instrument_);
90       play_element (instrument_name_);
91     }
92   Performer_group_performer::create_audio_elements ();
93 }
94
95 void
96 Staff_performer::stop_translation_timestep ()
97 {
98   SCM proc = ly_lily_module_constant ("percussion?");
99
100   SCM drums = scm_call_1 (proc, ly_symbol2scm (instrument_string_.to_str0 ()));
101   audio_staff_->channel_ = (drums == SCM_BOOL_T ? 9 : -1);
102   if (name_)
103     {
104       play_element (name_);
105       name_ = 0;
106     }
107   if (tempo_)
108     {
109       play_element (tempo_);
110       tempo_ = 0;
111     }
112   instrument_name_ = 0;
113   instrument_ = 0;
114   Performer_group_performer::stop_translation_timestep ();
115 }
116
117 void
118 Staff_performer::finalize ()
119 {
120   Performer_group_performer::finalize ();
121   Performer::play_element (audio_staff_);
122   audio_staff_ = 0;
123 }
124
125 String
126 Staff_performer::new_instrument_string ()
127 {
128   // mustn't ask Score for instrument: it will return piano!
129   SCM minstr = get_property ("midiInstrument");
130
131   if (!scm_is_string (minstr)
132       || ly_scm2string (minstr) == instrument_string_)
133     return "";
134
135   instrument_string_ = ly_scm2string (minstr);
136
137   return instrument_string_;
138 }
139
140 void
141 Staff_performer::play_element (Audio_element *p)
142 {
143   if (Audio_item *ai = dynamic_cast<Audio_item *> (p))
144     {
145       audio_staff_->add_audio_item (ai);
146     }
147   Performer::play_element (p);
148 }
149