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