b795067a61e5cd18dc6cfb5023ac089f37fbbc24
[ardour.git] / 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 MIDI {
34         class Channel;
35         class Parser;
36 }
37
38 class GenericMidiControlProtocol;
39
40 namespace ARDOUR {
41         class AsyncMIDIPort;
42 }
43
44 class MIDIControllable : public PBD::Stateful
45 {
46   public:
47         MIDIControllable (GenericMidiControlProtocol *, MIDI::Parser&, PBD::Controllable&, bool momentary);
48         MIDIControllable (GenericMidiControlProtocol *, MIDI::Parser&, bool momentary = false);
49         virtual ~MIDIControllable ();
50
51         int init (const std::string&);
52
53         void rediscover_controllable ();
54         bool bank_relative() const { return _bank_relative; }
55         uint32_t rid() const { return _rid; }
56         std::string what() const { return _what; }
57
58         enum CtlType {
59                 Ctl_Momentary,
60                 Ctl_Toggle,
61                 Ctl_Dial,
62         };
63
64         enum Encoder {
65                 No_enc,
66                 Enc_R,
67                 Enc_L,
68                 Enc_2,
69                 Enc_B,
70         };
71
72         MIDI::byte* write_feedback (MIDI::byte* buf, int32_t& bufsize, bool force = false);
73
74         void midi_rebind (MIDI::channel_t channel=-1);
75         void midi_forget ();
76         void learn_about_external_control ();
77         void stop_learning ();
78         void drop_external_control ();
79
80         int control_to_midi(float val);
81         float midi_to_control(int val);
82
83         bool learned() const { return _learned; }
84
85         CtlType get_ctltype() const { return _ctltype; }
86         void set_ctltype (CtlType val) { _ctltype = val; }
87
88         Encoder get_encoder() const { return _encoder; }
89         void set_encoder (Encoder val) { _encoder = val; }
90
91         MIDI::Parser& get_parser() { return _parser; }
92         PBD::Controllable* get_controllable() const { return controllable; }
93         void set_controllable (PBD::Controllable*);
94         const std::string& current_uri() const { return _current_uri; }
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         std::string     _current_uri;
120         MIDI::Parser&   _parser;
121         bool             setting;
122         int              last_value;
123         int              last_incoming;
124         float            last_controllable_value;
125         bool            _momentary;
126         bool            _is_gain_controller;
127         bool            _learned;
128         CtlType         _ctltype;
129         Encoder                 _encoder;
130         int              midi_msg_id;      /* controller ID or note number */
131         PBD::ScopedConnection midi_sense_connection[2];
132         PBD::ScopedConnection midi_learn_connection;
133         PBD::ScopedConnection controllable_death_connection;
134         /** the type of MIDI message that is used for this control */
135         MIDI::eventType  control_type;
136         MIDI::byte       control_additional;
137         MIDI::channel_t  control_channel;
138         std::string     _control_description;
139         int16_t          control_rpn;
140         int16_t          control_nrpn;
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 #endif // __gm_midicontrollable_h__
162