]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-control-function-performer.cc
Revert "Issue 4550 (2/2) Avoid "using namespace std;" in included files"
[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--2015 by Heikki Tauriainen <g034737@welho.com>.
5   Adapted from performer implementations
6   Copyright (C) 1996--2015 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 using std::string;
35
36 /**
37    MIDI control function performer.  Announces "set property" events on MIDI
38    context properties.
39 */
40 class Midi_control_function_performer : public Performer
41 {
42 public:
43   TRANSLATOR_DECLARATIONS (Midi_control_function_performer);
44   void announce_function_value_change (SCM);
45   ~Midi_control_function_performer ();
46
47   void connect_to_context (Context *c);
48   void disconnect_from_context (Context *c);
49 };
50
51 Midi_control_function_performer::Midi_control_function_performer ()
52 {
53 }
54
55 Midi_control_function_performer::~Midi_control_function_performer ()
56 {
57 }
58
59 void
60 Midi_control_function_performer::connect_to_context (Context *c)
61 {
62   c->events_below ()->
63     add_listener (GET_LISTENER (Midi_control_function_performer, announce_function_value_change),
64                   ly_symbol2scm ("SetProperty"));
65 }
66
67 void
68 Midi_control_function_performer::disconnect_from_context (Context *c)
69 {
70   c->events_below ()->
71     remove_listener (GET_LISTENER (Midi_control_function_performer, announce_function_value_change),
72                      ly_symbol2scm ("SetProperty"));
73 }
74
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                 "midiExpression "
129                 "midiReverbLevel "
130                 "midiChorusLevel ",
131
132                 /* write */
133                 ""
134                );