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