move ControllableDescriptor from libpbd to libardour; add support for describing...
[ardour.git] / libs / surfaces / generic_midi / midicontrollable.h
1 /*
2     Copyright (C) 1998-2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __gm_midicontrollable_h__
21 #define __gm_midicontrollable_h__
22
23 #include <string>
24
25 #include "midi++/types.h"
26
27 #include "pbd/controllable.h"
28 #include "pbd/signals.h"
29 #include "pbd/stateful.h"
30
31 #include "ardour/types.h"
32
33 namespace ARDOUR {
34         class ControllableDescriptor;
35 }
36
37 namespace MIDI {
38         class Channel;
39         class Parser;
40 }
41
42 class GenericMidiControlProtocol;
43
44 namespace ARDOUR {
45         class AsyncMIDIPort;
46 }
47
48 class MIDIControllable : public PBD::Stateful
49 {
50   public:
51         MIDIControllable (GenericMidiControlProtocol *, MIDI::Parser&, PBD::Controllable&, bool momentary);
52         MIDIControllable (GenericMidiControlProtocol *, MIDI::Parser&, bool momentary = false);
53         virtual ~MIDIControllable ();
54
55         int init (const std::string&);
56
57         void rediscover_controllable ();
58         bool bank_relative() const { return _bank_relative; }
59         uint32_t rid() const { return _rid; }
60         std::string what() const { return _what; }
61
62         enum Encoder {
63                 No_enc,
64                 Enc_R,
65                 Enc_L,
66                 Enc_2,
67                 Enc_B,
68         };
69
70         MIDI::byte* write_feedback (MIDI::byte* buf, int32_t& bufsize, bool force = false);
71
72         void midi_rebind (MIDI::channel_t channel=-1);
73         void midi_forget ();
74         void learn_about_external_control ();
75         void stop_learning ();
76         void drop_external_control ();
77
78         bool get_midi_feedback () { return feedback; }
79         void set_midi_feedback (bool val) { feedback = val; }
80
81         int control_to_midi(float val);
82         float midi_to_control(int val);
83
84         bool learned() const { return _learned; }
85
86         Encoder get_encoder() const { return _encoder; }
87         void set_encoder (Encoder val) { _encoder = val; }
88
89         MIDI::Parser& get_parser() { return _parser; }
90         PBD::Controllable* get_controllable() const { return controllable; }
91         void set_controllable (PBD::Controllable*);
92         const std::string& current_uri() const { return _current_uri; }
93
94         ARDOUR::ControllableDescriptor& descriptor() const { return *_descriptor; }
95
96         std::string control_description() const { return _control_description; }
97
98         XMLNode& get_state (void);
99         int set_state (const XMLNode&, int version);
100
101         void bind_midi (MIDI::channel_t, MIDI::eventType, MIDI::byte);
102         void bind_rpn_value (MIDI::channel_t, uint16_t rpn);
103         void bind_nrpn_value (MIDI::channel_t, uint16_t rpn);
104         void bind_rpn_change (MIDI::channel_t, uint16_t rpn);
105         void bind_nrpn_change (MIDI::channel_t, uint16_t rpn);
106
107         MIDI::channel_t get_control_channel () { return control_channel; }
108         MIDI::eventType get_control_type () { return control_type; }
109         MIDI::byte get_control_additional () { return control_additional; }
110
111         int lookup_controllable();
112
113   private:
114
115         int max_value_for_type () const;
116
117         GenericMidiControlProtocol* _surface;
118         PBD::Controllable* controllable;
119         ARDOUR::ControllableDescriptor* _descriptor;
120         std::string     _current_uri;
121         MIDI::Parser&   _parser;
122         bool             setting;
123         int              last_value;
124         float            last_controllable_value;
125         bool            _momentary;
126         bool            _is_gain_controller;
127         bool            _learned;
128         Encoder                 _encoder;
129         int              midi_msg_id;      /* controller ID or note number */
130         PBD::ScopedConnection midi_sense_connection[2];
131         PBD::ScopedConnection midi_learn_connection;
132         PBD::ScopedConnection controllable_death_connection;
133         /** the type of MIDI message that is used for this control */
134         MIDI::eventType  control_type;
135         MIDI::byte       control_additional;
136         MIDI::channel_t  control_channel;
137         std::string     _control_description;
138         int16_t          control_rpn;
139         int16_t          control_nrpn;
140         bool             feedback;
141         uint32_t        _rid;
142         std::string     _what;
143         bool            _bank_relative;
144
145   void drop_controllable (PBD::Controllable*);
146
147         void midi_receiver (MIDI::Parser &p, MIDI::byte *, size_t);
148         void midi_sense_note (MIDI::Parser &, MIDI::EventTwoBytes *, bool is_on);
149         void midi_sense_note_on (MIDI::Parser &p, MIDI::EventTwoBytes *tb);
150         void midi_sense_note_off (MIDI::Parser &p, MIDI::EventTwoBytes *tb);
151         void midi_sense_controller (MIDI::Parser &, MIDI::EventTwoBytes *);
152         void midi_sense_program_change (MIDI::Parser &, MIDI::byte);
153         void midi_sense_pitchbend (MIDI::Parser &, MIDI::pitchbend_t);
154
155         void nrpn_value_change (MIDI::Parser&, uint16_t nrpn, float val);
156         void rpn_value_change (MIDI::Parser&, uint16_t nrpn, float val);
157         void rpn_change (MIDI::Parser&, uint16_t nrpn, int direction);
158         void nrpn_change (MIDI::Parser&, uint16_t nrpn, int direction);
159
160 };
161
162 #endif // __gm_midicontrollable_h__
163