ControlProtocol doesn't actually need any record of an event loop and doesn't need...
[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 PBD {
34         class ControllableDescriptor;
35 }
36
37 namespace MIDI {
38         class Channel;
39         class Port;
40         class Parser;
41 }
42
43 class GenericMidiControlProtocol;
44
45 class MIDIControllable : public PBD::Stateful
46 {
47   public:
48         MIDIControllable (GenericMidiControlProtocol *, MIDI::Port&, PBD::Controllable&, bool momentary);
49         MIDIControllable (GenericMidiControlProtocol *, MIDI::Port&, bool momentary = false);
50         virtual ~MIDIControllable ();
51
52         int init (const std::string&);
53
54         void rediscover_controllable ();
55         bool bank_relative() const { return _bank_relative; }
56         uint32_t rid() const { return _rid; }
57         std::string what() const { return _what; }
58
59         MIDI::byte* write_feedback (MIDI::byte* buf, int32_t& bufsize, bool force = false);
60         
61         void midi_rebind (MIDI::channel_t channel=-1);
62         void midi_forget ();
63         void learn_about_external_control ();
64         void stop_learning ();
65         void drop_external_control ();
66
67         bool get_midi_feedback () { return feedback; }
68         void set_midi_feedback (bool val) { feedback = val; }
69
70         int control_to_midi(float val);
71         float midi_to_control(int val);
72
73         bool learned() const { return _learned; }
74
75         MIDI::Port& get_port() const { return _port; }
76         PBD::Controllable* get_controllable() const { return controllable; }
77         void set_controllable (PBD::Controllable*);
78         const std::string& current_uri() const { return _current_uri; }
79
80         PBD::ControllableDescriptor& descriptor() const { return *_descriptor; }
81
82         std::string control_description() const { return _control_description; }
83
84         XMLNode& get_state (void);
85         int set_state (const XMLNode&, int version);
86
87         void bind_midi (MIDI::channel_t, MIDI::eventType, MIDI::byte);
88         MIDI::channel_t get_control_channel () { return control_channel; }
89         MIDI::eventType get_control_type () { return control_type; }
90         MIDI::byte get_control_additional () { return control_additional; }
91         
92   private:
93
94         int max_value_for_type () const;
95
96         GenericMidiControlProtocol* _surface;
97         PBD::Controllable* controllable;
98         PBD::ControllableDescriptor* _descriptor;
99         std::string        _current_uri;
100         MIDI::Port&     _port;
101         bool             setting;
102         int              last_value;
103         float            last_controllable_value;
104         bool            _momentary;
105         bool            _is_gain_controller;
106         bool            _learned;
107         int              midi_msg_id;      /* controller ID or note number */
108         PBD::ScopedConnection midi_sense_connection[2];
109         PBD::ScopedConnection midi_learn_connection;
110         /** the type of MIDI message that is used for this control */
111         MIDI::eventType  control_type;
112         MIDI::byte       control_additional;
113         MIDI::channel_t  control_channel;
114         std::string     _control_description;
115         bool             feedback;
116         uint32_t        _rid;
117         std::string     _what;
118         bool            _bank_relative;
119         
120         void midi_receiver (MIDI::Parser &p, MIDI::byte *, size_t);
121         void midi_sense_note (MIDI::Parser &, MIDI::EventTwoBytes *, bool is_on);
122         void midi_sense_note_on (MIDI::Parser &p, MIDI::EventTwoBytes *tb);
123         void midi_sense_note_off (MIDI::Parser &p, MIDI::EventTwoBytes *tb);
124         void midi_sense_controller (MIDI::Parser &, MIDI::EventTwoBytes *);
125         void midi_sense_program_change (MIDI::Parser &, MIDI::byte);
126         void midi_sense_pitchbend (MIDI::Parser &, MIDI::pitchbend_t);
127 };
128
129 #endif // __gm_midicontrollable_h__
130