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