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