AU: install latency listener
[ardour.git] / libs / ardour / ardour / midi_channel_filter.h
1 /*
2     Copyright (C) 2006-2015 Paul Davis
3     Author: David Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __ardour_channel_filter_h__
21 #define __ardour_channel_filter_h__
22
23 #include <stdint.h>
24
25 #include <glib.h>
26
27 #include "ardour/types.h"
28 #include "pbd/signals.h"
29
30 namespace ARDOUR
31 {
32
33 class BufferSet;
34
35 /** Filter/mapper for MIDI channels.
36  *
37  * Channel mapping is configured by setting a mode and "mask", where the
38  * meaning of the mask depends on the mode.
39  *
40  * If mode is FilterChannels, each mask bit represents a midi channel (bit 0 =
41  * channel 0, bit 1 = channel 1, ...).  Only events whose channel corresponds
42  * to a 1 bit will be passed.
43  *
44  * If mode is ForceChannel, mask is simply a channel number which all events
45  * will be forced to.
46  */
47 class LIBARDOUR_API MidiChannelFilter
48 {
49 public:
50         MidiChannelFilter();
51
52         /** Filter `bufs` in-place. */
53         void filter(BufferSet& bufs);
54
55         /** Filter/map a MIDI message by channel.
56          *
57          * May modify the channel in `buf` if necessary.
58          *
59          * @return true if this event should be filtered out.
60          */
61         bool filter(uint8_t* buf, uint32_t len);
62
63         /** Atomically set the channel mode and corresponding mask.
64          * @return true iff configuration changed.
65          */
66         bool set_channel_mode(ChannelMode mode, uint16_t mask);
67
68         /** Atomically set the channel mask for the current mode.
69          * @return true iff configuration changed.
70          */
71         bool set_channel_mask(uint16_t mask);
72
73         /** Atomically get both the channel mode and mask. */
74         void get_mode_and_mask(ChannelMode* mode, uint16_t* mask) const {
75                 const uint32_t mm = g_atomic_int_get(&_mode_mask);
76                 *mode = static_cast<ChannelMode>((mm & 0xFFFF0000) >> 16);
77                 *mask = (mm & 0x0000FFFF);
78         }
79
80         ChannelMode get_channel_mode() const {
81                 return static_cast<ChannelMode>((g_atomic_int_get(&_mode_mask) & 0xFFFF0000) >> 16);
82         }
83
84         uint16_t get_channel_mask() const {
85                 return g_atomic_int_get(&_mode_mask) & 0x0000FFFF;
86         }
87
88         PBD::Signal0<void> ChannelMaskChanged;
89         PBD::Signal0<void> ChannelModeChanged;
90
91 private:
92         uint32_t _mode_mask;  ///< 16 bits mode, 16 bits mask
93 };
94
95 } /* namespace ARDOUR */
96
97 #endif /* __ardour_channel_filter_h__ */