ab6fd26f7e4c4d138eba90b0b2cf1b68217441b6
[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 == "mute") {
141                 p_type = MuteAutomation;
142         } else if (str == "fadein") {
143                 p_type = FadeInAutomation;
144         } else if (str == "fadeout") {
145                 p_type = FadeOutAutomation;
146         } else if (str == "envelope") {
147                 p_type = EnvelopeAutomation;
148         } else if (str == "pan-azimuth") {
149                 p_type = PanAzimuthAutomation;
150         } else if (str == "pan-width") {
151                 p_type = PanWidthAutomation;
152         } else if (str == "pan-elevation") {
153                 p_type = PanElevationAutomation;
154         } else if (str == "pan-frontback") {
155                 p_type = PanFrontBackAutomation;
156         } else if (str == "pan-lfe") {
157                 p_type = PanLFEAutomation;
158         } else if (str.length() > 10 && str.substr(0, 10) == "parameter-") {
159                 p_type = PluginAutomation;
160                 p_id = atoi(str.c_str()+10);
161 #ifdef LV2_SUPPORT
162         } else if (str.length() > 9 && str.substr(0, 9) == "property-") {
163                 p_type = PluginPropertyAutomation;
164                 const char* name = str.c_str() + 9;
165                 if (isdigit(str.c_str()[0])) {
166                         p_id = atoi(name);
167                 } else {
168                         p_id = _uri_map->uri_to_id(name);
169                 }
170 #endif
171         } else if (str.length() > 7 && str.substr(0, 7) == "midicc-") {
172                 p_type = MidiCCAutomation;
173                 uint32_t channel = 0;
174                 sscanf(str.c_str(), "midicc-%d-%d", &channel, &p_id);
175                 assert(channel < 16);
176                 p_channel = channel;
177         } else if (str.length() > 16 && str.substr(0, 16) == "midi-pgm-change-") {
178                 p_type = MidiPgmChangeAutomation;
179                 uint32_t channel = 0;
180                 sscanf(str.c_str(), "midi-pgm-change-%d", &channel);
181                 assert(channel < 16);
182                 p_id = 0;
183                 p_channel = channel;
184         } else if (str.length() > 18 && str.substr(0, 18) == "midi-pitch-bender-") {
185                 p_type = MidiPitchBenderAutomation;
186                 uint32_t channel = 0;
187                 sscanf(str.c_str(), "midi-pitch-bender-%d", &channel);
188                 assert(channel < 16);
189                 p_id = 0;
190                 p_channel = channel;
191         } else if (str.length() > 22 && str.substr(0, 22) == "midi-channel-pressure-") {
192                 p_type = MidiChannelPressureAutomation;
193                 uint32_t channel = 0;
194                 sscanf(str.c_str(), "midi-channel-pressure-%d", &channel);
195                 assert(channel < 16);
196                 p_id = 0;
197                 p_channel = channel;
198         } else if (str.length() > 19 && str.substr(0, 19) == "midi-note-pressure-") {
199                 p_type = MidiNotePressureAutomation;
200                 uint32_t channel = 0;
201                 sscanf(str.c_str(), "midi-note-pressure-%d-%d", &channel, &p_id);
202                 assert(channel < 16);
203                 assert(p_id < 127);
204                 p_channel = channel;
205         } else {
206                 PBD::warning << "Unknown Parameter '" << str << "'" << endmsg;
207         }
208
209         return Evoral::Parameter(p_type, p_channel, p_id);
210 }
211
212 /** Unique string representation, suitable as an XML property value.
213  * e.g. <AutomationList automation-id="whatthisreturns">
214  */
215 std::string
216 EventTypeMap::to_symbol(const Evoral::Parameter& param) const
217 {
218         AutomationType t = (AutomationType)param.type();
219
220         if (t == GainAutomation) {
221                 return "gain";
222         } else if (t == TrimAutomation) {
223                 return "trim";
224         } else if (t == PanAzimuthAutomation) {
225                 return "pan-azimuth";
226         } else if (t == PanElevationAutomation) {
227                 return "pan-elevation";
228         } else if (t == PanWidthAutomation) {
229                 return "pan-width";
230         } else if (t == PanFrontBackAutomation) {
231                 return "pan-frontback";
232         } else if (t == PanLFEAutomation) {
233                 return "pan-lfe";
234         } else if (t == SoloAutomation) {
235                 return "solo";
236         } else if (t == MuteAutomation) {
237                 return "mute";
238         } else if (t == FadeInAutomation) {
239                 return "fadein";
240         } else if (t == FadeOutAutomation) {
241                 return "fadeout";
242         } else if (t == EnvelopeAutomation) {
243                 return "envelope";
244         } else if (t == PluginAutomation) {
245                 return string_compose("parameter-%1", param.id());
246 #ifdef LV2_SUPPORT
247         } else if (t == PluginPropertyAutomation) {
248                 const char* uri = _uri_map->id_to_uri(param.id());
249                 if (uri) {
250                         return string_compose("property-%1", uri);
251                 } else {
252                         return string_compose("property-%1", param.id());
253                 }
254 #endif
255         } else if (t == MidiCCAutomation) {
256                 return string_compose("midicc-%1-%2", int(param.channel()), param.id());
257         } else if (t == MidiPgmChangeAutomation) {
258                 return string_compose("midi-pgm-change-%1", int(param.channel()));
259         } else if (t == MidiPitchBenderAutomation) {
260                 return string_compose("midi-pitch-bender-%1", int(param.channel()));
261         } else if (t == MidiChannelPressureAutomation) {
262                 return string_compose("midi-channel-pressure-%1", int(param.channel()));
263         } else if (t == MidiNotePressureAutomation) {
264                 return string_compose("midi-note-pressure-%1-%2", int(param.channel()), param.id());
265         } else {
266                 PBD::warning << "Uninitialized Parameter symbol() called." << endmsg;
267                 return "";
268         }
269 }
270
271 Evoral::ParameterDescriptor
272 EventTypeMap::descriptor(const Evoral::Parameter& param) const
273 {
274         // Found an existing (perhaps custom) descriptor
275         Descriptors::const_iterator d = _descriptors.find(param);
276         if (d != _descriptors.end()) {
277                 return d->second;
278         }
279
280         // Add default descriptor and return that
281         return ARDOUR::ParameterDescriptor(param);
282 }
283
284 void
285 EventTypeMap::set_descriptor(const Evoral::Parameter&           param,
286                              const Evoral::ParameterDescriptor& desc)
287 {
288         _descriptors.insert(std::make_pair(param, desc));
289 }
290
291 } // namespace ARDOUR
292