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