]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-performer.cc
Fix some bugs in the dynamic engraver and PostScript backend
[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--2006 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.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
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 acknowledge_audio_element (Audio_element_info info);
30   virtual void finalize ();
31   virtual void initialize ();
32   void process_music ();
33   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 #include "translator.icc"
44
45 ADD_TRANSLATOR (Staff_performer, "", "",
46                 "",
47                 "", "");
48
49 Staff_performer::Staff_performer ()
50 {
51   audio_staff_ = 0;
52   instrument_ = 0;
53   instrument_name_ = 0;
54   name_ = 0;
55   tempo_ = 0;
56 }
57
58 Staff_performer::~Staff_performer ()
59 {
60 }
61
62 void
63 Staff_performer::initialize ()
64 {
65   audio_staff_ = new Audio_staff;
66   name_ = new Audio_text (Audio_text::TRACK_NAME, context ()->id_string ());
67   tempo_ = new Audio_tempo (get_tempo ());
68
69   audio_staff_->add_audio_item (name_);
70   audio_staff_->add_audio_item (tempo_);
71   
72   announce_element (Audio_element_info (audio_staff_, 0));
73   announce_element (Audio_element_info (name_, 0));
74   announce_element (Audio_element_info (tempo_, 0));
75 }
76
77 void
78 Staff_performer::process_music ()
79 {
80   string str = new_instrument_string ();
81   if (str.length ())
82     {
83       instrument_name_ = new Audio_text (Audio_text::INSTRUMENT_NAME, str);
84       announce_element (Audio_element_info (instrument_name_, 0));
85       instrument_ = new Audio_instrument (str);
86       announce_element (Audio_element_info (instrument_, 0));
87
88       audio_staff_->add_audio_item (instrument_);
89       audio_staff_->add_audio_item (instrument_name_);
90      
91       /*
92         Have to be here before notes arrive into the staff.
93       */
94       play_element (instrument_);
95       play_element (instrument_name_);
96     }
97 }
98
99 void
100 Staff_performer::stop_translation_timestep ()
101 {
102   SCM proc = ly_lily_module_constant ("percussion?");
103
104   SCM drums = scm_call_1 (proc, ly_symbol2scm (instrument_string_.c_str ()));
105   audio_staff_->channel_ = (drums == SCM_BOOL_T ? 9 : -1);
106   if (name_)
107     {
108       play_element (name_);
109       name_ = 0;
110     }
111   if (tempo_)
112     {
113       play_element (tempo_);
114       tempo_ = 0;
115     }
116   instrument_name_ = 0;
117   instrument_ = 0;
118 }
119
120 void
121 Staff_performer::finalize ()
122 {
123   Performer::play_element (audio_staff_);
124   audio_staff_ = 0;
125 }
126
127 string
128 Staff_performer::new_instrument_string ()
129 {
130   // mustn't ask Score for instrument: it will return piano!
131   SCM minstr = get_property ("midiInstrument");
132
133   if (!scm_is_string (minstr)
134       || ly_scm2string (minstr) == instrument_string_)
135     return "";
136
137   instrument_string_ = ly_scm2string (minstr);
138
139   return instrument_string_;
140 }
141
142 void
143 Staff_performer::acknowledge_audio_element (Audio_element_info inf)
144 {
145   if (Audio_item *ai = dynamic_cast<Audio_item *> (inf.elem_))
146     audio_staff_->add_audio_item (ai);
147 }
148