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