]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-control-function-performer.cc
ff0855d15cf5136807f877a049afb4f870043c96
[lilypond.git] / lily / midi-control-function-performer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2013 by Heikki Tauriainen <g034737@welho.com>.
5   Adapted from performer implementations
6   Copyright (C) 1996--2012 Jan Nieuwenhuizen <janneke@gnu.org>,
7   Han-Wen Nienhyus <hanwen@xs4all.nl> and others.
8
9   LilyPond is free software: you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation, either version 3 of the License, or
12   (at your option) any later version.
13
14   LilyPond is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "performer.hh"
24
25 #include "audio-item.hh"
26 #include "context.hh"
27 #include "dispatcher.hh"
28 #include "international.hh"
29 #include "listener.hh"
30 #include "stream-event.hh"
31
32 #include "translator.icc"
33
34 /**
35    MIDI control function performer.  Announces "set property" events on MIDI
36    context properties.
37 */
38 class Midi_control_function_performer : public Performer
39 {
40 public:
41   TRANSLATOR_DECLARATIONS (Midi_control_function_performer);
42   DECLARE_LISTENER (announce_function_value_change);
43   ~Midi_control_function_performer ();
44
45   void connect_to_context (Context *c);
46   void disconnect_from_context (Context *c);
47 };
48
49 Midi_control_function_performer::Midi_control_function_performer ()
50 {
51 }
52
53 Midi_control_function_performer::~Midi_control_function_performer ()
54 {
55 }
56
57 void
58 Midi_control_function_performer::connect_to_context (Context *c)
59 {
60   c->events_below ()->
61     add_listener (GET_LISTENER (announce_function_value_change),
62                   ly_symbol2scm ("SetProperty"));
63 }
64
65 void
66 Midi_control_function_performer::disconnect_from_context (Context *c)
67 {
68   c->events_below ()->
69     remove_listener (GET_LISTENER (announce_function_value_change),
70                      ly_symbol2scm ("SetProperty"));
71 }
72
73 IMPLEMENT_LISTENER (Midi_control_function_performer,
74                     announce_function_value_change)
75 void
76 Midi_control_function_performer::announce_function_value_change (SCM sev)
77 {
78   Stream_event *ev = unsmob_stream_event (sev);
79   SCM sym = ev->get_property ("symbol");
80   if (!scm_is_symbol (sym))
81     return;
82
83   // Search for a matching context property; if found, check that the value
84   // of the property is within the allowed range, and announce a possible
85   // change in the value of the corresponding control function.
86   string symbol = ly_symbol2string (sym);
87   for (const Audio_control_function_value_change::Context_property *p
88          = Audio_control_function_value_change::context_properties_;
89        p->name_; ++p)
90     {
91       if (symbol == p->name_)
92         {
93           SCM value = ev->get_property ("value");
94           if (scm_is_number (value))
95             {
96               Real val = scm_to_double (value);
97               if (val >= p->range_min_ && val <= p->range_max_)
98                 {
99                   // Normalize the value to the 0.0 to 1.0 range.
100                   val = ((val - p->range_min_)
101                          / (p->range_max_ - p->range_min_));
102                   Audio_control_function_value_change *item
103                     = new Audio_control_function_value_change (p->control_,
104                                                                val);
105                   announce_element (Audio_element_info (item, 0));
106                 }
107               else
108                 ev->origin ()->
109                   warning (_f ("ignoring out-of-range value change for MIDI "
110                                "property `%s'",
111                                p->name_));
112             }
113           break;
114         }
115     }
116 }
117
118 ADD_TRANSLATOR (Midi_control_function_performer,
119                 /* doc */
120                 "",
121
122                 /* create */
123                 "",
124
125                 /* read */
126                 "midiBalance "
127                 "midiPanPosition "
128                 "midiReverbLevel "
129                 "midiChorusLevel ",
130
131                 /* write */
132                 ""
133                );