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