Optimize automation-event process splitting
[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 #ifdef LV2_SUPPORT
28 #include "ardour/uri_map.h"
29 #endif
30 #include "evoral/Parameter.hpp"
31 #include "evoral/ParameterDescriptor.hpp"
32 #include "evoral/midi_events.h"
33 #include "pbd/error.h"
34 #include "pbd/compose.h"
35
36 using namespace std;
37
38 namespace ARDOUR {
39
40 EventTypeMap* EventTypeMap::event_type_map;
41
42 EventTypeMap&
43 EventTypeMap::instance()
44 {
45         if (!EventTypeMap::event_type_map) {
46 #ifdef LV2_SUPPORT
47                 EventTypeMap::event_type_map = new EventTypeMap(&URIMap::instance());
48 #else
49                 EventTypeMap::event_type_map = new EventTypeMap(NULL);
50 #endif
51         }
52         return *EventTypeMap::event_type_map;
53 }
54
55 bool
56 EventTypeMap::type_is_midi(uint32_t type) const
57 {
58         return ARDOUR::parameter_is_midi((AutomationType)type);
59 }
60
61 uint8_t
62 EventTypeMap::parameter_midi_type(const Evoral::Parameter& param) const
63 {
64         return ARDOUR::parameter_midi_type((AutomationType)param.type());
65 }
66
67 Evoral::ParameterType
68 EventTypeMap::midi_parameter_type(const uint8_t* buf, uint32_t len) const
69 {
70         return (uint32_t)ARDOUR::midi_parameter_type(buf[0]);
71 }
72
73 Evoral::ControlList::InterpolationStyle
74 EventTypeMap::interpolation_of(const Evoral::Parameter& param)
75 {
76         switch (param.type()) {
77         case MidiCCAutomation:
78                 switch (param.id()) {
79                 case MIDI_CTL_LSB_BANK:
80                 case MIDI_CTL_MSB_BANK:
81                 case MIDI_CTL_LSB_EFFECT1:
82                 case MIDI_CTL_LSB_EFFECT2:
83                 case MIDI_CTL_MSB_EFFECT1:
84                 case MIDI_CTL_MSB_EFFECT2:
85                 case MIDI_CTL_MSB_GENERAL_PURPOSE1:
86                 case MIDI_CTL_MSB_GENERAL_PURPOSE2:
87                 case MIDI_CTL_MSB_GENERAL_PURPOSE3:
88                 case MIDI_CTL_MSB_GENERAL_PURPOSE4:
89                 case MIDI_CTL_SUSTAIN:
90                 case MIDI_CTL_PORTAMENTO:
91                 case MIDI_CTL_SOSTENUTO:
92                 case MIDI_CTL_SOFT_PEDAL:
93                 case MIDI_CTL_LEGATO_FOOTSWITCH:
94                 case MIDI_CTL_HOLD2:
95                 case MIDI_CTL_GENERAL_PURPOSE5:
96                 case MIDI_CTL_GENERAL_PURPOSE6:
97                 case MIDI_CTL_GENERAL_PURPOSE7:
98                 case MIDI_CTL_GENERAL_PURPOSE8:
99                 case MIDI_CTL_DATA_INCREMENT:
100                 case MIDI_CTL_DATA_DECREMENT:
101                 case MIDI_CTL_NONREG_PARM_NUM_LSB:
102                 case MIDI_CTL_NONREG_PARM_NUM_MSB:
103                 case MIDI_CTL_REGIST_PARM_NUM_LSB:
104                 case MIDI_CTL_REGIST_PARM_NUM_MSB:
105                 case MIDI_CTL_ALL_SOUNDS_OFF:
106                 case MIDI_CTL_RESET_CONTROLLERS:
107                 case MIDI_CTL_LOCAL_CONTROL_SWITCH:
108                 case MIDI_CTL_ALL_NOTES_OFF:
109                 case MIDI_CTL_OMNI_OFF:
110                 case MIDI_CTL_OMNI_ON:
111                 case MIDI_CTL_MONO:
112                 case MIDI_CTL_POLY:
113                         return Evoral::ControlList::Discrete; break;
114                 default:
115                         return Evoral::ControlList::Linear; break;
116                 }
117                 break;
118         case MidiPgmChangeAutomation:       return Evoral::ControlList::Discrete; break;
119         case MidiChannelPressureAutomation: return Evoral::ControlList::Linear; break;
120         case MidiNotePressureAutomation:    return Evoral::ControlList::Linear; break;
121         case MidiPitchBenderAutomation:     return Evoral::ControlList::Linear; break;
122         default: assert(false);
123         }
124         return Evoral::ControlList::Linear; // Not reached, suppress warnings
125 }
126
127 Evoral::Parameter
128 EventTypeMap::from_symbol(const string& str) const
129 {
130         AutomationType p_type    = NullAutomation;
131         uint8_t        p_channel = 0;
132         uint32_t       p_id      = 0;
133
134         if (str == "gain") {
135                 p_type = GainAutomation;
136         } else if (str == "trim") {
137                 p_type = TrimAutomation;
138         } else if (str == "solo") {
139                 p_type = SoloAutomation;
140         } else if (str == "solo-iso") {
141                 p_type = SoloIsolateAutomation;
142         } else if (str == "solo-safe") {
143                 p_type = SoloSafeAutomation;
144         } else if (str == "mute") {
145                 p_type = MuteAutomation;
146         } else if (str == "fadein") {
147                 p_type = FadeInAutomation;
148         } else if (str == "fadeout") {
149                 p_type = FadeOutAutomation;
150         } else if (str == "envelope") {
151                 p_type = EnvelopeAutomation;
152         } else if (str == "pan-azimuth") {
153                 p_type = PanAzimuthAutomation;
154         } else if (str == "pan-width") {
155                 p_type = PanWidthAutomation;
156         } else if (str == "pan-elevation") {
157                 p_type = PanElevationAutomation;
158         } else if (str == "pan-frontback") {
159                 p_type = PanFrontBackAutomation;
160         } else if (str == "pan-lfe") {
161                 p_type = PanLFEAutomation;
162         } else if (str == "rec-enable") {
163                 p_type = RecEnableAutomation;
164         } else if (str == "rec-safe") {
165                 p_type = RecSafeAutomation;
166         } else if (str == "phase") {
167                 p_type = PhaseAutomation;
168         } else if (str == "monitor") {
169                 p_type = MonitoringAutomation;
170         } else if (str == "pan-lfe") {
171                 p_type = PanLFEAutomation;
172         } else if (str.length() > 10 && str.substr(0, 10) == "parameter-") {
173                 p_type = PluginAutomation;
174                 p_id = atoi(str.c_str()+10);
175 #ifdef LV2_SUPPORT
176         } else if (str.length() > 9 && str.substr(0, 9) == "property-") {
177                 p_type = PluginPropertyAutomation;
178                 const char* name = str.c_str() + 9;
179                 if (isdigit(str.c_str()[0])) {
180                         p_id = atoi(name);
181                 } else {
182                         p_id = _uri_map->uri_to_id(name);
183                 }
184 #endif
185         } else if (str.length() > 7 && str.substr(0, 7) == "midicc-") {
186                 p_type = MidiCCAutomation;
187                 uint32_t channel = 0;
188                 sscanf(str.c_str(), "midicc-%d-%d", &channel, &p_id);
189                 assert(channel < 16);
190                 p_channel = channel;
191         } else if (str.length() > 16 && str.substr(0, 16) == "midi-pgm-change-") {
192                 p_type = MidiPgmChangeAutomation;
193                 uint32_t channel = 0;
194                 sscanf(str.c_str(), "midi-pgm-change-%d", &channel);
195                 assert(channel < 16);
196                 p_id = 0;
197                 p_channel = channel;
198         } else if (str.length() > 18 && str.substr(0, 18) == "midi-pitch-bender-") {
199                 p_type = MidiPitchBenderAutomation;
200                 uint32_t channel = 0;
201                 sscanf(str.c_str(), "midi-pitch-bender-%d", &channel);
202                 assert(channel < 16);
203                 p_id = 0;
204                 p_channel = channel;
205         } else if (str.length() > 22 && str.substr(0, 22) == "midi-channel-pressure-") {
206                 p_type = MidiChannelPressureAutomation;
207                 uint32_t channel = 0;
208                 sscanf(str.c_str(), "midi-channel-pressure-%d", &channel);
209                 assert(channel < 16);
210                 p_id = 0;
211                 p_channel = channel;
212         } else if (str.length() > 19 && str.substr(0, 19) == "midi-note-pressure-") {
213                 p_type = MidiNotePressureAutomation;
214                 uint32_t channel = 0;
215                 sscanf(str.c_str(), "midi-note-pressure-%d-%d", &channel, &p_id);
216                 assert(channel < 16);
217                 assert(p_id < 127);
218                 p_channel = channel;
219         } else {
220                 PBD::warning << "Unknown Parameter '" << str << "'" << endmsg;
221         }
222
223         return Evoral::Parameter(p_type, p_channel, p_id);
224 }
225
226 /** Unique string representation, suitable as an XML property value.
227  * e.g. <AutomationList automation-id="whatthisreturns">
228  */
229 std::string
230 EventTypeMap::to_symbol(const Evoral::Parameter& param) const
231 {
232         AutomationType t = (AutomationType)param.type();
233
234         if (t == GainAutomation) {
235                 return "gain";
236         } else if (t == TrimAutomation) {
237                 return "trim";
238         } else if (t == PanAzimuthAutomation) {
239                 return "pan-azimuth";
240         } else if (t == PanElevationAutomation) {
241                 return "pan-elevation";
242         } else if (t == PanWidthAutomation) {
243                 return "pan-width";
244         } else if (t == PanFrontBackAutomation) {
245                 return "pan-frontback";
246         } else if (t == PanLFEAutomation) {
247                 return "pan-lfe";
248         } else if (t == SoloAutomation) {
249                 return "solo";
250         } else if (t == MuteAutomation) {
251                 return "mute";
252         } else if (t == FadeInAutomation) {
253                 return "fadein";
254         } else if (t == FadeOutAutomation) {
255                 return "fadeout";
256         } else if (t == EnvelopeAutomation) {
257                 return "envelope";
258         } else if (t == PhaseAutomation) {
259                 return "phase";
260         } else if (t == SoloIsolateAutomation) {
261                 return "solo-iso";
262         } else if (t == SoloSafeAutomation) {
263                 return "solo-safe";
264         } else if (t == MonitoringAutomation) {
265                 return "monitor";
266         } else if (t == RecEnableAutomation) {
267                 return "rec-enable";
268         } else if (t == RecSafeAutomation) {
269                 return "rec-safe";
270         } else if (t == PluginAutomation) {
271                 return std::string("parameter-") + PBD::to_string(param.id());
272 #ifdef LV2_SUPPORT
273         } else if (t == PluginPropertyAutomation) {
274                 const char* uri = _uri_map->id_to_uri(param.id());
275                 if (uri) {
276                         return std::string("property-") + uri;
277                 } else {
278                         return std::string("property-") + PBD::to_string(param.id());
279                 }
280 #endif
281         } else if (t == MidiCCAutomation) {
282                 return std::string("midicc-") + PBD::to_string (param.channel()) + "-" + PBD::to_string (param.id());
283         } else if (t == MidiPgmChangeAutomation) {
284                 return std::string("midi-pgm-change-") + PBD::to_string(param.channel());
285         } else if (t == MidiPitchBenderAutomation) {
286                 return std::string("midi-pitch-bender-") + PBD::to_string(param.channel());
287         } else if (t == MidiChannelPressureAutomation) {
288                 return std::string("midi-channel-pressure-") + PBD::to_string(param.channel());
289         } else if (t == MidiNotePressureAutomation) {
290                 return std::string ("midi-note-pressure-") + PBD::to_string (param.channel()) + "-" + PBD::to_string (param.id());
291         } else {
292                 PBD::warning << "Uninitialized Parameter symbol() called." << endmsg;
293                 return "";
294         }
295 }
296
297 Evoral::ParameterDescriptor
298 EventTypeMap::descriptor(const Evoral::Parameter& param) const
299 {
300         // Found an existing (perhaps custom) descriptor
301         Descriptors::const_iterator d = _descriptors.find(param);
302         if (d != _descriptors.end()) {
303                 return d->second;
304         }
305
306         // Add default descriptor and return that
307         return ARDOUR::ParameterDescriptor(param);
308 }
309
310 void
311 EventTypeMap::set_descriptor(const Evoral::Parameter&           param,
312                              const Evoral::ParameterDescriptor& desc)
313 {
314         _descriptors.insert(std::make_pair(param, desc));
315 }
316
317 } // namespace ARDOUR
318