cc85cc42c198569f79397b1d7dc662fcc6c6a3b4
[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 <boost/signals2.hpp>
26
27 #include "midi++/types.h"
28 #include "pbd/controllable.h"
29 #include "pbd/stateful.h"
30 #include "ardour/types.h"
31
32 namespace MIDI {
33
34 class Channel;
35 class Port;
36 class Parser;
37
38 }
39
40 class MIDIControllable : public PBD::Stateful
41 {
42   public:
43         MIDIControllable (MIDI::Port&, PBD::Controllable&, bool bistate = false);
44         MIDIControllable (MIDI::Port&, const std::string& uri, bool bistate = false);
45         virtual ~MIDIControllable ();
46
47         bool ok() const { return !_current_uri.empty(); }
48
49         void send_feedback ();
50         MIDI::byte* write_feedback (MIDI::byte* buf, int32_t& bufsize, bool force = false);
51         
52         void midi_rebind (MIDI::channel_t channel=-1);
53         void midi_forget ();
54         void learn_about_external_control ();
55         void stop_learning ();
56         void drop_external_control ();
57
58         bool get_midi_feedback () { return feedback; }
59         void set_midi_feedback (bool val) { feedback = val; }
60
61         float control_to_midi(float val);
62         float midi_to_control(float val);
63
64         MIDI::Port& get_port() const { return _port; }
65         PBD::Controllable* get_controllable() const { return controllable; }
66
67         std::string control_description() const { return _control_description; }
68
69         XMLNode& get_state (void);
70         int set_state (const XMLNode&, int version);
71
72         void bind_midi (MIDI::channel_t, MIDI::eventType, MIDI::byte);
73         MIDI::channel_t get_control_channel () { return control_channel; }
74         MIDI::eventType get_control_type () { return control_type; }
75         MIDI::byte get_control_additional () { return control_additional; }
76         
77   private:
78         PBD::Controllable* controllable;
79         std::string        _current_uri;
80         MIDI::Port&     _port;
81         bool             setting;
82         MIDI::byte       last_value;
83         bool             bistate;
84         int              midi_msg_id;      /* controller ID or note number */
85         boost::signals2::connection midi_sense_connection[2];
86         boost::signals2::connection midi_learn_connection;
87         size_t           connections;
88         MIDI::eventType  control_type;
89         MIDI::byte       control_additional;
90         MIDI::channel_t  control_channel;
91         std::string     _control_description;
92         bool             feedback;
93
94         void init ();
95         void reacquire_controllable ();
96         
97         void midi_receiver (MIDI::Parser &p, MIDI::byte *, size_t);
98         void midi_sense_note (MIDI::Parser &, MIDI::EventTwoBytes *, bool is_on);
99         void midi_sense_note_on (MIDI::Parser &p, MIDI::EventTwoBytes *tb);
100         void midi_sense_note_off (MIDI::Parser &p, MIDI::EventTwoBytes *tb);
101         void midi_sense_controller (MIDI::Parser &, MIDI::EventTwoBytes *);
102         void midi_sense_program_change (MIDI::Parser &, MIDI::byte);
103         void midi_sense_pitchbend (MIDI::Parser &, MIDI::pitchbend_t);
104 };
105
106 #endif // __gm_midicontrollable_h__
107