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