Set the toggled default metadata of parameters.
[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         bool   toggled = false;
161
162         switch((AutomationType)type) {
163         case NullAutomation:
164         case GainAutomation:
165                 max = 2.0f;
166                 normal = 1.0f;
167                 break;
168         case PanAzimuthAutomation:
169                 normal = 0.5f; // there really is no normal but this works for stereo, sort of
170                 break;
171         case PanWidthAutomation:
172                 min = -1.0;
173                 max = 1.0;
174                 normal = 0.0f;
175                 break;
176         case PanElevationAutomation:
177         case PanFrontBackAutomation:
178         case PanLFEAutomation:
179                 break;
180         case RecEnableAutomation:
181                 /* default 0.0 - 1.0 is fine */
182                 toggled = true;
183                 break;
184         case PluginAutomation:
185         case FadeInAutomation:
186         case FadeOutAutomation:
187         case EnvelopeAutomation:
188                 max = 2.0f;
189                 normal = 1.0f;
190                 break;
191         case SoloAutomation:
192         case MuteAutomation:
193                 max = 1.0f;
194                 normal = 0.0f;
195                 toggled = true;
196                 break;
197         case MidiCCAutomation:
198         case MidiPgmChangeAutomation:
199         case MidiChannelPressureAutomation:
200                 Evoral::MIDI::controller_range(min, max, normal); break;
201         case MidiPitchBenderAutomation:
202                 Evoral::MIDI::bender_range(min, max, normal); break;
203         case MidiSystemExclusiveAutomation:
204                 return p;
205         case PluginPropertyAutomation:
206                 return p;
207         }
208
209         p.set_range(type, min, max, normal, toggled);
210         return p;
211 }
212
213 Evoral::Parameter
214 EventTypeMap::new_parameter(const string& str) const
215 {
216         AutomationType p_type    = NullAutomation;
217         uint8_t        p_channel = 0;
218         uint32_t       p_id      = 0;
219
220         if (str == "gain") {
221                 p_type = GainAutomation;
222         } else if (str == "solo") {
223                 p_type = SoloAutomation;
224         } else if (str == "mute") {
225                 p_type = MuteAutomation;
226         } else if (str == "fadein") {
227                 p_type = FadeInAutomation;
228         } else if (str == "fadeout") {
229                 p_type = FadeOutAutomation;
230         } else if (str == "envelope") {
231                 p_type = EnvelopeAutomation;
232         } else if (str == "pan-azimuth") {
233                 p_type = PanAzimuthAutomation;
234         } else if (str == "pan-width") {
235                 p_type = PanWidthAutomation;
236         } else if (str == "pan-elevation") {
237                 p_type = PanElevationAutomation;
238         } else if (str == "pan-frontback") {
239                 p_type = PanFrontBackAutomation;
240         } else if (str == "pan-lfe") {
241                 p_type = PanLFEAutomation;
242         } else if (str.length() > 10 && str.substr(0, 10) == "parameter-") {
243                 p_type = PluginAutomation;
244                 p_id = atoi(str.c_str()+10);
245         } else if (str.length() > 9 && str.substr(0, 9) == "property-") {
246                 p_type = PluginPropertyAutomation;
247                 const char* name = str.c_str() + 9;
248                 if (isdigit(str.c_str()[0])) {
249                         p_id = atoi(name);
250                 } else {
251                         p_id = _uri_map.uri_to_id(name);
252                 }
253         } else if (str.length() > 7 && str.substr(0, 7) == "midicc-") {
254                 p_type = MidiCCAutomation;
255                 uint32_t channel = 0;
256                 sscanf(str.c_str(), "midicc-%d-%d", &channel, &p_id);
257                 assert(channel < 16);
258                 p_channel = channel;
259         } else if (str.length() > 16 && str.substr(0, 16) == "midi-pgm-change-") {
260                 p_type = MidiPgmChangeAutomation;
261                 uint32_t channel = 0;
262                 sscanf(str.c_str(), "midi-pgm-change-%d", &channel);
263                 assert(channel < 16);
264                 p_id = 0;
265                 p_channel = channel;
266         } else if (str.length() > 18 && str.substr(0, 18) == "midi-pitch-bender-") {
267                 p_type = MidiPitchBenderAutomation;
268                 uint32_t channel = 0;
269                 sscanf(str.c_str(), "midi-pitch-bender-%d", &channel);
270                 assert(channel < 16);
271                 p_id = 0;
272                 p_channel = channel;
273         } else if (str.length() > 22 && str.substr(0, 22) == "midi-channel-pressure-") {
274                 p_type = MidiChannelPressureAutomation;
275                 uint32_t channel = 0;
276                 sscanf(str.c_str(), "midi-channel-pressure-%d", &channel);
277                 assert(channel < 16);
278                 p_id = 0;
279                 p_channel = channel;
280         } else {
281                 PBD::warning << "Unknown Parameter '" << str << "'" << endmsg;
282         }
283         
284         return new_parameter(p_type, p_channel, p_id);
285 }
286
287 /** Unique string representation, suitable as an XML property value.
288  * e.g. <AutomationList automation-id="whatthisreturns">
289  */
290 std::string
291 EventTypeMap::to_symbol(const Evoral::Parameter& param) const
292 {
293         AutomationType t = (AutomationType)param.type();
294
295         if (t == GainAutomation) {
296                 return "gain";
297         } else if (t == PanAzimuthAutomation) {
298                 return "pan-azimuth";
299         } else if (t == PanElevationAutomation) {
300                 return "pan-elevation";
301         } else if (t == PanWidthAutomation) {
302                 return "pan-width";
303         } else if (t == PanFrontBackAutomation) {
304                 return "pan-frontback";
305         } else if (t == PanLFEAutomation) {
306                 return "pan-lfe";
307         } else if (t == SoloAutomation) {
308                 return "solo";
309         } else if (t == MuteAutomation) {
310                 return "mute";
311         } else if (t == FadeInAutomation) {
312                 return "fadein";
313         } else if (t == FadeOutAutomation) {
314                 return "fadeout";
315         } else if (t == EnvelopeAutomation) {
316                 return "envelope";
317         } else if (t == PluginAutomation) {
318                 return string_compose("parameter-%1", param.id());
319         } else if (t == PluginPropertyAutomation) {
320                 const char* uri = _uri_map.id_to_uri(param.id());
321                 if (uri) {
322                         return string_compose("property-%1", uri);
323                 } else {
324                         return string_compose("property-%1", param.id());
325                 }
326         } else if (t == MidiCCAutomation) {
327                 return string_compose("midicc-%1-%2", int(param.channel()), param.id());
328         } else if (t == MidiPgmChangeAutomation) {
329                 return string_compose("midi-pgm-change-%1", int(param.channel()));
330         } else if (t == MidiPitchBenderAutomation) {
331                 return string_compose("midi-pitch-bender-%1", int(param.channel()));
332         } else if (t == MidiChannelPressureAutomation) {
333                 return string_compose("midi-channel-pressure-%1", int(param.channel()));
334         } else {
335                 PBD::warning << "Uninitialized Parameter symbol() called." << endmsg;
336                 return "";
337         }
338 }
339
340 } // namespace ARDOUR
341