]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-dynamic-performer.cc
1b11fba6e7d637422d9ecc0b488fa4c558e08ffb
[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
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   virtual void process_music ();
35   virtual 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     {
62       last_volume_ = d->volume_;
63     }
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 (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 (a);
105     }
106
107   if (span_events_[STOP])
108     { 
109       finished_dynamic_tuples_.top ().audio_->volume_ = last_volume_;
110     }
111   
112   if (span_events_[START])
113     {
114       dynamic_tuples_[0].audio_->volume_ = last_volume_;
115     }
116
117   span_events_[START] = 0;
118   span_events_[STOP] = 0;
119 }
120
121 void
122 Span_dynamic_performer::stop_translation_timestep ()
123 {
124   if (finished_dynamic_tuples_.size () > 1)
125     {
126       Real start_volume = finished_dynamic_tuples_[0].audio_->volume_;
127       Real dv = finished_dynamic_tuples_.top ().audio_->volume_
128         - start_volume;
129       /*
130         urg.
131         Catch and fix the case of:
132
133              |                         |
134             x|                        x|
135             f cresc.  -- -- -- -- --  pp 
136
137          Actually, we should provide a non-displayed dynamic/volume setting,
138          to set volume to 'ff' just before the pp.
139        */
140       if (!dv || sign (dv) != finished_dir_)
141         {
142           // urg.  20%: about two volume steps
143           dv = (Real)finished_dir_ * 0.2;
144           if (!start_volume)
145             start_volume = finished_dynamic_tuples_.top
146  ().audio_->volume_ - dv;
147         }
148       Moment start_mom = finished_dynamic_tuples_[0].mom_;
149       Moment dt = finished_dynamic_tuples_.top ().mom_ - start_mom;
150       for (int i = 0; i < finished_dynamic_tuples_.size (); i++)
151         {
152           Audio_dynamic_tuple* a = &finished_dynamic_tuples_[i];
153           Real volume = start_volume + dv * (Real) (a->mom_ - start_mom).main_part_
154             / (Real)dt.main_part_;
155           a->audio_->volume_ = volume;
156         }
157       finished_dynamic_tuples_.clear ();
158     }
159
160   if (audio_)
161     {
162       play_element (audio_);
163       audio_ = 0;
164     }
165
166   span_events_[STOP] = 0;
167   span_events_[START] = 0;
168
169 }
170
171 bool
172 Span_dynamic_performer::try_music (Music* r)
173 {
174   if (r->is_mus_type ("crescendo-event")
175       || r->is_mus_type ("decrescendo-event"))
176     {
177       Direction d = to_dir (r->get_property ("span-direction"));
178       span_events_[d] = r;
179       return true;
180     }
181   return false;
182 }
183 ADD_TRANSLATOR (Span_dynamic_performer,
184                    "", "",
185                    "crescendo-event decrescendo-event", 
186                    "", "", "");