summaryrefslogtreecommitdiff
path: root/libs/evoral/evoral/PatchChange.hpp
blob: 90d3dba732b5c3132a83ff7d6542aff1983f6dd3 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
  Copyright (C) 2010 Paul Davis
  Author: Carl Hetherington <cth@carlh.net>

  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.
*/

#ifndef EVORAL_PATCH_CHANGE_HPP
#define EVORAL_PATCH_CHANGE_HPP

#include "evoral/visibility.h"
#include "evoral/Event.hpp"
#include "evoral/Event.hpp"

namespace Evoral {

/** Event representing a `patch change', composed of a LSB and MSB
 *  bank select and then a program change.
 */
template<typename Time>
class /*LIBEVORAL_API*/ PatchChange
{
public:
	/** @param t Time.
	 *  @param c Channel.
	 *  @param p Program change number (counted from 0).
	 *  @param b Bank number (counted from 0, 14-bit).
	 */
	PatchChange (Time t, uint8_t c, uint8_t p, int b)
		: _bank_change_msb (MIDI_EVENT, t, 3, 0, true)
		, _bank_change_lsb (MIDI_EVENT, t, 3, 0, true)
		, _program_change (MIDI_EVENT, t, 2, 0, true)
	{
		_bank_change_msb.buffer()[0] = MIDI_CMD_CONTROL | c;
		_bank_change_msb.buffer()[1] = MIDI_CTL_MSB_BANK;
		_bank_change_msb.buffer()[2] = (b >> 7) & 0x7f;

		_bank_change_lsb.buffer()[0] = MIDI_CMD_CONTROL | c;
		_bank_change_lsb.buffer()[1] = MIDI_CTL_LSB_BANK;
		_bank_change_lsb.buffer()[2] = b & 0x7f;

		_program_change.buffer()[0] = MIDI_CMD_PGM_CHANGE | c;
		_program_change.buffer()[1] = p;
	}

	PatchChange (const PatchChange & other)
		: _bank_change_msb (other._bank_change_msb, true)
		, _bank_change_lsb (other._bank_change_lsb, true)
		, _program_change (other._program_change, true)
	{
		set_id (other.id ());
	}

	event_id_t id () const {
		return _program_change.id ();
	}

	void set_id (event_id_t id) {
		_bank_change_msb.set_id (id);
		_bank_change_lsb.set_id (id);
		_program_change.set_id (id);
	}

	Time time () const {
		return _program_change.time ();
	}

	void set_time (Time t) {
		_bank_change_msb.set_time (t);
		_bank_change_lsb.set_time (t);
		_program_change.set_time (t);
	}

	void set_channel (uint8_t c) {
		_bank_change_msb.buffer()[0] &= 0xf0;
		_bank_change_msb.buffer()[0] |= c;
		_bank_change_lsb.buffer()[0] &= 0xf0;
		_bank_change_lsb.buffer()[0] |= c;
		_program_change.buffer()[0] &= 0xf0;
		_program_change.buffer()[0] |= c;
	}

	uint8_t program () const {
		return _program_change.buffer()[1];
	}

	void set_program (uint8_t p) {
		_program_change.buffer()[1] = p;
	}

	int bank () const {
		return (bank_msb() << 7) | bank_lsb();
	}

	void set_bank (int b) {
		_bank_change_msb.buffer()[2] = (b >> 7) & 0x7f;
		_bank_change_lsb.buffer()[2] = b & 0x7f;
	}

	uint8_t bank_msb () const {
		return _bank_change_msb.buffer()[2];
	}

	uint8_t bank_lsb () const {
		return _bank_change_lsb.buffer()[2];
	}

	uint8_t channel () const { return _program_change.buffer()[0] & 0xf; }

	inline bool operator< (const PatchChange<Time>& o) const {
		if (time() != o.time()) {
			return time() < o.time();
		}

		if (bank() != o.bank()) {
			return bank() < o.bank();
		}

		return (program() < o.program());
	}

	inline bool operator== (const PatchChange<Time>& o) const {
		return (time() == o.time() && program() == o.program() && bank() == o.bank());
	}

	/** The PatchChange is made up of messages() MIDI messages; this method returns them by index.
	 *  @param i index of message to return.
	 */
	Event<Time> const & message (int i) const {
		switch (i) {
		case 0:
			return _bank_change_msb;
		case 1:
			return _bank_change_lsb;
		case 2:
			return _program_change;
		default:
			abort(); /*NOTREACHED*/
			return _program_change;
		}
	}

	/** @return Number of MIDI messages that make up this change */
	int messages () const {
		return 3;
	}

private:
	Event<Time> _bank_change_msb;
	Event<Time> _bank_change_lsb;
	Event<Time> _program_change;
};

}

template<typename Time>
/*LIBEVORAL_API*/ std::ostream& operator<< (std::ostream& o, const Evoral::PatchChange<Time>& p) {
	o << "Patch Change " << p.id() << " @ " << p.time() << " bank " << (int) p.bank() << " program " << (int) p.program();
	return o;
}

#endif