2 Copyright (C) 2006-2015 Paul Davis
3 Author: David Robillard
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.
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.
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.
20 #include "ardour/buffer_set.h"
21 #include "ardour/midi_buffer.h"
22 #include "ardour/midi_channel_filter.h"
27 MidiChannelFilter::MidiChannelFilter()
28 : _mode_mask(0x0000FFFF)
32 MidiChannelFilter::filter(BufferSet& bufs)
36 get_mode_and_mask(&mode, &mask);
38 if (mode == AllChannels) {
42 MidiBuffer& buf = bufs.get_midi(0);
44 for (MidiBuffer::iterator e = buf.begin(); e != buf.end(); ) {
45 Evoral::MIDIEvent<framepos_t> ev(*e, false);
47 if (ev.is_channel_event()) {
50 if (0 == ((1 << ev.channel()) & mask)) {
57 ev.set_channel(PBD::ffs(mask) - 1);
61 /* handled by the opening if() */
72 MidiChannelFilter::filter(uint8_t* buf, uint32_t len)
76 get_mode_and_mask(&mode, &mask);
78 const uint8_t type = buf[0] & 0xF0;
79 const bool is_channel_event = (0x80 <= type) && (type <= 0xE0);
80 if (!is_channel_event) {
84 const uint8_t channel = buf[0] & 0x0F;
89 return !((1 << channel) & mask);
91 buf[0] = (0xF0 & buf[0]) | (0x0F & (PBD::ffs(mask) - 1));
98 /** If mode is ForceChannel, force mask to the lowest set channel or 1 if no
101 static inline uint16_t
102 force_mask(const ChannelMode mode, const uint16_t mask)
104 return ((mode == ForceChannel)
105 ? (mask ? (1 << (PBD::ffs(mask) - 1)) : 1)
110 MidiChannelFilter::set_channel_mode(ChannelMode mode, uint16_t mask)
112 ChannelMode old_mode;
114 get_mode_and_mask(&old_mode, &old_mask);
116 if (old_mode != mode || old_mask != mask) {
117 mask = force_mask(mode, mask);
118 g_atomic_int_set(&_mode_mask, (uint32_t(mode) << 16) | uint32_t(mask));
119 ChannelModeChanged();
127 MidiChannelFilter::set_channel_mask(uint16_t mask)
131 get_mode_and_mask(&mode, &old_mask);
133 if (old_mask != mask) {
134 mask = force_mask(mode, mask);
135 g_atomic_int_set(&_mode_mask, (uint32_t(mode) << 16) | uint32_t(mask));
136 ChannelMaskChanged();
143 } /* namespace ARDOUR */