add support for IP MIDI (multicast MIDI over IP UDP sockets) to ardour and use it...
[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
25 #include "pbd/signals.h"
26
27 #include "midi++/types.h"
28 #include "midi++/parser.h"
29
30 namespace MIDI {
31
32 class Port;
33
34 /** Stateful MIDI channel class.
35  *
36  * This remembers various useful information about the current 'state' of a
37  * MIDI channel (eg current pitch bend value).
38  */
39 class Channel : public PBD::ScopedConnectionList {
40
41   public:
42         Channel (byte channel_number, Port &);
43
44         Port &midi_port()           { return _port; }
45         byte channel()                  { return _channel_number; }
46         byte program()                  { return _program_number; }
47         byte bank()                     { return _bank_number; }
48         byte pressure ()                { return _chanpress; }
49         byte poly_pressure (byte n)     { return _polypress[n]; }
50
51         byte last_note_on () { 
52                 return _last_note_on;
53         }
54         byte last_on_velocity () { 
55                 return _last_on_velocity;
56         }
57         byte last_note_off () { 
58                 return _last_note_off;
59         }
60         byte last_off_velocity () { 
61                 return _last_off_velocity;
62         }
63
64         pitchbend_t pitchbend () { 
65                 return _pitch_bend;
66         }
67
68         controller_value_t controller_value (byte n) { 
69                 return _controller_val[n%128];
70         }
71
72         controller_value_t *controller_addr (byte n) {
73                 return &_controller_val[n%128];
74         }
75
76         void set_controller (byte n, byte val) {
77                 _controller_val[n%128] = val;
78         }
79
80         bool channel_msg (byte id, byte val1, byte val2, timestamp_t timestamp);
81         bool all_notes_off (timestamp_t timestamp) {
82                 return channel_msg (MIDI::controller, 123, 0, timestamp);
83         }
84         
85         bool control (byte id, byte value, timestamp_t timestamp) {
86                 return channel_msg (MIDI::controller, id, value, timestamp);
87         }
88         
89         bool note_on (byte note, byte velocity, timestamp_t timestamp) {
90                 return channel_msg (MIDI::on, note, velocity, timestamp);
91         }
92         
93         bool note_off (byte note, byte velocity, timestamp_t timestamp) {
94                 return channel_msg (MIDI::off, note, velocity, timestamp);
95         }
96         
97         bool aftertouch (byte value, timestamp_t timestamp) {
98                 return channel_msg (MIDI::chanpress, value, 0, timestamp);
99         }
100
101         bool poly_aftertouch (byte note, byte value, timestamp_t timestamp) {
102                 return channel_msg (MIDI::polypress, note, value, timestamp);
103         }
104
105         bool program_change (byte value, timestamp_t timestamp) {
106                 return channel_msg (MIDI::program, value, 0, timestamp);
107         }
108
109         bool pitchbend (byte msb, byte lsb, timestamp_t timestamp) {
110                 return channel_msg (MIDI::pitchbend, lsb, msb, timestamp);
111         }
112
113   protected:
114         friend class Port;
115         void connect_signals ();
116
117   private:
118         Port& _port;
119
120         /* Current channel values */
121         byte               _channel_number;
122         byte               _bank_number;
123         byte               _program_number;
124         byte               _rpn_msb;
125         byte               _rpn_lsb;
126         byte               _nrpn_msb;
127         byte               _nrpn_lsb;
128         byte               _chanpress;
129         byte               _polypress[128];
130         bool               _controller_14bit[128];
131         controller_value_t _controller_val[128];
132         byte               _controller_msb[128];
133         byte               _controller_lsb[128];
134         byte               _last_note_on;
135         byte               _last_on_velocity;
136         byte               _last_note_off;
137         byte               _last_off_velocity;
138         pitchbend_t        _pitch_bend;
139         bool               _omni;
140         bool               _poly;
141         bool               _mono;
142         size_t             _notes_on;
143
144         void reset (timestamp_t timestamp, framecnt_t nframes, bool notes_off = true);
145         
146         void process_note_off (Parser &, EventTwoBytes *);
147         void process_note_on (Parser &, EventTwoBytes *);
148         void process_controller (Parser &, EventTwoBytes *);
149         void process_polypress (Parser &, EventTwoBytes *);
150         void process_program_change (Parser &, byte);
151         void process_chanpress (Parser &, byte);
152         void process_pitchbend (Parser &, pitchbend_t);
153         void process_reset (Parser &);
154 };
155
156 } // namespace MIDI
157
158 #endif // __midichannel_h__
159
160
161
162