]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-dynamic-performer.cc
5754244f61440528ae92693b7e41b23bb8e1f819
[lilypond.git] / lily / span-dynamic-performer.cc
1 /*
2   span-dynamic-performer.cc -- implement Span_dynamic_performer
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "performer.hh"
10 #include "audio-item.hh"
11
12 /*
13   TODO: fold this into 1 engraver: \< and \> should also stop when
14   absdyn is encountered.
15 */
16 struct Audio_dynamic_tuple
17 {
18   Audio_dynamic *audio_;
19   Moment mom_;
20 };
21
22 /**
23    perform span-dynamics
24 */
25 class Span_dynamic_performer : public Performer
26 {
27 public:
28   TRANSLATOR_DECLARATIONS (Span_dynamic_performer);
29
30 protected:
31   virtual bool try_music (Music *);
32   virtual void acknowledge_audio_element (Audio_element_info);
33   virtual void process_music ();
34   virtual void stop_translation_timestep ();
35
36 private:
37   Audio_dynamic *audio_;
38   Real last_volume_;
39   Music *span_start_event_;
40   Drul_array<Music *> span_events_;
41   Array<Audio_dynamic_tuple> dynamic_tuples_;
42   Array<Audio_dynamic_tuple> finished_dynamic_tuples_;
43   Direction dir_;
44   Direction finished_dir_;
45 };
46
47 Span_dynamic_performer::Span_dynamic_performer ()
48 {
49   span_events_[START] = 0;
50   span_events_[STOP] = 0;
51   span_start_event_ = 0;
52   audio_ = 0;
53   last_volume_ = 0;
54 }
55
56 void
57 Span_dynamic_performer::acknowledge_audio_element (Audio_element_info i)
58 {
59   if (Audio_dynamic *d = dynamic_cast<Audio_dynamic *> (i.elem_))
60     {
61       last_volume_ = d->volume_;
62     }
63 }
64
65 void
66 Span_dynamic_performer::process_music ()
67 {
68   if (span_start_event_ || span_events_[START])
69     {
70       audio_ = new Audio_dynamic (0);
71       Audio_element_info info (audio_, span_events_[START]
72                                ? span_events_[START]
73                                : span_events_[STOP]);
74       announce_element (info);
75       Audio_dynamic_tuple a = { audio_, now_mom () };
76       dynamic_tuples_.push (a);
77     }
78
79   if (span_events_[STOP])
80     {
81       if (!span_start_event_)
82         {
83           span_events_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
84           span_events_[STOP] = 0;
85         }
86       else
87         {
88           finished_dir_ = dir_;
89           finished_dynamic_tuples_ = dynamic_tuples_;
90         }
91       dynamic_tuples_.clear ();
92       span_start_event_ = 0;
93     }
94
95   if (span_events_[START])
96     {
97       dir_ = (span_events_[START]->is_mus_type ("crescendo-event"))
98         ? RIGHT : LEFT;
99       span_start_event_ = span_events_[START];
100
101       dynamic_tuples_.clear ();
102       Audio_dynamic_tuple a = { audio_, now_mom () };
103       dynamic_tuples_.push (a);
104     }
105
106   if (span_events_[STOP])
107     {
108       finished_dynamic_tuples_.top ().audio_->volume_ = last_volume_;
109     }
110
111   if (span_events_[START])
112     {
113       dynamic_tuples_[0].audio_->volume_ = last_volume_;
114     }
115
116   span_events_[START] = 0;
117   span_events_[STOP] = 0;
118 }
119
120 void
121 Span_dynamic_performer::stop_translation_timestep ()
122 {
123   if (finished_dynamic_tuples_.size () > 1)
124     {
125       Real start_volume = finished_dynamic_tuples_[0].audio_->volume_;
126       Real dv = finished_dynamic_tuples_.top ().audio_->volume_
127         - start_volume;
128       /*
129         urg.
130         Catch and fix the case of:
131
132         |                         |
133         x|                        x|
134         f cresc.  -- -- -- -- --  pp
135
136         Actually, we should provide a non-displayed dynamic/volume setting,
137         to set volume to 'ff' just before the pp.
138       */
139       if (!dv || sign (dv) != finished_dir_)
140         {
141           // urg.  20%: about two volume steps
142           dv = (Real)finished_dir_ * 0.2;
143           if (!start_volume)
144             start_volume = finished_dynamic_tuples_.top
145               ().audio_->volume_ - dv;
146         }
147       Moment start_mom = finished_dynamic_tuples_[0].mom_;
148       Moment dt = finished_dynamic_tuples_.top ().mom_ - start_mom;
149       for (int i = 0; i < finished_dynamic_tuples_.size (); i++)
150         {
151           Audio_dynamic_tuple *a = &finished_dynamic_tuples_[i];
152           Real volume = start_volume + dv * (Real) (a->mom_ - start_mom).main_part_
153             / (Real)dt.main_part_;
154           a->audio_->volume_ = volume;
155         }
156       finished_dynamic_tuples_.clear ();
157     }
158
159   if (audio_)
160     {
161       play_element (audio_);
162       audio_ = 0;
163     }
164
165   span_events_[STOP] = 0;
166   span_events_[START] = 0;
167 }
168
169 bool
170 Span_dynamic_performer::try_music (Music *r)
171 {
172   if (r->is_mus_type ("crescendo-event")
173       || r->is_mus_type ("decrescendo-event"))
174     {
175       Direction d = to_dir (r->get_property ("span-direction"));
176       span_events_[d] = r;
177       return true;
178     }
179   return false;
180 }
181 ADD_TRANSLATOR (Span_dynamic_performer,
182                 "", "",
183                 "crescendo-event decrescendo-event",
184                 "", "", "");