]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-control-function-performer.cc
Doc: scm - Clarify ly:context-pushpop-property
[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 /**
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   void announce_function_value_change (SCM);
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 (Midi_control_function_performer, 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 (Midi_control_function_performer, announce_function_value_change),
70                      ly_symbol2scm ("SetProperty"));
71 }
72
73 void
74 Midi_control_function_performer::announce_function_value_change (SCM sev)
75 {
76   Stream_event *ev = unsmob<Stream_event> (sev);
77   SCM sym = ev->get_property ("symbol");
78   if (!scm_is_symbol (sym))
79     return;
80
81   // Search for a matching context property; if found, check that the value
82   // of the property is within the allowed range, and announce a possible
83   // change in the value of the corresponding control function.
84   string symbol = ly_symbol2string (sym);
85   for (const Audio_control_function_value_change::Context_property *p
86          = Audio_control_function_value_change::context_properties_;
87        p->name_; ++p)
88     {
89       if (symbol == p->name_)
90         {
91           SCM value = ev->get_property ("value");
92           if (scm_is_number (value))
93             {
94               Real val = scm_to_double (value);
95               if (val >= p->range_min_ && val <= p->range_max_)
96                 {
97                   // Normalize the value to the 0.0 to 1.0 range.
98                   val = ((val - p->range_min_)
99                          / (p->range_max_ - p->range_min_));
100                   Audio_control_function_value_change *item
101                     = new Audio_control_function_value_change (p->control_,
102                                                                val);
103                   announce_element (Audio_element_info (item, 0));
104                 }
105               else
106                 ev->origin ()->
107                   warning (_f ("ignoring out-of-range value change for MIDI "
108                                "property `%s'",
109                                p->name_));
110             }
111           break;
112         }
113     }
114 }
115
116 ADD_TRANSLATOR (Midi_control_function_performer,
117                 /* doc */
118                 "",
119
120                 /* create */
121                 "",
122
123                 /* read */
124                 "midiBalance "
125                 "midiPanPosition "
126                 "midiExpression "
127                 "midiReverbLevel "
128                 "midiChorusLevel ",
129
130                 /* write */
131                 ""
132                );