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