Only show user-presets in favorite sidebar
[ardour.git] / libs / midi++2 / midi++ / channel.h
1 /*
2     Copyright (C) 1998-99 Paul Barton-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 __midichannel_h__
21 #define __midichannel_h__
22
23 #include <queue>
24 #include <map>
25
26 #include "pbd/signals.h"
27 #include "midi++/parser.h"
28
29 namespace MIDI {
30
31 class Port;
32
33 /** Stateful MIDI channel class.
34  *
35  * This remembers various useful information about the current 'state' of a
36  * MIDI channel (eg current pitch bend value).
37  */
38 class LIBMIDIPP_API Channel : public PBD::ScopedConnectionList {
39
40   public:
41         Channel (byte channel_number, Port &);
42
43         Port &midi_port()           { return _port; }
44         byte channel()                  { return _channel_number; }
45         byte program()                  { return _program_number; }
46         unsigned short bank()           { return _bank_number; }
47         byte pressure ()                { return _chanpress; }
48         byte poly_pressure (byte n)     { return _polypress[n]; }
49
50         byte last_note_on () {
51                 return _last_note_on;
52         }
53         byte last_on_velocity () {
54                 return _last_on_velocity;
55         }
56         byte last_note_off () {
57                 return _last_note_off;
58         }
59         byte last_off_velocity () {
60                 return _last_off_velocity;
61         }
62
63         pitchbend_t pitchbend () {
64                 return _pitch_bend;
65         }
66
67         controller_value_t controller_value (byte n) {
68                 return _controller_val[n%128];
69         }
70
71         controller_value_t *controller_addr (byte n) {
72                 return &_controller_val[n%128];
73         }
74
75         void set_controller (byte n, byte val) {
76                 _controller_val[n%128] = val;
77         }
78
79         controller_value_t rpn_value (uint16_t rpn_id);
80         controller_value_t nrpn_value (uint16_t rpn_id);
81
82         bool channel_msg (byte id, byte val1, byte val2, timestamp_t timestamp);
83         bool all_notes_off (timestamp_t timestamp) {
84                 return channel_msg (MIDI::controller, 123, 0, timestamp);
85         }
86
87         bool control (byte id, byte value, timestamp_t timestamp) {
88                 return channel_msg (MIDI::controller, id, value, timestamp);
89         }
90
91         bool note_on (byte note, byte velocity, timestamp_t timestamp) {
92                 return channel_msg (MIDI::on, note, velocity, timestamp);
93         }
94
95         bool note_off (byte note, byte velocity, timestamp_t timestamp) {
96                 return channel_msg (MIDI::off, note, velocity, timestamp);
97         }
98
99         bool aftertouch (byte value, timestamp_t timestamp) {
100                 return channel_msg (MIDI::chanpress, value, 0, timestamp);
101         }
102
103         bool poly_aftertouch (byte note, byte value, timestamp_t timestamp) {
104                 return channel_msg (MIDI::polypress, note, value, timestamp);
105         }
106
107         bool program_change (byte value, timestamp_t timestamp) {
108                 return channel_msg (MIDI::program, value, 0, timestamp);
109         }
110
111         bool pitchbend (byte msb, byte lsb, timestamp_t timestamp) {
112                 return channel_msg (MIDI::pitchbend, lsb, msb, timestamp);
113         }
114
115         float rpn_value (uint16_t rpn) const;
116         float nrpn_value (uint16_t nrpn) const;
117         float rpn_value_absolute (uint16_t rpn) const;
118         float nrpn_value_absolute (uint16_t nrpn) const;
119
120   protected:
121         friend class Port;
122         void connect_signals ();
123
124   private:
125         Port& _port;
126
127         enum RPNState {
128                 HaveLSB = 0x1,
129                 HaveMSB = 0x2,
130                 HaveValue = 0x4
131         };
132
133         /* Current channel values */
134         byte               _channel_number;
135         unsigned short     _bank_number;
136         byte               _program_number;
137         byte               _rpn_msb;
138         byte               _rpn_lsb;
139         byte               _rpn_val_msb;
140         byte               _rpn_val_lsb;
141         byte               _nrpn_msb;
142         byte               _nrpn_lsb;
143         byte               _nrpn_val_lsb;
144         byte               _nrpn_val_msb;
145         RPNState           _rpn_state;
146         RPNState           _nrpn_state;
147         byte               _chanpress;
148         byte               _polypress[128];
149         bool               _controller_14bit[128];
150         controller_value_t _controller_val[128];
151         byte               _controller_msb[128];
152         byte               _controller_lsb[128];
153         byte               _last_note_on;
154         byte               _last_on_velocity;
155         byte               _last_note_off;
156         byte               _last_off_velocity;
157         pitchbend_t        _pitch_bend;
158         bool               _omni;
159         bool               _poly;
160         bool               _mono;
161         size_t             _notes_on;
162
163         typedef std::map<uint16_t,float> RPNList;
164
165         RPNList rpns;
166         RPNList nrpns;
167
168         void reset (timestamp_t timestamp, samplecnt_t nframes, bool notes_off = true);
169
170         void process_note_off (Parser &, EventTwoBytes *);
171         void process_note_on (Parser &, EventTwoBytes *);
172         void process_controller (Parser &, EventTwoBytes *);
173         void process_polypress (Parser &, EventTwoBytes *);
174         void process_program_change (Parser &, byte);
175         void process_chanpress (Parser &, byte);
176         void process_pitchbend (Parser &, pitchbend_t);
177         void process_reset (Parser &);
178         bool maybe_process_rpns (Parser&, EventTwoBytes *);
179
180         void rpn_reset ();
181         void nrpn_reset ();
182
183         static const RPNState RPN_READY_FOR_VALUE;
184         static const RPNState RPN_VALUE_READY;
185
186 };
187
188 } // namespace MIDI
189
190 #endif // __midichannel_h__
191
192
193
194