Allow to dynamically un/load Midnam Patches
[ardour.git] / libs / ardour / ardour / midi_patch_manager.h
1 /*
2     Copyright (C) 2008 Hans Baier
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     $Id$
19 */
20
21 #ifndef MIDI_PATCH_MANAGER_H_
22 #define MIDI_PATCH_MANAGER_H_
23
24 #include "midi++/midnam_patch.h"
25
26 #include "pbd/signals.h"
27 #include "pbd/search_path.h"
28
29 #include "ardour/libardour_visibility.h"
30
31 namespace MIDI
32 {
33
34 namespace Name
35 {
36
37 class LIBARDOUR_API MidiPatchManager
38 {
39         /// Singleton
40 private:
41         MidiPatchManager();
42         MidiPatchManager( const MidiPatchManager& );
43         MidiPatchManager& operator= (const MidiPatchManager&);
44
45         static MidiPatchManager* _manager;
46
47 public:
48         typedef std::map<std::string, boost::shared_ptr<MIDINameDocument> >    MidiNameDocuments;
49         typedef std::map<std::string, MIDINameDocument::MasterDeviceNamesList> DeviceNamesByMaker;
50
51         virtual ~MidiPatchManager() { _manager = 0; }
52
53         static MidiPatchManager& instance() {
54                 if (_manager == 0) {
55                         _manager = new MidiPatchManager();
56                 }
57                 return *_manager;
58         }
59
60         PBD::Signal0<void> PatchesChanged;
61
62         bool add_custom_midnam (const std::string& id, const std::string& midnam);
63         bool remove_custom_midnam (const std::string& id);
64
65         void add_search_path (const PBD::Searchpath& search_path);
66
67         void remove_search_path (const PBD::Searchpath& search_path);
68
69         boost::shared_ptr<MIDINameDocument> document_by_model(std::string model_name) const;
70
71         boost::shared_ptr<MasterDeviceNames> master_device_by_model(std::string model_name)
72                 { return _master_devices_by_model[model_name]; }
73
74         boost::shared_ptr<ChannelNameSet> find_channel_name_set(
75                         std::string model,
76                         std::string custom_device_mode,
77                         uint8_t channel) {
78                 boost::shared_ptr<MIDI::Name::MasterDeviceNames> master_device = master_device_by_model(model);
79
80                 if (master_device != 0 && custom_device_mode != "") {
81                         return master_device->channel_name_set_by_channel(custom_device_mode, channel);
82                 } else {
83                         return boost::shared_ptr<ChannelNameSet>();
84                 }
85         }
86
87         boost::shared_ptr<Patch> find_patch(
88                         std::string model,
89                         std::string custom_device_mode,
90                         uint8_t channel,
91                         PatchPrimaryKey patch_key) {
92
93                 boost::shared_ptr<ChannelNameSet> channel_name_set = find_channel_name_set(model, custom_device_mode, channel);
94
95                 if (channel_name_set) {
96                         return  channel_name_set->find_patch(patch_key);
97                 } else {
98                         return boost::shared_ptr<Patch>();
99                 }
100         }
101
102         boost::shared_ptr<Patch> previous_patch(
103                         std::string model,
104                         std::string custom_device_mode,
105                         uint8_t channel,
106                         PatchPrimaryKey patch_key) {
107
108                 boost::shared_ptr<ChannelNameSet> channel_name_set = find_channel_name_set(model, custom_device_mode, channel);
109
110                 if (channel_name_set) {
111                         return  channel_name_set->previous_patch(patch_key);
112                 } else {
113                         return boost::shared_ptr<Patch>();
114                 }
115         }
116
117         boost::shared_ptr<Patch> next_patch(
118                         std::string model,
119                         std::string custom_device_mode,
120                         uint8_t channel,
121                         PatchPrimaryKey patch_key) {
122
123                 boost::shared_ptr<ChannelNameSet> channel_name_set = find_channel_name_set(model, custom_device_mode, channel);
124
125                 if (channel_name_set) {
126                         return  channel_name_set->next_patch(patch_key);
127                 } else {
128                         return boost::shared_ptr<Patch>();
129                 }
130         }
131
132         std::list<std::string> custom_device_mode_names_by_model(std::string model_name) {
133                 if (model_name != "") {
134                         if (master_device_by_model(model_name)) {
135                                 return master_device_by_model(model_name)->custom_device_mode_names();
136                         }
137                 }
138                 return std::list<std::string>();
139         }
140
141         const MasterDeviceNames::Models& all_models() const { return _all_models; }
142
143         const DeviceNamesByMaker& devices_by_manufacturer() const { return _devices_by_manufacturer; }
144
145 private:
146         bool load_midi_name_document(const std::string& file_path);
147         bool add_midi_name_document(boost::shared_ptr<MIDINameDocument>);
148         bool remove_midi_name_document(const std::string& file_path);
149
150         void add_midnam_files_from_directory(const std::string& directory_path);
151         void remove_midnam_files_from_directory(const std::string& directory_path);
152
153 private:
154         PBD::Searchpath                         _search_path;
155
156         MidiNameDocuments                       _documents;
157         MIDINameDocument::MasterDeviceNamesList _master_devices_by_model;
158         DeviceNamesByMaker                      _devices_by_manufacturer;
159         MasterDeviceNames::Models               _all_models;
160 };
161
162 } // namespace Name
163
164 } // namespace MIDI
165
166 #endif /* MIDI_PATCH_MANAGER_H_ */