Fix lost MIDI events due to broken range check.
[ardour.git] / libs / ardour / event_type_map.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: David Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <ctype.h>
22 #include <cstdio>
23 #include "ardour/types.h"
24 #include "ardour/event_type_map.h"
25 #include "ardour/parameter_descriptor.h"
26 #include "ardour/parameter_types.h"
27 #include "ardour/uri_map.h"
28 #include "evoral/Parameter.hpp"
29 #include "evoral/ParameterDescriptor.hpp"
30 #include "evoral/midi_events.h"
31 #include "pbd/error.h"
32 #include "pbd/compose.h"
33
34 using namespace std;
35
36 namespace ARDOUR {
37
38 EventTypeMap* EventTypeMap::event_type_map;
39
40 EventTypeMap&
41 EventTypeMap::instance()
42 {
43         if (!EventTypeMap::event_type_map) {
44                 EventTypeMap::event_type_map = new EventTypeMap(URIMap::instance());
45         }
46         return *EventTypeMap::event_type_map;
47 }
48
49 bool
50 EventTypeMap::type_is_midi(uint32_t type) const
51 {
52         return ARDOUR::parameter_is_midi((AutomationType)type);
53 }
54
55 uint8_t
56 EventTypeMap::parameter_midi_type(const Evoral::Parameter& param) const
57 {
58         return ARDOUR::parameter_midi_type((AutomationType)param.type());
59 }
60
61 uint32_t
62 EventTypeMap::midi_event_type(uint8_t status) const
63 {
64         return (uint32_t)ARDOUR::midi_parameter_type(status);
65 }
66
67 Evoral::ControlList::InterpolationStyle
68 EventTypeMap::interpolation_of(const Evoral::Parameter& param)
69 {
70         switch (param.type()) {
71         case MidiCCAutomation:
72                 switch (param.id()) {
73                 case MIDI_CTL_LSB_BANK:
74                 case MIDI_CTL_MSB_BANK:
75                 case MIDI_CTL_LSB_EFFECT1:
76                 case MIDI_CTL_LSB_EFFECT2:
77                 case MIDI_CTL_MSB_EFFECT1:
78                 case MIDI_CTL_MSB_EFFECT2:
79                 case MIDI_CTL_MSB_GENERAL_PURPOSE1:
80                 case MIDI_CTL_MSB_GENERAL_PURPOSE2:
81                 case MIDI_CTL_MSB_GENERAL_PURPOSE3:
82                 case MIDI_CTL_MSB_GENERAL_PURPOSE4:
83                 case MIDI_CTL_SUSTAIN:
84                 case MIDI_CTL_PORTAMENTO:
85                 case MIDI_CTL_SOSTENUTO:
86                 case MIDI_CTL_SOFT_PEDAL:
87                 case MIDI_CTL_LEGATO_FOOTSWITCH:
88                 case MIDI_CTL_HOLD2:
89                 case MIDI_CTL_GENERAL_PURPOSE5:
90                 case MIDI_CTL_GENERAL_PURPOSE6:
91                 case MIDI_CTL_GENERAL_PURPOSE7:
92                 case MIDI_CTL_GENERAL_PURPOSE8:
93                 case MIDI_CTL_DATA_INCREMENT:
94                 case MIDI_CTL_DATA_DECREMENT:
95                 case MIDI_CTL_NONREG_PARM_NUM_LSB:
96                 case MIDI_CTL_NONREG_PARM_NUM_MSB:
97                 case MIDI_CTL_REGIST_PARM_NUM_LSB:
98                 case MIDI_CTL_REGIST_PARM_NUM_MSB:
99                 case MIDI_CTL_ALL_SOUNDS_OFF:
100                 case MIDI_CTL_RESET_CONTROLLERS:
101                 case MIDI_CTL_LOCAL_CONTROL_SWITCH:
102                 case MIDI_CTL_ALL_NOTES_OFF:
103                 case MIDI_CTL_OMNI_OFF:
104                 case MIDI_CTL_OMNI_ON:
105                 case MIDI_CTL_MONO:
106                 case MIDI_CTL_POLY:
107                         return Evoral::ControlList::Discrete; break;
108                 default:
109                         return Evoral::ControlList::Linear; break;
110                 }
111                 break;
112         case MidiPgmChangeAutomation:       return Evoral::ControlList::Discrete; break;
113         case MidiChannelPressureAutomation: return Evoral::ControlList::Linear; break;
114         case MidiPitchBenderAutomation:     return Evoral::ControlList::Linear; break;
115         default: assert(false);
116         }
117         return Evoral::ControlList::Linear; // Not reached, suppress warnings
118 }
119
120 Evoral::Parameter
121 EventTypeMap::from_symbol(const string& str) const
122 {
123         AutomationType p_type    = NullAutomation;
124         uint8_t        p_channel = 0;
125         uint32_t       p_id      = 0;
126
127         if (str == "gain") {
128                 p_type = GainAutomation;
129         } else if (str == "solo") {
130                 p_type = SoloAutomation;
131         } else if (str == "mute") {
132                 p_type = MuteAutomation;
133         } else if (str == "fadein") {
134                 p_type = FadeInAutomation;
135         } else if (str == "fadeout") {
136                 p_type = FadeOutAutomation;
137         } else if (str == "envelope") {
138                 p_type = EnvelopeAutomation;
139         } else if (str == "pan-azimuth") {
140                 p_type = PanAzimuthAutomation;
141         } else if (str == "pan-width") {
142                 p_type = PanWidthAutomation;
143         } else if (str == "pan-elevation") {
144                 p_type = PanElevationAutomation;
145         } else if (str == "pan-frontback") {
146                 p_type = PanFrontBackAutomation;
147         } else if (str == "pan-lfe") {
148                 p_type = PanLFEAutomation;
149         } else if (str.length() > 10 && str.substr(0, 10) == "parameter-") {
150                 p_type = PluginAutomation;
151                 p_id = atoi(str.c_str()+10);
152         } else if (str.length() > 9 && str.substr(0, 9) == "property-") {
153                 p_type = PluginPropertyAutomation;
154                 const char* name = str.c_str() + 9;
155                 if (isdigit(str.c_str()[0])) {
156                         p_id = atoi(name);
157                 } else {
158                         p_id = _uri_map.uri_to_id(name);
159                 }
160         } else if (str.length() > 7 && str.substr(0, 7) == "midicc-") {
161                 p_type = MidiCCAutomation;
162                 uint32_t channel = 0;
163                 sscanf(str.c_str(), "midicc-%d-%d", &channel, &p_id);
164                 assert(channel < 16);
165                 p_channel = channel;
166         } else if (str.length() > 16 && str.substr(0, 16) == "midi-pgm-change-") {
167                 p_type = MidiPgmChangeAutomation;
168                 uint32_t channel = 0;
169                 sscanf(str.c_str(), "midi-pgm-change-%d", &channel);
170                 assert(channel < 16);
171                 p_id = 0;
172                 p_channel = channel;
173         } else if (str.length() > 18 && str.substr(0, 18) == "midi-pitch-bender-") {
174                 p_type = MidiPitchBenderAutomation;
175                 uint32_t channel = 0;
176                 sscanf(str.c_str(), "midi-pitch-bender-%d", &channel);
177                 assert(channel < 16);
178                 p_id = 0;
179                 p_channel = channel;
180         } else if (str.length() > 22 && str.substr(0, 22) == "midi-channel-pressure-") {
181                 p_type = MidiChannelPressureAutomation;
182                 uint32_t channel = 0;
183                 sscanf(str.c_str(), "midi-channel-pressure-%d", &channel);
184                 assert(channel < 16);
185                 p_id = 0;
186                 p_channel = channel;
187         } else {
188                 PBD::warning << "Unknown Parameter '" << str << "'" << endmsg;
189         }
190         
191         return Evoral::Parameter(p_type, p_channel, p_id);
192 }
193
194 /** Unique string representation, suitable as an XML property value.
195  * e.g. <AutomationList automation-id="whatthisreturns">
196  */
197 std::string
198 EventTypeMap::to_symbol(const Evoral::Parameter& param) const
199 {
200         AutomationType t = (AutomationType)param.type();
201
202         if (t == GainAutomation) {
203                 return "gain";
204         } else if (t == PanAzimuthAutomation) {
205                 return "pan-azimuth";
206         } else if (t == PanElevationAutomation) {
207                 return "pan-elevation";
208         } else if (t == PanWidthAutomation) {
209                 return "pan-width";
210         } else if (t == PanFrontBackAutomation) {
211                 return "pan-frontback";
212         } else if (t == PanLFEAutomation) {
213                 return "pan-lfe";
214         } else if (t == SoloAutomation) {
215                 return "solo";
216         } else if (t == MuteAutomation) {
217                 return "mute";
218         } else if (t == FadeInAutomation) {
219                 return "fadein";
220         } else if (t == FadeOutAutomation) {
221                 return "fadeout";
222         } else if (t == EnvelopeAutomation) {
223                 return "envelope";
224         } else if (t == PluginAutomation) {
225                 return string_compose("parameter-%1", param.id());
226         } else if (t == PluginPropertyAutomation) {
227                 const char* uri = _uri_map.id_to_uri(param.id());
228                 if (uri) {
229                         return string_compose("property-%1", uri);
230                 } else {
231                         return string_compose("property-%1", param.id());
232                 }
233         } else if (t == MidiCCAutomation) {
234                 return string_compose("midicc-%1-%2", int(param.channel()), param.id());
235         } else if (t == MidiPgmChangeAutomation) {
236                 return string_compose("midi-pgm-change-%1", int(param.channel()));
237         } else if (t == MidiPitchBenderAutomation) {
238                 return string_compose("midi-pitch-bender-%1", int(param.channel()));
239         } else if (t == MidiChannelPressureAutomation) {
240                 return string_compose("midi-channel-pressure-%1", int(param.channel()));
241         } else {
242                 PBD::warning << "Uninitialized Parameter symbol() called." << endmsg;
243                 return "";
244         }
245 }
246
247 Evoral::ParameterDescriptor
248 EventTypeMap::descriptor(const Evoral::Parameter& param) const
249 {
250         // Found an existing (perhaps custom) descriptor
251         Descriptors::const_iterator d = _descriptors.find(param);
252         if (d != _descriptors.end()) {
253                 return d->second;
254         }
255
256         // Add default descriptor and return that
257         return ARDOUR::ParameterDescriptor(param);
258 }
259
260 void
261 EventTypeMap::set_descriptor(const Evoral::Parameter&           param,
262                              const Evoral::ParameterDescriptor& desc)
263 {
264         _descriptors.insert(std::make_pair(param, desc));
265 }
266
267 } // namespace ARDOUR
268