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