summaryrefslogtreecommitdiff
path: root/libs/midi++2/midi++/midnam_patch.h
blob: ddd62c29167a66183f6a7e331dc1a6aca324047f (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
    Copyright (C) 2012 Paul Davis
    Author: Hans Baier

    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 MIDNAM_PATCH_H_
#define MIDNAM_PATCH_H_

#include <algorithm>
#include <iostream>
#include <string>
#include <list>
#include <set>
#include <map>
#include <vector>

#include <stdint.h>

#include "midi++/event.h"
#include "pbd/xml++.h"

namespace MIDI
{

namespace Name
{

struct PatchPrimaryKey
{
public:
	int bank_number;
	int program_number;

	PatchPrimaryKey (uint8_t a_program_number = 0, uint16_t a_bank_number = 0) {
		bank_number = std::min (a_bank_number, (uint16_t) 16384);
		program_number = std::min (a_program_number, (uint8_t) 127);
	}
	
	bool is_sane() const { 	
		return ((bank_number >= 0) && (bank_number <= 16384) && 
			(program_number >=0 ) && (program_number <= 127));
	}
	
	inline PatchPrimaryKey& operator=(const PatchPrimaryKey& id) {
		bank_number = id.bank_number;
		program_number = id.program_number;
		return *this;
	}
	
	inline bool operator==(const PatchPrimaryKey& id) const {
		return (bank_number == id.bank_number && program_number == id.program_number);
	}
	
	/**
	 * obey strict weak ordering or crash in STL containers
	 */
	inline bool operator<(const PatchPrimaryKey& id) const {
		if (bank_number < id.bank_number) {
			return true;
		} else if (bank_number == id.bank_number && program_number < id.program_number) {
			return true;
		}
		
		return false;
	}
};

class PatchBank;
	
class Patch 
{
public:

	Patch (std::string a_name = std::string(), uint8_t a_number = 0, uint16_t bank_number = 0);
	virtual ~Patch() {};

	const std::string& name() const        { return _name; }
	void set_name(const std::string& name) { _name = name; }
	
	const std::string& note_list_name() const  { return _note_list_name; }

	uint8_t program_number() const     { return _id.program_number; }
	void set_program_number(uint8_t n) { _id.program_number = n; }

	uint16_t bank_number() const      { return _id.bank_number; }
	void set_bank_number (uint16_t n) { _id.bank_number = n; }

	const PatchPrimaryKey&   patch_primary_key()   const { return _id; }

	XMLNode& get_state (void);
	int      set_state (const XMLTree&, const XMLNode&);

private:
	std::string     _name;
	PatchPrimaryKey _id;
	std::string     _note_list_name;
};

typedef std::list<boost::shared_ptr<Patch> > PatchNameList;

class PatchBank 
{
public:
	PatchBank (uint16_t n = 0, std::string a_name = std::string()) : _name(a_name), _number (n) {};
	virtual ~PatchBank() { }

	const std::string& name() const          { return _name; }
	void set_name(const std::string& a_name) { _name = a_name; }

	int number() const { return _number; }

	const PatchNameList& patch_name_list() const { return _patch_name_list; }
	const std::string& patch_list_name() const { return _patch_list_name; }

	int set_patch_name_list (const PatchNameList&);

	XMLNode& get_state (void);
	int      set_state (const XMLTree&, const XMLNode&);

private:
	std::string       _name;
	uint16_t          _number;
	PatchNameList     _patch_name_list;
	std::string       _patch_list_name;
};

class ChannelNameSet
{
public:
	typedef std::set<uint8_t>                                    AvailableForChannels;
	typedef std::list<boost::shared_ptr<PatchBank> >             PatchBanks;
	typedef std::map<PatchPrimaryKey, boost::shared_ptr<Patch> > PatchMap;
	typedef std::list<PatchPrimaryKey>                           PatchList;

	ChannelNameSet() {};
	virtual ~ChannelNameSet() {};
	ChannelNameSet(std::string& name) : _name(name) {};

	const std::string& name() const        { return _name; }
	void set_name(const std::string& name) { _name = name; }
	
	const PatchBanks& patch_banks() const    { return _patch_banks; }

	bool available_for_channel(uint8_t channel) const { 
		return _available_for_channels.find(channel) != _available_for_channels.end(); 
	}
	
	boost::shared_ptr<Patch> find_patch(const PatchPrimaryKey& key) {
		assert(key.is_sane());
		return _patch_map[key];
	}
	
	boost::shared_ptr<Patch> previous_patch(const PatchPrimaryKey& key) {
		assert(key.is_sane());
		for (PatchList::const_iterator i = _patch_list.begin();
			 i != _patch_list.end();
			 ++i) {
			if ((*i) == key) {
				if (i != _patch_list.begin()) {
					--i;
					return  _patch_map[*i];
				} 
			}
		}
			
		return boost::shared_ptr<Patch>();
	}
	
	boost::shared_ptr<Patch> next_patch(const PatchPrimaryKey& key) {
		assert(key.is_sane());
		for (PatchList::const_iterator i = _patch_list.begin();
			 i != _patch_list.end();
			 ++i) {
			if ((*i) == key) {
				if (++i != _patch_list.end()) {
					return  _patch_map[*i];
				} else {
					--i;
				}
			}
		}
			
		return boost::shared_ptr<Patch>();
	}

	const std::string& note_list_name()    const { return _note_list_name; }
	const std::string& control_list_name() const { return _control_list_name; }

	XMLNode& get_state (void);
	int      set_state (const XMLTree&, const XMLNode&);

	void set_patch_banks (const PatchBanks&);
	void use_patch_name_list (const PatchNameList&);

private:
	friend std::ostream& operator<< (std::ostream&, const ChannelNameSet&);

	std::string          _name;
	AvailableForChannels _available_for_channels;
	PatchBanks           _patch_banks;
	PatchMap             _patch_map;
	PatchList            _patch_list;
	std::string          _patch_list_name;
	std::string          _note_list_name;
	std::string          _control_list_name;
};

std::ostream& operator<< (std::ostream&, const ChannelNameSet&);

class Note
{
public:
	Note() {}
	Note(uint8_t number, const std::string& name) : _number(number), _name(name) {}

	const std::string& name() const        { return _name; }
	void set_name(const std::string& name) { _name = name; }

	uint8_t number() const             { return _number; }
	void    set_number(uint8_t number) { _number = number; }

	XMLNode& get_state (void);
	int      set_state (const XMLTree&, const XMLNode&);

private:
	uint8_t     _number;
	std::string _name;
};

class NoteNameList 
{
public:
	typedef std::vector< boost::shared_ptr<Note> > Notes;

	NoteNameList() { _notes.resize(128); }
	NoteNameList (const std::string& name) : _name(name) { _notes.resize(128); }

	const std::string& name() const  { return _name; }
	const Notes&       notes() const { return _notes; }

	void set_name(const std::string& name) { _name = name; }

	XMLNode& get_state (void);
	int      set_state (const XMLTree&, const XMLNode&);

private:
	std::string _name;
	Notes       _notes;
};

class Control
{
public:
	Control() {}
	Control(const std::string& type,
	        const uint16_t     number,
	        const std::string& name)
		: _type(type)
		, _number(number)
		, _name(name)
	{}

	const std::string& type()   const { return _type; }
	uint16_t           number() const { return _number; }
	const std::string& name()   const { return _name; }

	void set_type(const std::string& type) { _type = type; }
	void set_number(uint16_t number)       { _number = number; }
	void set_name(const std::string& name) { _name = name; }

	XMLNode& get_state(void);
	int      set_state(const XMLTree&, const XMLNode&);

private:
	std::string _type;
	uint16_t    _number;
	std::string _name;
};

class ControlNameList 
{
public:
	typedef std::map<uint16_t, boost::shared_ptr<Control> > Controls;

	ControlNameList() {}
	ControlNameList(const std::string& name) : _name(name) {}

	const std::string& name() const { return _name; }

	void set_name(const std::string& name) { _name = name; }

	boost::shared_ptr<const Control> control(uint16_t num) const;

	const Controls& controls() const { return _controls; }

	XMLNode& get_state(void);
	int      set_state(const XMLTree&, const XMLNode&);

private:
	std::string _name;
	Controls    _controls;
};

class CustomDeviceMode
{
public:
	CustomDeviceMode() {};
	virtual ~CustomDeviceMode() {};

	const std::string& name() const        { return _name; }
	void set_name(const std::string& name) { _name = name; }

	
	XMLNode& get_state (void);
	int      set_state (const XMLTree&, const XMLNode&);
	
	/// Note: channel here is 0-based while in the MIDNAM-file it's 1-based
	const std::string& channel_name_set_name_by_channel(uint8_t channel) {
		assert(channel <= 15);
		return _channel_name_set_assignments[channel]; 
	}
	
private:
	/// array index = channel number
	/// string contents = name of channel name set 
	std::string _name;
	std::string _channel_name_set_assignments[16];
};

class MasterDeviceNames
{
public:
	typedef std::set<std::string>                                       Models;
	/// maps name to CustomDeviceMode
	typedef std::map<std::string, boost::shared_ptr<CustomDeviceMode> > CustomDeviceModes;
	typedef std::list<std::string>                                      CustomDeviceModeNames;
	/// maps name to ChannelNameSet
	typedef std::map<std::string, boost::shared_ptr<ChannelNameSet> >   ChannelNameSets;
	typedef std::map<std::string, boost::shared_ptr<NoteNameList> >     NoteNameLists;
	typedef std::map<std::string, boost::shared_ptr<ControlNameList> >  ControlNameLists;
	typedef std::map<std::string, PatchNameList>                        PatchNameLists;
	
	MasterDeviceNames() {};
	virtual ~MasterDeviceNames() {};
	
	const std::string& manufacturer() const { return _manufacturer; }
	void set_manufacturer(const std::string& manufacturer) { _manufacturer = manufacturer; }
	
	const Models& models() const { return _models; }
	void set_models(const Models some_models) { _models = some_models; }

	const ControlNameLists& controls() const { return _control_name_lists; }

	const CustomDeviceModeNames& custom_device_mode_names() const { return _custom_device_mode_names; }
	
	boost::shared_ptr<CustomDeviceMode> custom_device_mode_by_name(const std::string& mode_name);
	boost::shared_ptr<ChannelNameSet> channel_name_set_by_device_mode_and_channel(const std::string& mode, uint8_t channel);
	boost::shared_ptr<Patch> find_patch(const std::string& mode, uint8_t channel, const PatchPrimaryKey& key);

	boost::shared_ptr<ControlNameList> control_name_list(const std::string& name);
	boost::shared_ptr<NoteNameList>    note_name_list(const std::string& name);
	boost::shared_ptr<ChannelNameSet>  channel_name_set(const std::string& name);

	std::string note_name(const std::string& mode_name,
	                      uint8_t            channel,
	                      uint16_t           bank,
	                      uint8_t            program,
	                      uint8_t            number);

	XMLNode& get_state (void);
	int      set_state (const XMLTree&, const XMLNode&);
	
private:
	std::string           _manufacturer;
	Models                _models;
	CustomDeviceModes     _custom_device_modes;
	CustomDeviceModeNames _custom_device_mode_names;
	ChannelNameSets       _channel_name_sets;
	NoteNameLists         _note_name_lists;
	PatchNameLists        _patch_name_lists;
	ControlNameLists      _control_name_lists;
};

class MIDINameDocument
{
public:
	// Maps Model names to MasterDeviceNames
	typedef std::map<std::string, boost::shared_ptr<MasterDeviceNames> > MasterDeviceNamesList;
	
	MIDINameDocument() {}
	MIDINameDocument(const std::string& filename);
	virtual ~MIDINameDocument() {};

	const std::string& author() const { return _author; }
	void set_author(const std::string& author) { _author = author; }
	
	boost::shared_ptr<MasterDeviceNames> master_device_names(const std::string& model);

	const MasterDeviceNamesList& master_device_names_by_model() const { return _master_device_names_list; }
	
	const MasterDeviceNames::Models& all_models() const { return _all_models; }
		
	XMLNode& get_state (void);
	int      set_state (const XMLTree&, const XMLNode&);

private:
	std::string                   _author;
	MasterDeviceNamesList         _master_device_names_list;
	XMLTree                       _document;
	MasterDeviceNames::Models     _all_models;
};

extern const char* general_midi_program_names[128]; /* 0 .. 127 */

}

}
#endif /*MIDNAM_PATCH_H_*/