]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-dynamic-performer.cc
(LY_DEFINE): use Scheme style naming for
[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--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "performer.hh"
10
11 #include "event.hh"
12 #include "audio-item.hh"
13
14 /*
15   TODO: fold this into 1 engraver: \< and \> should also stop when
16   absdyn is encountered.
17  */
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   virtual void process_music ();
37   virtual 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   Array<Audio_dynamic_tuple> dynamic_tuples_;
45   Array<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     {
64       last_volume_ = d->volume_;
65     }
66 }
67
68 void
69 Span_dynamic_performer::process_music ()
70 {
71   if (span_start_event_ || span_events_[START])
72     {
73       audio_ = new Audio_dynamic (0);
74       Audio_element_info info (audio_, span_events_[START]
75                                ? span_events_[START]
76                                : span_events_[STOP]);
77       announce_element (info);
78       Audio_dynamic_tuple a = { audio_, now_mom () };
79       dynamic_tuples_.push (a);
80     }
81   
82   if (span_events_[STOP])
83     {
84       if (!span_start_event_)
85         {
86           span_events_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
87           span_events_[STOP] = 0;
88         }
89       else
90         {
91           finished_dir_ = dir_;
92           finished_dynamic_tuples_ = dynamic_tuples_;
93         }
94       dynamic_tuples_.clear ();
95       span_start_event_ = 0;
96     }
97
98   if (span_events_[START])
99     {
100       dir_ =  (span_events_[START]->is_mus_type ("crescendo-event"))
101         ? RIGHT : LEFT;
102       span_start_event_ = span_events_[START];
103       
104       dynamic_tuples_.clear ();
105       Audio_dynamic_tuple a = { audio_, now_mom () };
106       dynamic_tuples_.push (a);
107     }
108
109
110   if (span_events_[STOP])
111     { 
112       finished_dynamic_tuples_.top ().audio_->volume_ = last_volume_;
113     }
114   
115   if (span_events_[START])
116     {
117       dynamic_tuples_[0].audio_->volume_ = last_volume_;
118     }
119   span_start_event_ = 0;
120   span_events_[START] = 0;
121   span_events_[STOP] = 0;
122 }
123
124 void
125 Span_dynamic_performer::stop_translation_timestep ()
126 {
127   if (finished_dynamic_tuples_.size () > 1)
128     {
129       Real start_volume = finished_dynamic_tuples_[0].audio_->volume_;
130       Real dv = finished_dynamic_tuples_.top ().audio_->volume_
131         - start_volume;
132       /*
133         urg.
134         Catch and fix the case of:
135
136              |                         |
137             x|                        x|
138             f cresc.  -- -- -- -- --  pp 
139
140          Actually, we should provide a non-displayed dynamic/volume setting,
141          to set volume to 'ff' just before the pp.
142        */
143       if (!dv || sign (dv) != finished_dir_)
144         {
145           // urg.  20%: about two volume steps
146           dv = (Real)finished_dir_ * 0.2;
147           if (!start_volume)
148             start_volume = finished_dynamic_tuples_.top
149  ().audio_->volume_ - dv;
150         }
151       Moment start_mom = finished_dynamic_tuples_[0].mom_;
152       Moment dt = finished_dynamic_tuples_.top ().mom_ - start_mom;
153       for (int i=0; i < finished_dynamic_tuples_.size (); i++)
154         {
155           Audio_dynamic_tuple* a = &finished_dynamic_tuples_[i];
156           Real volume = start_volume + dv * (Real) (a->mom_ - start_mom).main_part_
157             / (Real)dt.main_part_;
158           a->audio_->volume_ = volume;
159         }
160       finished_dynamic_tuples_.clear ();
161     }
162
163   if (audio_)
164     {
165       play_element (audio_);
166       audio_ = 0;
167     }
168
169   span_events_[STOP] = 0;
170   span_events_[START] = 0;
171
172 }
173
174 bool
175 Span_dynamic_performer::try_music (Music* r)
176 {
177   if (r->is_mus_type ("crescendo-event")
178       || r->is_mus_type ("decrescendo-event"))
179     {
180       Direction d = to_dir (r->get_property ("span-direction"));
181       span_events_[d] = r;
182       return true;
183     }
184   return false;
185 }
186 ENTER_DESCRIPTION (Span_dynamic_performer,
187                    "", "",
188                    "crescendo-event decrescendo-event", 
189                    "", "", "");