2 Copyright (C) 2010 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
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 #ifndef EVORAL_PATCH_CHANGE_HPP
21 #define EVORAL_PATCH_CHANGE_HPP
23 #include "evoral/visibility.h"
24 #include "evoral/Event.hpp"
25 #include "evoral/MIDIEvent.hpp"
29 /** Event representing a `patch change', composed of a LSB and MSB
30 * bank select and then a program change.
32 template<typename Time>
33 class /*LIBEVORAL_API*/ PatchChange
38 * @param p Program change number (counted from 0).
39 * @param b Bank number (counted from 0, 14-bit).
41 PatchChange (Time t, uint8_t c, uint8_t p, int b)
42 : _bank_change_msb (0, t, 3, 0, true)
43 , _bank_change_lsb (0, t, 3, 0, true)
44 , _program_change (0, t, 2, 0, true)
46 _bank_change_msb.buffer()[0] = MIDI_CMD_CONTROL | c;
47 _bank_change_msb.buffer()[1] = MIDI_CTL_MSB_BANK;
48 _bank_change_msb.buffer()[2] = (b >> 7) & 0x7f;
50 _bank_change_lsb.buffer()[0] = MIDI_CMD_CONTROL | c;
51 _bank_change_lsb.buffer()[1] = MIDI_CTL_LSB_BANK;
52 _bank_change_lsb.buffer()[2] = b & 0x7f;
54 _program_change.buffer()[0] = MIDI_CMD_PGM_CHANGE | c;
55 _program_change.buffer()[1] = p;
58 PatchChange (const PatchChange & other)
59 : _bank_change_msb (other._bank_change_msb, true)
60 , _bank_change_lsb (other._bank_change_lsb, true)
61 , _program_change (other._program_change, true)
66 event_id_t id () const {
67 return _program_change.id ();
70 void set_id (event_id_t id) {
71 _bank_change_msb.set_id (id);
72 _bank_change_lsb.set_id (id);
73 _program_change.set_id (id);
77 return _program_change.time ();
80 void set_time (Time t) {
81 _bank_change_msb.set_time (t);
82 _bank_change_lsb.set_time (t);
83 _program_change.set_time (t);
86 void set_channel (uint8_t c) {
87 _bank_change_msb.buffer()[0] &= 0xf0;
88 _bank_change_msb.buffer()[0] |= c;
89 _bank_change_lsb.buffer()[0] &= 0xf0;
90 _bank_change_lsb.buffer()[0] |= c;
91 _program_change.buffer()[0] &= 0xf0;
92 _program_change.buffer()[0] |= c;
95 uint8_t program () const {
96 return _program_change.buffer()[1];
99 void set_program (uint8_t p) {
100 _program_change.buffer()[1] = p;
104 return (bank_msb() << 7) | bank_lsb();
107 void set_bank (int b) {
108 _bank_change_msb.buffer()[2] = (b >> 7) & 0x7f;
109 _bank_change_lsb.buffer()[2] = b & 0x7f;
112 uint8_t bank_msb () const {
113 return _bank_change_msb.buffer()[2];
116 uint8_t bank_lsb () const {
117 return _bank_change_lsb.buffer()[2];
120 uint8_t channel () const { return _program_change.buffer()[0] & 0xf; }
122 inline bool operator< (const PatchChange<Time>& o) const {
123 if (!musical_time_equal (time(), o.time())) {
124 return time() < o.time();
127 if (bank() != o.bank()) {
128 return bank() < o.bank();
131 return (program() < o.program());
134 inline bool operator== (const PatchChange<Time>& o) const {
135 return (musical_time_equal (time(), o.time()) && program() == o.program() && bank() == o.bank());
138 /** The PatchChange is made up of messages() MIDI messages; this method returns them by index.
139 * @param i index of message to return.
141 MIDIEvent<Time> const & message (int i) const {
144 return _bank_change_msb;
146 return _bank_change_lsb;
148 return _program_change;
152 return _program_change;
156 /** @return Number of MIDI messages that make up this change */
157 int messages () const {
162 MIDIEvent<Time> _bank_change_msb;
163 MIDIEvent<Time> _bank_change_lsb;
164 MIDIEvent<Time> _program_change;
169 template<typename Time>
170 /*LIBEVORAL_API*/ std::ostream& operator<< (std::ostream& o, const Evoral::PatchChange<Time>& p) {
171 o << "Patch Change " << p.id() << " @ " << p.time() << " bank " << (int) p.bank() << " program " << (int) p.program();