2 Copyright (C) 2008 Hans Baier
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
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #ifndef MIDNAM_PATCH_H_
22 #define MIDNAM_PATCH_H_
29 #include "pbd/stateful.h"
30 #include "midi++/event.h"
31 #include "pbd/xml++.h"
39 struct PatchPrimaryKey
46 PatchPrimaryKey(int a_msb = -1, int a_lsb = -1, int a_program_number = -1) {
49 program_number = a_program_number;
53 return ((msb >= 0) && (msb <= 127) &&
54 (lsb >= 0) && (lsb <= 127) &&
55 (program_number >=0 ) && (program_number <= 127));
58 inline PatchPrimaryKey& operator=(const PatchPrimaryKey& id) {
61 program_number = id.program_number;
65 inline bool operator==(const PatchPrimaryKey& id) const {
66 return (msb == id.msb && lsb == id.lsb && program_number == id.program_number);
70 * obey strict weak ordering or crash in STL containers
72 inline bool operator<(const PatchPrimaryKey& id) const {
75 } else if (msb == id.msb && lsb < id.lsb) {
77 } else if (lsb == id.lsb && program_number < id.program_number) {
87 class Patch : public PBD::Stateful
91 Patch(PatchBank* a_bank = 0) : _bank(a_bank) {};
92 Patch(std::string a_number, std::string a_name, PatchBank* a_bank = 0)
93 : _number(a_number), _name(a_name), _bank(a_bank) {};
96 const std::string& name() const { return _name; }
97 void set_name(const std::string a_name) { _name = a_name; }
99 const std::string& number() const { return _number; }
100 void set_number(const std::string a_number) { _number = a_number; }
102 const PatchPrimaryKey& patch_primary_key() const { return _id; }
104 XMLNode& get_state (void);
105 int set_state (const XMLNode&, int version = 3000);
110 // cannot use a boost::shared_ptr here in order to avoid retain cycles
115 class PatchBank : public PBD::Stateful
118 typedef std::list<boost::shared_ptr<Patch> > PatchNameList;
120 PatchBank () : _id(0) {};
121 PatchBank (std::string a_name, PatchPrimaryKey* an_id = 0) : _name(a_name), _id(an_id) {};
122 virtual ~PatchBank() { delete _id; };
124 const std::string& name() const { return _name; }
125 void set_name(const std::string a_name) { _name = a_name; }
127 const PatchNameList& patch_name_list() const { return _patch_name_list; }
129 const PatchPrimaryKey* patch_primary_key() const { return _id; }
131 XMLNode& get_state (void);
132 int set_state (const XMLNode&, int version = 3000);
136 PatchNameList _patch_name_list;
137 PatchPrimaryKey* _id;
142 class ChannelNameSet : public PBD::Stateful
145 typedef std::set<uint8_t> AvailableForChannels;
146 typedef std::list<boost::shared_ptr<PatchBank> > PatchBanks;
147 typedef std::map<PatchPrimaryKey, boost::shared_ptr<Patch> > PatchMap;
148 typedef std::list<PatchPrimaryKey> PatchList;
151 virtual ~ChannelNameSet() {};
152 ChannelNameSet(std::string a_name) : _name(a_name) {};
154 const std::string& name() const { return _name; }
155 void set_name(const std::string a_name) { _name = a_name; }
157 const PatchBanks& patch_banks() const { return _patch_banks; }
159 bool available_for_channel(uint8_t channel) const {
160 return _available_for_channels.find(channel) != _available_for_channels.end();
163 boost::shared_ptr<Patch> find_patch(PatchPrimaryKey& key) {
164 assert(key.is_sane());
165 return _patch_map[key];
168 boost::shared_ptr<Patch> previous_patch(PatchPrimaryKey& key) {
169 assert(key.is_sane());
170 std::cerr << "finding patch with " << key.msb << "/" << key.lsb << "/" <<key.program_number << std::endl;
171 for (PatchList::const_iterator i = _patch_list.begin();
172 i != _patch_list.end();
175 if (i != _patch_list.begin()) {
176 std::cerr << "got it!" << std::endl;
178 return _patch_map[*i];
183 return boost::shared_ptr<Patch>();
186 boost::shared_ptr<Patch> next_patch(PatchPrimaryKey& key) {
187 assert(key.is_sane());
188 std::cerr << "finding patch with " << key.msb << "/" << key.lsb << "/" <<key.program_number << std::endl;
189 for (PatchList::const_iterator i = _patch_list.begin();
190 i != _patch_list.end();
193 if (++i != _patch_list.end()) {
194 std::cerr << "got it!" << std::endl;
195 return _patch_map[*i];
202 return boost::shared_ptr<Patch>();
205 XMLNode& get_state (void);
206 int set_state (const XMLNode& a_node, int version = 3000);
210 AvailableForChannels _available_for_channels;
211 PatchBanks _patch_banks;
213 PatchList _patch_list;
216 class Note : public PBD::Stateful
220 Note(std::string a_number, std::string a_name) : _number(a_number), _name(a_name) {};
223 const std::string& name() const { return _name; }
224 void set_name(const std::string a_name) { _name = a_name; }
226 const std::string& number() const { return _number; }
227 void set_number(const std::string a_number) { _number = a_number; }
229 XMLNode& get_state (void);
230 int set_state (const XMLNode&, int version = 3000);
237 class NoteNameList : public PBD::Stateful
240 typedef std::list<boost::shared_ptr<Note> > Notes;
242 NoteNameList (std::string a_name) : _name(a_name) {};
245 const std::string& name() const { return _name; }
246 void set_name(const std::string a_name) { _name = a_name; }
248 const Notes& notes() const { return _notes; }
250 XMLNode& get_state (void);
251 int set_state (const XMLNode&, int version = 3000);
258 class CustomDeviceMode : public PBD::Stateful
261 CustomDeviceMode() {};
262 virtual ~CustomDeviceMode() {};
264 const std::string& name() const { return _name; }
265 void set_name(const std::string a_name) { _name = a_name; }
268 XMLNode& get_state (void);
269 int set_state (const XMLNode&, int version = 3000);
271 std::string channel_name_set_name_by_channel(uint8_t channel) {
272 assert(channel <= 15);
273 return _channel_name_set_assignments[channel];
277 /// array index = channel number
278 /// string contents = name of channel name set
280 std::string _channel_name_set_assignments[16];
283 class MasterDeviceNames : public PBD::Stateful
286 typedef std::list<std::string> Models;
287 /// maps name to CustomDeviceMode
288 typedef std::map<std::string, boost::shared_ptr<CustomDeviceMode> > CustomDeviceModes;
289 typedef std::list<std::string> CustomDeviceModeNames;
290 /// maps name to ChannelNameSet
291 typedef std::map<std::string, boost::shared_ptr<ChannelNameSet> > ChannelNameSets;
292 typedef std::list<boost::shared_ptr<NoteNameList> > NoteNameLists;
295 MasterDeviceNames() {};
296 virtual ~MasterDeviceNames() {};
298 const std::string& manufacturer() const { return _manufacturer; }
299 void set_manufacturer(const std::string a_manufacturer) { _manufacturer = a_manufacturer; }
301 const Models& models() const { return _models; }
302 void set_models(const Models some_models) { _models = some_models; }
304 const CustomDeviceModeNames& custom_device_mode_names() const { return _custom_device_mode_names; }
306 boost::shared_ptr<CustomDeviceMode> custom_device_mode_by_name(std::string mode_name) {
307 assert(mode_name != "");
308 return _custom_device_modes[mode_name];
311 boost::shared_ptr<ChannelNameSet> channel_name_set_by_device_mode_and_channel(std::string mode, uint8_t channel) {
312 return _channel_name_sets[custom_device_mode_by_name(mode)->channel_name_set_name_by_channel(channel)];
315 boost::shared_ptr<Patch> find_patch(std::string mode, uint8_t channel, PatchPrimaryKey& key) {
316 return channel_name_set_by_device_mode_and_channel(mode, channel)->find_patch(key);
319 XMLNode& get_state (void);
320 int set_state (const XMLNode&, int version = 3000);
323 std::string _manufacturer;
325 CustomDeviceModes _custom_device_modes;
326 CustomDeviceModeNames _custom_device_mode_names;
327 ChannelNameSets _channel_name_sets;
328 NoteNameLists _note_name_lists;
331 class MIDINameDocument : public PBD::Stateful
334 // Maps Model names to MasterDeviceNames
335 typedef std::map<std::string, boost::shared_ptr<MasterDeviceNames> > MasterDeviceNamesList;
337 MIDINameDocument() {};
338 MIDINameDocument(const std::string &filename) : _document(XMLTree(filename)) { set_state(*_document.root()); };
339 virtual ~MIDINameDocument() {};
341 const std::string& author() const { return _author; }
342 void set_author(const std::string an_author) { _author = an_author; }
344 const MasterDeviceNamesList& master_device_names_by_model() const { return _master_device_names_list; }
346 const MasterDeviceNames::Models& all_models() const { return _all_models; }
348 XMLNode& get_state (void);
349 int set_state (const XMLNode&, int version = 3000);
353 MasterDeviceNamesList _master_device_names_list;
355 MasterDeviceNames::Models _all_models;
361 #endif /*MIDNAM_PATCH_H_*/