]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-performer.cc
release: 1.3.118
[lilypond.git] / lily / dynamic-performer.cc
1 /*
2   dynamic-performer.cc -- implement 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 /*
15   TODO:
16     handle multiple requests
17  */
18
19 /**
20    perform absolute (text) dynamics
21  */
22 class Dynamic_performer : public Performer
23 {
24 public:
25   VIRTUAL_COPY_CONS (Translator);
26   
27   Dynamic_performer ();
28
29 protected:
30   virtual bool try_music (Music* req_l);
31   virtual void stop_translation_timestep ();
32   virtual void create_audio_elements ();
33
34 private:
35   Music* script_req_l_;
36   Audio_dynamic* audio_p_;
37 };
38
39 ADD_THIS_TRANSLATOR (Dynamic_performer);
40
41 Dynamic_performer::Dynamic_performer ()
42 {
43   script_req_l_ = 0;
44   audio_p_ = 0;
45 }
46
47 void
48 Dynamic_performer::create_audio_elements ()
49 {
50   if (script_req_l_)
51     {
52       SCM proc = get_property ("dynamicAbsoluteVolumeFunction");
53
54       SCM svolume  = SCM_EOL;
55       if (gh_procedure_p (proc))
56         {
57           // urg
58           svolume = gh_call1 (proc, script_req_l_->get_mus_property ("text"));
59         }
60
61       Real volume = 0.5; 
62       if (gh_number_p (svolume))
63         volume = gh_scm2double (svolume);
64
65       /*
66         properties override default equaliser setting
67        */
68       SCM min = get_property ("midiMinimumVolume");
69       SCM max = get_property ("midiMaximumVolume");
70       if (gh_number_p (min) || gh_number_p (max))
71         {
72           Interval iv (0, 1);
73           if (gh_number_p (min))
74             iv[MIN] = gh_scm2double (min);
75           if (gh_number_p (max))
76             iv[MAX] = gh_scm2double (max);
77           volume = iv[MIN] + iv.length () * volume;
78         }
79       else
80         {
81           /*
82             urg, code duplication:: staff_performer
83           */
84           SCM s = get_property ("midiInstrument");
85           
86           if (!gh_string_p(s))
87             s = get_property ("instrument");
88           
89           if (!gh_string_p(s))
90             s = ly_str02scm ("piano");
91           
92           
93           SCM eq = get_property ("instrumentEqualizer");
94           if (gh_procedure_p (eq))
95             {
96               s = gh_call1 (eq, s);
97             }
98
99           if (gh_pair_p (s))
100             {
101               Interval iv;
102               iv[MIN] = gh_scm2double (gh_car (s));
103               iv[MAX] = gh_scm2double (gh_cdr (s));
104               volume = iv[MIN] + iv.length () * volume;
105             }
106         }
107       
108       audio_p_ = new Audio_dynamic (volume);
109       Audio_element_info info (audio_p_, script_req_l_);
110       announce_element (info);
111       script_req_l_ = 0;
112     }
113 }
114
115 void
116 Dynamic_performer::stop_translation_timestep ()
117 {
118   if (audio_p_)
119     {
120       play_element (audio_p_);
121       audio_p_ = 0;
122     }
123 }
124
125 bool
126 Dynamic_performer::try_music (Music* r)
127 {
128   if (!script_req_l_)
129     {
130       if( dynamic_cast <Text_script_req*> (r)
131           && r->get_mus_property ("text-type") == ly_symbol2scm ("dynamic"))
132         {
133           script_req_l_ = r;
134           return true;
135         }
136     }
137   return false;
138 }
139