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