]> git.donarmstrong.com Git - lilypond.git/blob - lily/span-dynamic-performer.cc
patch::: 1.3.102.jcn1
[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 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "performer.hh"
10 #include "command-request.hh"
11 #include "musical-request.hh"
12 #include "audio-item.hh"
13
14 struct Audio_dynamic_tuple
15 {
16   Audio_dynamic* audio_l_;
17   Moment mom_;
18 };
19
20 /**
21    perform span-dynamics
22  */
23 class Span_dynamic_performer : public Performer
24 {
25 public:
26   VIRTUAL_COPY_CONS (Translator);
27   
28   Span_dynamic_performer ();
29
30 protected:
31   virtual bool do_try_music (Music*);
32   virtual void acknowledge_element (Audio_element_info);
33   virtual void process_acknowledged ();
34   virtual void do_process_music ();
35   virtual void do_pre_move_processing ();
36   virtual void do_post_move_processing ();
37
38 private:
39   Audio_dynamic* audio_p_;
40   Real last_volume_;
41   Span_req* span_start_req_l_;
42   Drul_array<Span_req*> span_req_l_drul_;
43   Array<Audio_dynamic_tuple> dynamic_tuple_arr_;
44   Array<Audio_dynamic_tuple> finished_dynamic_tuple_arr_;
45   Direction dir_;
46   Direction finished_dir_;
47 };
48
49 ADD_THIS_TRANSLATOR (Span_dynamic_performer);
50
51 Span_dynamic_performer::Span_dynamic_performer ()
52 {
53   span_req_l_drul_[START] = 0;
54   span_req_l_drul_[STOP] = 0;
55   span_start_req_l_ = 0;
56   audio_p_ = 0;
57   last_volume_ = 0;
58 }
59
60 void
61 Span_dynamic_performer::acknowledge_element (Audio_element_info i)
62 {
63   if (Audio_dynamic * d = dynamic_cast <Audio_dynamic*> (i.elem_l_))
64     {
65       last_volume_ = d->volume_;
66     }
67 }
68
69 void
70 Span_dynamic_performer::do_process_music ()
71 {
72   if (span_start_req_l_ || span_req_l_drul_[START])
73     {
74       audio_p_ = new Audio_dynamic (0);
75       Audio_element_info info (audio_p_, span_req_l_drul_[START]
76                                ? span_req_l_drul_[START]
77                                : span_req_l_drul_[STOP]);
78       announce_element (info);
79       Audio_dynamic_tuple a = { audio_p_, now_mom () };
80       dynamic_tuple_arr_.push (a);
81     }
82   
83   if (span_req_l_drul_[STOP])
84     {
85       if (!span_start_req_l_)
86         {
87           span_req_l_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
88           span_req_l_drul_[STOP] = 0;
89         }
90       else
91         {
92           finished_dir_ = dir_;
93           finished_dynamic_tuple_arr_ = dynamic_tuple_arr_;
94         }
95       dynamic_tuple_arr_.clear ();
96       span_start_req_l_ = 0;
97     }
98
99   if (span_req_l_drul_[START])
100     {
101       dir_ = span_req_l_drul_[START]->span_type_str_ == "crescendo"
102         ? RIGHT : LEFT;
103       span_start_req_l_ = span_req_l_drul_[START];
104       
105       dynamic_tuple_arr_.clear ();
106       Audio_dynamic_tuple a = { audio_p_, now_mom () };
107       dynamic_tuple_arr_.push (a);
108     }
109 }
110
111 void
112 Span_dynamic_performer::process_acknowledged ()
113 {
114   if (span_req_l_drul_[STOP])
115    { 
116      finished_dynamic_tuple_arr_.top ().audio_l_->volume_ = last_volume_;
117    }
118
119   if (span_req_l_drul_[START])
120     {
121      dynamic_tuple_arr_[0].audio_l_->volume_ = last_volume_;
122     }
123 }
124   
125 void
126 Span_dynamic_performer::do_pre_move_processing ()
127 {
128   if (finished_dynamic_tuple_arr_.size () > 1)
129     {
130       Real start_volume = finished_dynamic_tuple_arr_[0].audio_l_->volume_;
131       Real dv = finished_dynamic_tuple_arr_.top ().audio_l_->volume_
132         - start_volume;
133       /*
134         urg.
135         Catch and fix the case of:
136
137              |                         |
138             x|                        x|
139             f cresc.  -- -- -- -- --  pp 
140
141          Actually, we should provide a non-displayed dynamic/volume setting,
142          to set volume to 'ff' just before the pp.
143        */
144       if (!dv || sign (dv) != finished_dir_)
145         {
146           // urg.  20%: about two volume steps
147           dv = (Real)finished_dir_ * 0.2;
148           if (!start_volume)
149             start_volume = finished_dynamic_tuple_arr_.top
150               ().audio_l_->volume_ - dv;
151         }
152       Moment start_mom = finished_dynamic_tuple_arr_[0].mom_;
153       Moment dt = finished_dynamic_tuple_arr_.top ().mom_ - start_mom;
154       for (int i=0; i < finished_dynamic_tuple_arr_.size (); i++)
155         {
156           Audio_dynamic_tuple* a = &finished_dynamic_tuple_arr_[i];
157           Real volume = start_volume + dv * (Real)(a->mom_ - start_mom)
158             / (Real)dt;
159           a->audio_l_->volume_ = volume;
160         }
161       finished_dynamic_tuple_arr_.clear ();
162     }
163
164   if (audio_p_)
165     {
166       play_element (audio_p_);
167       audio_p_ = 0;
168     }
169 }
170
171 void
172 Span_dynamic_performer::do_post_move_processing ()
173 {
174   span_req_l_drul_[STOP] = 0;
175   span_req_l_drul_[START] = 0;
176 }
177
178 bool
179 Span_dynamic_performer::do_try_music (Music* r)
180 {
181   if (Span_req * s = dynamic_cast<Span_req*>(r))
182     {
183       if (s-> span_type_str_ == "crescendo"
184           || s->span_type_str_ == "decrescendo")
185         {
186           span_req_l_drul_[s->span_dir_] = s;
187           return true;
188         }
189     }
190   return false;
191 }