2 * Copyright (C) 2015-2016 David Robillard <d@drobilla.net>
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.
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.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "ardour/buffer_set.h"
20 #include "ardour/midi_buffer.h"
21 #include "ardour/midi_channel_filter.h"
26 MidiChannelFilter::MidiChannelFilter()
27 : _mode_mask(0x0000FFFF)
31 MidiChannelFilter::filter(BufferSet& bufs)
35 get_mode_and_mask(&mode, &mask);
37 if (mode == AllChannels) {
41 MidiBuffer& buf = bufs.get_midi(0);
43 for (MidiBuffer::iterator e = buf.begin(); e != buf.end(); ) {
44 Evoral::Event<samplepos_t> ev(*e, false);
46 if (ev.is_channel_event()) {
49 if (0 == ((1 << ev.channel()) & mask)) {
56 ev.set_channel(PBD::ffs(mask) - 1);
60 /* handled by the opening if() */
71 MidiChannelFilter::filter(uint8_t* buf, uint32_t len)
75 get_mode_and_mask(&mode, &mask);
77 const uint8_t type = buf[0] & 0xF0;
78 const bool is_channel_event = (0x80 <= type) && (type <= 0xE0);
79 if (!is_channel_event) {
83 const uint8_t channel = buf[0] & 0x0F;
88 return !((1 << channel) & mask);
90 buf[0] = (0xF0 & buf[0]) | (0x0F & (PBD::ffs(mask) - 1));
97 /** If mode is ForceChannel, force mask to the lowest set channel or 1 if no
100 static inline uint16_t
101 force_mask(const ChannelMode mode, const uint16_t mask)
103 return ((mode == ForceChannel)
104 ? (mask ? (1 << (PBD::ffs(mask) - 1)) : 1)
109 MidiChannelFilter::set_channel_mode(ChannelMode mode, uint16_t mask)
111 ChannelMode old_mode;
113 get_mode_and_mask(&old_mode, &old_mask);
115 if (old_mode != mode || old_mask != mask) {
116 mask = force_mask(mode, mask);
117 g_atomic_int_set(&_mode_mask, (uint32_t(mode) << 16) | uint32_t(mask));
118 ChannelModeChanged();
126 MidiChannelFilter::set_channel_mask(uint16_t mask)
130 get_mode_and_mask(&mode, &old_mask);
132 if (old_mask != mask) {
133 mask = force_mask(mode, mask);
134 g_atomic_int_set(&_mode_mask, (uint32_t(mode) << 16) | uint32_t(mask));
135 ChannelMaskChanged();
142 } /* namespace ARDOUR */