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