Replace half-baked param metadata with descriptor.
[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/ParameterDescriptor.hpp"
29 #include "evoral/midi_events.h"
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         return *EventTypeMap::event_type_map;
46 }
47
48 bool
49 EventTypeMap::type_is_midi(uint32_t type) const
50 {
51         return ARDOUR::parameter_is_midi((AutomationType)type);
52 }
53
54 uint8_t
55 EventTypeMap::parameter_midi_type(const Evoral::Parameter& param) const
56 {
57         return ARDOUR::parameter_midi_type((AutomationType)param.type());
58 }
59
60 uint32_t
61 EventTypeMap::midi_event_type(uint8_t status) const
62 {
63         return (uint32_t)ARDOUR::midi_parameter_type(status);
64 }
65
66 Evoral::ControlList::InterpolationStyle
67 EventTypeMap::interpolation_of(const Evoral::Parameter& param)
68 {
69         switch (param.type()) {
70         case MidiCCAutomation:
71                 switch (param.id()) {
72                 case MIDI_CTL_LSB_BANK:
73                 case MIDI_CTL_MSB_BANK:
74                 case MIDI_CTL_LSB_EFFECT1:
75                 case MIDI_CTL_LSB_EFFECT2:
76                 case MIDI_CTL_MSB_EFFECT1:
77                 case MIDI_CTL_MSB_EFFECT2:
78                 case MIDI_CTL_MSB_GENERAL_PURPOSE1:
79                 case MIDI_CTL_MSB_GENERAL_PURPOSE2:
80                 case MIDI_CTL_MSB_GENERAL_PURPOSE3:
81                 case MIDI_CTL_MSB_GENERAL_PURPOSE4:
82                 case MIDI_CTL_SUSTAIN:
83                 case MIDI_CTL_PORTAMENTO:
84                 case MIDI_CTL_SOSTENUTO:
85                 case MIDI_CTL_SOFT_PEDAL:
86                 case MIDI_CTL_LEGATO_FOOTSWITCH:
87                 case MIDI_CTL_HOLD2:
88                 case MIDI_CTL_GENERAL_PURPOSE5:
89                 case MIDI_CTL_GENERAL_PURPOSE6:
90                 case MIDI_CTL_GENERAL_PURPOSE7:
91                 case MIDI_CTL_GENERAL_PURPOSE8:
92                 case MIDI_CTL_DATA_INCREMENT:
93                 case MIDI_CTL_DATA_DECREMENT:
94                 case MIDI_CTL_NONREG_PARM_NUM_LSB:
95                 case MIDI_CTL_NONREG_PARM_NUM_MSB:
96                 case MIDI_CTL_REGIST_PARM_NUM_LSB:
97                 case MIDI_CTL_REGIST_PARM_NUM_MSB:
98                 case MIDI_CTL_ALL_SOUNDS_OFF:
99                 case MIDI_CTL_RESET_CONTROLLERS:
100                 case MIDI_CTL_LOCAL_CONTROL_SWITCH:
101                 case MIDI_CTL_ALL_NOTES_OFF:
102                 case MIDI_CTL_OMNI_OFF:
103                 case MIDI_CTL_OMNI_ON:
104                 case MIDI_CTL_MONO:
105                 case MIDI_CTL_POLY:
106                         return Evoral::ControlList::Discrete; break;
107                 default:
108                         return Evoral::ControlList::Linear; break;
109                 }
110                 break;
111         case MidiPgmChangeAutomation:       return Evoral::ControlList::Discrete; break;
112         case MidiChannelPressureAutomation: return Evoral::ControlList::Linear; break;
113         case MidiPitchBenderAutomation:     return Evoral::ControlList::Linear; break;
114         default: assert(false);
115         }
116         return Evoral::ControlList::Linear; // Not reached, suppress warnings
117 }
118
119 Evoral::Parameter
120 EventTypeMap::from_symbol(const string& str) const
121 {
122         AutomationType p_type    = NullAutomation;
123         uint8_t        p_channel = 0;
124         uint32_t       p_id      = 0;
125
126         if (str == "gain") {
127                 p_type = GainAutomation;
128         } else if (str == "solo") {
129                 p_type = SoloAutomation;
130         } else if (str == "mute") {
131                 p_type = MuteAutomation;
132         } else if (str == "fadein") {
133                 p_type = FadeInAutomation;
134         } else if (str == "fadeout") {
135                 p_type = FadeOutAutomation;
136         } else if (str == "envelope") {
137                 p_type = EnvelopeAutomation;
138         } else if (str == "pan-azimuth") {
139                 p_type = PanAzimuthAutomation;
140         } else if (str == "pan-width") {
141                 p_type = PanWidthAutomation;
142         } else if (str == "pan-elevation") {
143                 p_type = PanElevationAutomation;
144         } else if (str == "pan-frontback") {
145                 p_type = PanFrontBackAutomation;
146         } else if (str == "pan-lfe") {
147                 p_type = PanLFEAutomation;
148         } else if (str.length() > 10 && str.substr(0, 10) == "parameter-") {
149                 p_type = PluginAutomation;
150                 p_id = atoi(str.c_str()+10);
151         } else if (str.length() > 9 && str.substr(0, 9) == "property-") {
152                 p_type = PluginPropertyAutomation;
153                 const char* name = str.c_str() + 9;
154                 if (isdigit(str.c_str()[0])) {
155                         p_id = atoi(name);
156                 } else {
157                         p_id = _uri_map.uri_to_id(name);
158                 }
159         } else if (str.length() > 7 && str.substr(0, 7) == "midicc-") {
160                 p_type = MidiCCAutomation;
161                 uint32_t channel = 0;
162                 sscanf(str.c_str(), "midicc-%d-%d", &channel, &p_id);
163                 assert(channel < 16);
164                 p_channel = channel;
165         } else if (str.length() > 16 && str.substr(0, 16) == "midi-pgm-change-") {
166                 p_type = MidiPgmChangeAutomation;
167                 uint32_t channel = 0;
168                 sscanf(str.c_str(), "midi-pgm-change-%d", &channel);
169                 assert(channel < 16);
170                 p_id = 0;
171                 p_channel = channel;
172         } else if (str.length() > 18 && str.substr(0, 18) == "midi-pitch-bender-") {
173                 p_type = MidiPitchBenderAutomation;
174                 uint32_t channel = 0;
175                 sscanf(str.c_str(), "midi-pitch-bender-%d", &channel);
176                 assert(channel < 16);
177                 p_id = 0;
178                 p_channel = channel;
179         } else if (str.length() > 22 && str.substr(0, 22) == "midi-channel-pressure-") {
180                 p_type = MidiChannelPressureAutomation;
181                 uint32_t channel = 0;
182                 sscanf(str.c_str(), "midi-channel-pressure-%d", &channel);
183                 assert(channel < 16);
184                 p_id = 0;
185                 p_channel = channel;
186         } else {
187                 PBD::warning << "Unknown Parameter '" << str << "'" << endmsg;
188         }
189         
190         return Evoral::Parameter(p_type, p_channel, p_id);
191 }
192
193 /** Unique string representation, suitable as an XML property value.
194  * e.g. <AutomationList automation-id="whatthisreturns">
195  */
196 std::string
197 EventTypeMap::to_symbol(const Evoral::Parameter& param) const
198 {
199         AutomationType t = (AutomationType)param.type();
200
201         if (t == GainAutomation) {
202                 return "gain";
203         } else if (t == PanAzimuthAutomation) {
204                 return "pan-azimuth";
205         } else if (t == PanElevationAutomation) {
206                 return "pan-elevation";
207         } else if (t == PanWidthAutomation) {
208                 return "pan-width";
209         } else if (t == PanFrontBackAutomation) {
210                 return "pan-frontback";
211         } else if (t == PanLFEAutomation) {
212                 return "pan-lfe";
213         } else if (t == SoloAutomation) {
214                 return "solo";
215         } else if (t == MuteAutomation) {
216                 return "mute";
217         } else if (t == FadeInAutomation) {
218                 return "fadein";
219         } else if (t == FadeOutAutomation) {
220                 return "fadeout";
221         } else if (t == EnvelopeAutomation) {
222                 return "envelope";
223         } else if (t == PluginAutomation) {
224                 return string_compose("parameter-%1", param.id());
225         } else if (t == PluginPropertyAutomation) {
226                 const char* uri = _uri_map.id_to_uri(param.id());
227                 if (uri) {
228                         return string_compose("property-%1", uri);
229                 } else {
230                         return string_compose("property-%1", param.id());
231                 }
232         } else if (t == MidiCCAutomation) {
233                 return string_compose("midicc-%1-%2", int(param.channel()), param.id());
234         } else if (t == MidiPgmChangeAutomation) {
235                 return string_compose("midi-pgm-change-%1", int(param.channel()));
236         } else if (t == MidiPitchBenderAutomation) {
237                 return string_compose("midi-pitch-bender-%1", int(param.channel()));
238         } else if (t == MidiChannelPressureAutomation) {
239                 return string_compose("midi-channel-pressure-%1", int(param.channel()));
240         } else {
241                 PBD::warning << "Uninitialized Parameter symbol() called." << endmsg;
242                 return "";
243         }
244 }
245
246 const Evoral::ParameterDescriptor&
247 EventTypeMap::descriptor(const Evoral::Parameter& param) const
248 {
249         static const Evoral::ParameterDescriptor nil;
250
251         Descriptors::const_iterator d = _descriptors.find(param);
252         return (d != _descriptors.end()) ? d->second : nil;
253 }
254
255 void
256 EventTypeMap::set_descriptor(const Evoral::Parameter&           param,
257                              const Evoral::ParameterDescriptor& desc)
258 {
259         _descriptors.insert(std::make_pair(param, desc));
260 }
261
262 } // namespace ARDOUR
263