Only show user-presets in favorite sidebar
[ardour.git] / libs / ardour / midi_scene_change.cc
1 /*
2     Copyright (C) 2014 Paul Davis
3
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.
8
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.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "pbd/error.h"
21 #include "pbd/compose.h"
22 #include "pbd/types_convert.h"
23
24 #include "ardour/midi_port.h"
25 #include "ardour/midi_scene_change.h"
26
27 #include "pbd/i18n.h"
28
29 using namespace PBD;
30 using namespace ARDOUR;
31
32 MIDISceneChange::MIDISceneChange (int c, int b, int p)
33         : _bank (b)
34         , _program (p)
35         , _channel (c & 0xf)
36 {
37         if (_bank > 16384) {
38                 _bank = -1;
39         }
40
41         if (_program > 128) {
42                 _program = -1;
43         }
44 }
45
46 MIDISceneChange::MIDISceneChange (const XMLNode& node, int version)
47         : _bank (-1)
48         , _program (-1)
49         , _channel (-1)
50 {
51         set_state (node, version);
52 }
53
54 MIDISceneChange::~MIDISceneChange ()
55 {
56 }
57
58 size_t
59 MIDISceneChange::get_bank_msb_message (uint8_t* buf, size_t size) const
60 {
61         if (size < 3 || _bank < 0) {
62                 return 0;
63         }
64
65         buf[0] = 0xB0 | (_channel & 0xf);
66         buf[1] = 0x0;
67         buf[2] = (_bank >> 7) & 0x7f;
68
69         return 3;
70 }
71
72 size_t
73 MIDISceneChange::get_bank_lsb_message (uint8_t* buf, size_t size) const
74 {
75         if (size < 3 || _bank < 0) {
76                 return 0;
77         }
78
79         buf[0] = 0xB0 | (_channel & 0xf);
80         buf[1] = 0x20;
81         buf[2] = _bank & 0x7f;
82
83         return 3;
84 }
85
86 size_t
87 MIDISceneChange::get_program_message (uint8_t* buf, size_t size) const
88 {
89         if (size < 2 || _program < 0) {
90                 return 0;
91         }
92
93         buf[0] = 0xC0 | (_channel & 0xf);
94         buf[1] = _program & 0x7f;
95
96         return 2;
97 }
98
99 XMLNode&
100 MIDISceneChange::get_state ()
101 {
102         XMLNode* node = new XMLNode (SceneChange::xml_node_name);
103
104         node->set_property (X_("type"), X_("MIDI"));
105         node->set_property (X_("id"), id());
106         node->set_property (X_("program"), _program);
107         node->set_property (X_("bank"), _bank);
108         node->set_property (X_("channel"), _channel);
109         node->set_property (X_("color"), _color);
110
111         return *node;
112 }
113
114 int
115 MIDISceneChange::set_state (const XMLNode& node, int /* version-ignored */)
116 {
117         if (!set_id (node)) {
118                 return -1;
119         }
120
121         if (!node.get_property (X_("program"), _program) || !node.get_property (X_("bank"), _bank) ||
122             !node.get_property (X_("channel"), _channel)) {
123                 return -1;
124         }
125
126         if (!node.get_property (X_("color"), _color)) {
127                 _color = out_of_bound_color;
128         }
129
130         return 0;
131 }
132
133 bool
134 MIDISceneChange::operator==(const MIDISceneChange& other) const
135 {
136         return _program == other._program &&
137                 _bank == other._bank &&
138                 _channel == other._channel;
139 }
140
141 void
142 MIDISceneChange::set_channel (int channel)
143 {
144         _channel = channel;
145 }
146
147 void
148 MIDISceneChange::set_program (int program)
149 {
150         _program = program;
151 }
152
153 void
154 MIDISceneChange::set_bank (int bank)
155 {
156         _bank = bank;
157 }
158
159