* make MIDI-specific menu show up on all MIDI lanes
[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         default: return 0;
56         }
57 }
58
59 uint32_t
60 EventTypeMap::midi_event_type(uint8_t status) const
61 {
62         switch (status & 0xF0) {
63         case MIDI_CMD_CONTROL:          return MidiCCAutomation; break;
64         case MIDI_CMD_PGM_CHANGE:       return MidiPgmChangeAutomation; break;
65         case MIDI_CMD_CHANNEL_PRESSURE: return MidiChannelPressureAutomation; break;
66         case MIDI_CMD_BENDER:           return MidiPitchBenderAutomation; break;
67         default: return 0;
68         }
69 }
70
71 bool
72 EventTypeMap::is_integer(const Evoral::Parameter& param) const
73 {
74         return (   param.type() >= MidiCCAutomation
75                         && param.type() <= MidiChannelPressureAutomation);
76 }
77
78 Evoral::Parameter
79 EventTypeMap::new_parameter(uint32_t type, uint8_t channel, uint32_t id) const
80 {
81         Evoral::Parameter p(type, channel, id);
82
83         double min    = 0.0f;
84         double max    = 1.0f;
85         double normal = 0.0f;
86         switch((AutomationType)type) {
87         case NullAutomation:
88         case GainAutomation:
89                 max = 2.0f;
90                 normal = 1.0f;
91                 break;
92         case PanAutomation:
93                 normal = 0.5f;
94                 break;
95         case PluginAutomation:
96         case SoloAutomation:
97         case MuteAutomation:
98         case FadeInAutomation:
99         case FadeOutAutomation:
100         case EnvelopeAutomation:
101                 max = 2.0f;
102                 normal = 1.0f;
103                 break;
104         case MidiCCAutomation:
105         case MidiPgmChangeAutomation:
106         case MidiChannelPressureAutomation:
107                 Evoral::MIDI::controller_range(min, max, normal); break;
108         case MidiPitchBenderAutomation:
109                 Evoral::MIDI::bender_range(min, max, normal); break;
110         }
111         
112         p.set_range(type, min, max, normal);
113         return p;
114 }
115
116 Evoral::Parameter
117 EventTypeMap::new_parameter(const string& str) const
118 {
119         AutomationType p_type    = NullAutomation;
120         uint8_t        p_channel = 0;
121         uint32_t       p_id      = 0;
122
123         if (str == "gain") {
124                 p_type = GainAutomation;
125         } else if (str == "solo") {
126                 p_type = SoloAutomation;
127         } else if (str == "mute") {
128                 p_type = MuteAutomation;
129         } else if (str == "fadein") {
130                 p_type = FadeInAutomation;
131         } else if (str == "fadeout") {
132                 p_type = FadeOutAutomation;
133         } else if (str == "envelope") {
134                 p_type = EnvelopeAutomation;
135         } else if (str == "pan") {
136                 p_type = PanAutomation;
137         } else if (str.length() > 4 && str.substr(0, 4) == "pan-") {
138                 p_type = PanAutomation;
139                 p_id = atoi(str.c_str()+4);
140         } else if (str.length() > 10 && str.substr(0, 10) == "parameter-") {
141                 p_type = PluginAutomation;
142                 p_id = atoi(str.c_str()+10);
143         } else if (str.length() > 7 && str.substr(0, 7) == "midicc-") {
144                 p_type = MidiCCAutomation;
145                 uint32_t channel = 0;
146                 sscanf(str.c_str(), "midicc-%d-%d", &channel, &p_id);
147                 assert(channel < 16);
148                 p_channel = channel;
149         } else if (str.length() > 16 && str.substr(0, 16) == "midi-pgm-change-") {
150                 p_type = MidiPgmChangeAutomation;
151                 uint32_t channel = 0;
152                 sscanf(str.c_str(), "midi-pgm-change-%d", &channel);
153                 assert(channel < 16);
154                 p_id = 0;
155                 p_channel = channel;
156         } else if (str.length() > 18 && str.substr(0, 18) == "midi-pitch-bender-") {
157                 p_type = MidiPitchBenderAutomation;
158                 uint32_t channel = 0;
159                 sscanf(str.c_str(), "midi-pitch-bender-%d", &channel);
160                 assert(channel < 16);
161                 p_id = 0;
162                 p_channel = channel;
163         } else if (str.length() > 24 && str.substr(0, 24) == "midi-channel-pressure-") {
164                 p_type = MidiChannelPressureAutomation;
165                 uint32_t channel = 0;
166                 sscanf(str.c_str(), "midi-channel-pressure-%d", &channel);
167                 assert(channel < 16);
168                 p_id = 0;
169                 p_channel = channel;
170         } else {
171                 PBD::warning << "Unknown Parameter '" << str << "'" << endmsg;
172         }
173
174         return new_parameter(p_type, p_channel, p_id);
175 }
176
177 /** Unique string representation, suitable as an XML property value.
178  * e.g. <AutomationList automation-id="whatthisreturns">
179  */
180 std::string
181 EventTypeMap::to_symbol(const Evoral::Parameter& param) const
182 {
183         AutomationType t = (AutomationType)param.type();
184
185         if (t == GainAutomation) {
186                 return "gain";
187         } else if (t == PanAutomation) {
188                 return string_compose("pan-%1", param.id());
189         } else if (t == SoloAutomation) {
190                 return "solo";
191         } else if (t == MuteAutomation) {
192                 return "mute";
193         } else if (t == FadeInAutomation) {
194                 return "fadein";
195         } else if (t == FadeOutAutomation) {
196                 return "fadeout";
197         } else if (t == EnvelopeAutomation) {
198                 return "envelope";
199         } else if (t == PluginAutomation) {
200                 return string_compose("parameter-%1", param.id());
201         } else if (t == MidiCCAutomation) {
202                 return string_compose("midicc-%1-%2", int(param.channel()), param.id());
203         } else if (t == MidiPgmChangeAutomation) {
204                 return string_compose("midi-pgm-change-%1", int(param.channel()));
205         } else if (t == MidiPitchBenderAutomation) {
206                 return string_compose("midi-pitch-bender-%1", int(param.channel()));
207         } else if (t == MidiChannelPressureAutomation) {
208                 return string_compose("midi-channel-pressure-%1", int(param.channel()));
209         } else {
210                 PBD::warning << "Uninitialized Parameter symbol() called." << endmsg;
211                 return "";
212         }
213 }
214
215 } // namespace ARDOUR
216