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