summaryrefslogtreecommitdiff
path: root/libs/ardour/midi_channel_filter.cc
blob: c467f37bd6ebd9147911c5a7fc19db35b4952314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
    Copyright (C) 2006-2015 Paul Davis
    Author: David Robillard

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "ardour/buffer_set.h"
#include "ardour/midi_buffer.h"
#include "ardour/midi_channel_filter.h"
#include "pbd/ffs.h"

namespace ARDOUR {

MidiChannelFilter::MidiChannelFilter()
	: _mode_mask(0x0000FFFF)
{}

void
MidiChannelFilter::filter(BufferSet& bufs)
{
	ChannelMode mode;
	uint16_t    mask;
	get_mode_and_mask(&mode, &mask);

	if (mode == AllChannels) {
		return;
	}

	MidiBuffer& buf = bufs.get_midi(0);

	for (MidiBuffer::iterator e = buf.begin(); e != buf.end(); ) {
		Evoral::Event<samplepos_t> ev(*e, false);

		if (ev.is_channel_event()) {
			switch (mode) {
			case FilterChannels:
				if (0 == ((1 << ev.channel()) & mask)) {
					e = buf.erase (e);
				} else {
					++e;
				}
				break;
			case ForceChannel:
				ev.set_channel(PBD::ffs(mask) - 1);
				++e;
				break;
			case AllChannels:
				/* handled by the opening if() */
				++e;
				break;
			}
		} else {
			++e;
		}
	}
}

bool
MidiChannelFilter::filter(uint8_t* buf, uint32_t len)
{
	ChannelMode mode;
	uint16_t    mask;
	get_mode_and_mask(&mode, &mask);

	const uint8_t type             = buf[0] & 0xF0;
	const bool    is_channel_event = (0x80 <= type) && (type <= 0xE0);
	if (!is_channel_event) {
		return false;
	}

	const uint8_t channel = buf[0] & 0x0F;
	switch (mode) {
	case AllChannels:
		return false;
	case FilterChannels:
		return !((1 << channel) & mask);
	case ForceChannel:
		buf[0] = (0xF0 & buf[0]) | (0x0F & (PBD::ffs(mask) - 1));
		return false;
	}

	return false;
}

/** If mode is ForceChannel, force mask to the lowest set channel or 1 if no
 *  channels are set.
 */
static inline uint16_t
force_mask(const ChannelMode mode, const uint16_t mask)
{
	return ((mode == ForceChannel)
	        ? (mask ? (1 << (PBD::ffs(mask) - 1)) : 1)
	        : mask);
}

bool
MidiChannelFilter::set_channel_mode(ChannelMode mode, uint16_t mask)
{
	ChannelMode old_mode;
	uint16_t    old_mask;
	get_mode_and_mask(&old_mode, &old_mask);

	if (old_mode != mode || old_mask != mask) {
		mask = force_mask(mode, mask);
		g_atomic_int_set(&_mode_mask, (uint32_t(mode) << 16) | uint32_t(mask));
		ChannelModeChanged();
		return true;
	}

	return false;
}

bool
MidiChannelFilter::set_channel_mask(uint16_t mask)
{
	ChannelMode mode;
	uint16_t    old_mask;
	get_mode_and_mask(&mode, &old_mask);

	if (old_mask != mask) {
		mask = force_mask(mode, mask);
		g_atomic_int_set(&_mode_mask, (uint32_t(mode) << 16) | uint32_t(mask));
		ChannelMaskChanged();
		return true;
	}

	return false;
}

} /* namespace ARDOUR */