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