Merge branch 'master' into windows
[ardour.git] / libs / midi++2 / midi++ / midnam_patch.h
1 /*
2     Copyright (C) 2012 Paul Davis
3     Author: Hans Baier
4
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.
9
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.
14
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.
18 */
19
20 #ifndef MIDNAM_PATCH_H_
21 #define MIDNAM_PATCH_H_
22
23 #include <algorithm>
24 #include <iostream>
25 #include <string>
26 #include <list>
27 #include <set>
28 #include <map>
29 #include <vector>
30
31 #include <stdint.h>
32
33 #include "midi++/event.h"
34 #include "pbd/xml++.h"
35
36 namespace MIDI
37 {
38
39 namespace Name
40 {
41
42 struct PatchPrimaryKey
43 {
44 public:
45         int bank_number;
46         int program_number;
47
48         PatchPrimaryKey (uint8_t a_program_number = 0, uint16_t a_bank_number = 0) {
49                 bank_number = std::min (a_bank_number, (uint16_t) 16384);
50                 program_number = std::min (a_program_number, (uint8_t) 127);
51         }
52         
53         bool is_sane() const {  
54                 return ((bank_number >= 0) && (bank_number <= 16384) && 
55                         (program_number >=0 ) && (program_number <= 127));
56         }
57         
58         inline PatchPrimaryKey& operator=(const PatchPrimaryKey& id) {
59                 bank_number = id.bank_number;
60                 program_number = id.program_number;
61                 return *this;
62         }
63         
64         inline bool operator==(const PatchPrimaryKey& id) const {
65                 return (bank_number == id.bank_number && program_number == id.program_number);
66         }
67         
68         /**
69          * obey strict weak ordering or crash in STL containers
70          */
71         inline bool operator<(const PatchPrimaryKey& id) const {
72                 if (bank_number < id.bank_number) {
73                         return true;
74                 } else if (bank_number == id.bank_number && program_number < id.program_number) {
75                         return true;
76                 }
77                 
78                 return false;
79         }
80 };
81
82 class PatchBank;
83         
84 class Patch 
85 {
86 public:
87
88         Patch (std::string a_name = std::string(), uint8_t a_number = 0, uint16_t bank_number = 0);
89         virtual ~Patch() {};
90
91         const std::string& name() const        { return _name; }
92         void set_name(const std::string& name) { _name = name; }
93         
94         const std::string& note_list_name() const  { return _note_list_name; }
95
96         uint8_t program_number() const     { return _id.program_number; }
97         void set_program_number(uint8_t n) { _id.program_number = n; }
98
99         uint16_t bank_number() const      { return _id.bank_number; }
100         void set_bank_number (uint16_t n) { _id.bank_number = n; }
101
102         const PatchPrimaryKey&   patch_primary_key()   const { return _id; }
103
104         XMLNode& get_state (void);
105         int      set_state (const XMLTree&, const XMLNode&);
106
107 private:
108         std::string     _name;
109         PatchPrimaryKey _id;
110         std::string     _note_list_name;
111 };
112
113 typedef std::list<boost::shared_ptr<Patch> > PatchNameList;
114
115 class PatchBank 
116 {
117 public:
118         PatchBank (uint16_t n = 0, std::string a_name = std::string()) : _name(a_name), _number (n) {};
119         virtual ~PatchBank() { }
120
121         const std::string& name() const          { return _name; }
122         void set_name(const std::string& a_name) { _name = a_name; }
123
124         int number() const { return _number; }
125
126         const PatchNameList& patch_name_list() const { return _patch_name_list; }
127         const std::string& patch_list_name() const { return _patch_list_name; }
128
129         int set_patch_name_list (const PatchNameList&);
130
131         XMLNode& get_state (void);
132         int      set_state (const XMLTree&, const XMLNode&);
133
134 private:
135         std::string       _name;
136         uint16_t          _number;
137         PatchNameList     _patch_name_list;
138         std::string       _patch_list_name;
139 };
140
141 class ChannelNameSet
142 {
143 public:
144         typedef std::set<uint8_t>                                    AvailableForChannels;
145         typedef std::list<boost::shared_ptr<PatchBank> >             PatchBanks;
146         typedef std::map<PatchPrimaryKey, boost::shared_ptr<Patch> > PatchMap;
147         typedef std::list<PatchPrimaryKey>                           PatchList;
148
149         ChannelNameSet() {};
150         virtual ~ChannelNameSet() {};
151         ChannelNameSet(std::string& name) : _name(name) {};
152
153         const std::string& name() const        { return _name; }
154         void set_name(const std::string& name) { _name = name; }
155         
156         const PatchBanks& patch_banks() const    { return _patch_banks; }
157
158         bool available_for_channel(uint8_t channel) const { 
159                 return _available_for_channels.find(channel) != _available_for_channels.end(); 
160         }
161         
162         boost::shared_ptr<Patch> find_patch(const PatchPrimaryKey& key) {
163                 assert(key.is_sane());
164                 return _patch_map[key];
165         }
166         
167         boost::shared_ptr<Patch> previous_patch(const PatchPrimaryKey& key) {
168                 assert(key.is_sane());
169                 for (PatchList::const_iterator i = _patch_list.begin();
170                          i != _patch_list.end();
171                          ++i) {
172                         if ((*i) == key) {
173                                 if (i != _patch_list.begin()) {
174                                         --i;
175                                         return  _patch_map[*i];
176                                 } 
177                         }
178                 }
179                         
180                 return boost::shared_ptr<Patch>();
181         }
182         
183         boost::shared_ptr<Patch> next_patch(const PatchPrimaryKey& key) {
184                 assert(key.is_sane());
185                 for (PatchList::const_iterator i = _patch_list.begin();
186                          i != _patch_list.end();
187                          ++i) {
188                         if ((*i) == key) {
189                                 if (++i != _patch_list.end()) {
190                                         return  _patch_map[*i];
191                                 } else {
192                                         --i;
193                                 }
194                         }
195                 }
196                         
197                 return boost::shared_ptr<Patch>();
198         }
199
200         const std::string& note_list_name()    const { return _note_list_name; }
201         const std::string& control_list_name() const { return _control_list_name; }
202
203         XMLNode& get_state (void);
204         int      set_state (const XMLTree&, const XMLNode&);
205
206         void set_patch_banks (const PatchBanks&);
207         void use_patch_name_list (const PatchNameList&);
208
209 private:
210         friend std::ostream& operator<< (std::ostream&, const ChannelNameSet&);
211
212         std::string          _name;
213         AvailableForChannels _available_for_channels;
214         PatchBanks           _patch_banks;
215         PatchMap             _patch_map;
216         PatchList            _patch_list;
217         std::string          _patch_list_name;
218         std::string          _note_list_name;
219         std::string          _control_list_name;
220 };
221
222 std::ostream& operator<< (std::ostream&, const ChannelNameSet&);
223
224 class Note
225 {
226 public:
227         Note() {}
228         Note(uint8_t number, const std::string& name) : _number(number), _name(name) {}
229
230         const std::string& name() const        { return _name; }
231         void set_name(const std::string& name) { _name = name; }
232
233         uint8_t number() const             { return _number; }
234         void    set_number(uint8_t number) { _number = number; }
235
236         XMLNode& get_state (void);
237         int      set_state (const XMLTree&, const XMLNode&);
238
239 private:
240         uint8_t     _number;
241         std::string _name;
242 };
243
244 class NoteNameList 
245 {
246 public:
247         typedef std::vector< boost::shared_ptr<Note> > Notes;
248
249         NoteNameList() { _notes.resize(128); }
250         NoteNameList (const std::string& name) : _name(name) { _notes.resize(128); }
251
252         const std::string& name() const  { return _name; }
253         const Notes&       notes() const { return _notes; }
254
255         void set_name(const std::string& name) { _name = name; }
256
257         XMLNode& get_state (void);
258         int      set_state (const XMLTree&, const XMLNode&);
259
260 private:
261         std::string _name;
262         Notes       _notes;
263 };
264
265 class Control
266 {
267 public:
268         Control() {}
269         Control(const std::string& type,
270                 const uint16_t     number,
271                 const std::string& name)
272                 : _type(type)
273                 , _number(number)
274                 , _name(name)
275         {}
276
277         const std::string& type()   const { return _type; }
278         uint16_t           number() const { return _number; }
279         const std::string& name()   const { return _name; }
280
281         void set_type(const std::string& type) { _type = type; }
282         void set_number(uint16_t number)       { _number = number; }
283         void set_name(const std::string& name) { _name = name; }
284
285         XMLNode& get_state(void);
286         int      set_state(const XMLTree&, const XMLNode&);
287
288 private:
289         std::string _type;
290         uint16_t    _number;
291         std::string _name;
292 };
293
294 class ControlNameList 
295 {
296 public:
297         typedef std::map<uint16_t, boost::shared_ptr<Control> > Controls;
298
299         ControlNameList() {}
300         ControlNameList(const std::string& name) : _name(name) {}
301
302         const std::string& name() const { return _name; }
303
304         void set_name(const std::string& name) { _name = name; }
305
306         boost::shared_ptr<const Control> control(uint16_t num) const;
307
308         const Controls& controls() const { return _controls; }
309
310         XMLNode& get_state(void);
311         int      set_state(const XMLTree&, const XMLNode&);
312
313 private:
314         std::string _name;
315         Controls    _controls;
316 };
317
318 class CustomDeviceMode
319 {
320 public:
321         CustomDeviceMode() {};
322         virtual ~CustomDeviceMode() {};
323
324         const std::string& name() const        { return _name; }
325         void set_name(const std::string& name) { _name = name; }
326
327         
328         XMLNode& get_state (void);
329         int      set_state (const XMLTree&, const XMLNode&);
330         
331         /// Note: channel here is 0-based while in the MIDNAM-file it's 1-based
332         const std::string& channel_name_set_name_by_channel(uint8_t channel) {
333                 assert(channel <= 15);
334                 return _channel_name_set_assignments[channel]; 
335         }
336         
337 private:
338         /// array index = channel number
339         /// string contents = name of channel name set 
340         std::string _name;
341         std::string _channel_name_set_assignments[16];
342 };
343
344 class MasterDeviceNames
345 {
346 public:
347         typedef std::set<std::string>                                       Models;
348         /// maps name to CustomDeviceMode
349         typedef std::map<std::string, boost::shared_ptr<CustomDeviceMode> > CustomDeviceModes;
350         typedef std::list<std::string>                                      CustomDeviceModeNames;
351         /// maps name to ChannelNameSet
352         typedef std::map<std::string, boost::shared_ptr<ChannelNameSet> >   ChannelNameSets;
353         typedef std::map<std::string, boost::shared_ptr<NoteNameList> >     NoteNameLists;
354         typedef std::map<std::string, boost::shared_ptr<ControlNameList> >  ControlNameLists;
355         typedef std::map<std::string, PatchNameList>                        PatchNameLists;
356         
357         MasterDeviceNames() {};
358         virtual ~MasterDeviceNames() {};
359         
360         const std::string& manufacturer() const { return _manufacturer; }
361         void set_manufacturer(const std::string& manufacturer) { _manufacturer = manufacturer; }
362         
363         const Models& models() const { return _models; }
364         void set_models(const Models some_models) { _models = some_models; }
365
366         const ControlNameLists& controls() const { return _control_name_lists; }
367
368         const CustomDeviceModeNames& custom_device_mode_names() const { return _custom_device_mode_names; }
369         
370         boost::shared_ptr<CustomDeviceMode> custom_device_mode_by_name(const std::string& mode_name);
371         boost::shared_ptr<ChannelNameSet> channel_name_set_by_device_mode_and_channel(const std::string& mode, uint8_t channel);
372         boost::shared_ptr<Patch> find_patch(const std::string& mode, uint8_t channel, const PatchPrimaryKey& key);
373
374         boost::shared_ptr<ControlNameList> control_name_list(const std::string& name);
375         boost::shared_ptr<NoteNameList>    note_name_list(const std::string& name);
376         boost::shared_ptr<ChannelNameSet>  channel_name_set(const std::string& name);
377
378         std::string note_name(const std::string& mode_name,
379                               uint8_t            channel,
380                               uint16_t           bank,
381                               uint8_t            program,
382                               uint8_t            number);
383
384         XMLNode& get_state (void);
385         int      set_state (const XMLTree&, const XMLNode&);
386         
387 private:
388         std::string           _manufacturer;
389         Models                _models;
390         CustomDeviceModes     _custom_device_modes;
391         CustomDeviceModeNames _custom_device_mode_names;
392         ChannelNameSets       _channel_name_sets;
393         NoteNameLists         _note_name_lists;
394         PatchNameLists        _patch_name_lists;
395         ControlNameLists      _control_name_lists;
396 };
397
398 class MIDINameDocument
399 {
400 public:
401         // Maps Model names to MasterDeviceNames
402         typedef std::map<std::string, boost::shared_ptr<MasterDeviceNames> > MasterDeviceNamesList;
403         
404         MIDINameDocument() {}
405         MIDINameDocument(const std::string& filename);
406         virtual ~MIDINameDocument() {};
407
408         const std::string& author() const { return _author; }
409         void set_author(const std::string& author) { _author = author; }
410         
411         boost::shared_ptr<MasterDeviceNames> master_device_names(const std::string& model);
412
413         const MasterDeviceNamesList& master_device_names_by_model() const { return _master_device_names_list; }
414         
415         const MasterDeviceNames::Models& all_models() const { return _all_models; }
416                 
417         XMLNode& get_state (void);
418         int      set_state (const XMLTree&, const XMLNode&);
419
420 private:
421         std::string                   _author;
422         MasterDeviceNamesList         _master_device_names_list;
423         XMLTree                       _document;
424         MasterDeviceNames::Models     _all_models;
425 };
426
427 extern const char* general_midi_program_names[128]; /* 0 .. 127 */
428
429 }
430
431 }
432 #endif /*MIDNAM_PATCH_H_*/