ff8150ff1709c9daa9f3723de5a241288b1bcb1d
[ardour.git] / libs / ardour / ardour / midi_patch_manager.h
1 /*
2  * Copyright (C) 2008 Hans Baier <hansfbaier@googlemail.com>
3  * Copyright (C) 2009-2014 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2014 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2015-2016 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifndef MIDI_PATCH_MANAGER_H_
24 #define MIDI_PATCH_MANAGER_H_
25
26 #include "midi++/midnam_patch.h"
27
28 #include "pbd/signals.h"
29 #include "pbd/search_path.h"
30
31 #include "ardour/libardour_visibility.h"
32
33 namespace MIDI
34 {
35
36 namespace Name
37 {
38
39 class LIBARDOUR_API MidiPatchManager
40 {
41         /// Singleton
42 private:
43         MidiPatchManager();
44         MidiPatchManager( const MidiPatchManager& );
45         MidiPatchManager& operator= (const MidiPatchManager&);
46
47         static MidiPatchManager* _manager;
48
49 public:
50         typedef std::map<std::string, boost::shared_ptr<MIDINameDocument> >    MidiNameDocuments;
51         typedef std::map<std::string, MIDINameDocument::MasterDeviceNamesList> DeviceNamesByMaker;
52
53         virtual ~MidiPatchManager() { _manager = 0; }
54
55         static MidiPatchManager& instance() {
56                 if (_manager == 0) {
57                         _manager = new MidiPatchManager();
58                 }
59                 return *_manager;
60         }
61
62         PBD::Signal0<void> PatchesChanged;
63
64         bool add_custom_midnam (const std::string& id, const std::string& midnam);
65         bool update_custom_midnam (const std::string& id, const std::string& midnam);
66         bool remove_custom_midnam (const std::string& id);
67         bool is_custom_model (const std::string& model) const;
68
69         void add_search_path (const PBD::Searchpath& search_path);
70
71         void remove_search_path (const PBD::Searchpath& search_path);
72
73         boost::shared_ptr<MIDINameDocument> document_by_model(std::string model_name) const;
74
75         boost::shared_ptr<MasterDeviceNames> master_device_by_model(std::string model_name)
76                 { return _master_devices_by_model[model_name]; }
77
78         boost::shared_ptr<ChannelNameSet> find_channel_name_set(
79                         std::string model,
80                         std::string custom_device_mode,
81                         uint8_t channel) {
82                 boost::shared_ptr<MIDI::Name::MasterDeviceNames> master_device = master_device_by_model(model);
83
84                 if (master_device != 0 && custom_device_mode != "") {
85                         return master_device->channel_name_set_by_channel(custom_device_mode, channel);
86                 } else {
87                         return boost::shared_ptr<ChannelNameSet>();
88                 }
89         }
90
91         boost::shared_ptr<Patch> find_patch(
92                         std::string model,
93                         std::string custom_device_mode,
94                         uint8_t channel,
95                         PatchPrimaryKey patch_key) {
96
97                 boost::shared_ptr<ChannelNameSet> channel_name_set = find_channel_name_set(model, custom_device_mode, channel);
98
99                 if (channel_name_set) {
100                         return  channel_name_set->find_patch(patch_key);
101                 } else {
102                         return boost::shared_ptr<Patch>();
103                 }
104         }
105
106         boost::shared_ptr<Patch> previous_patch(
107                         std::string model,
108                         std::string custom_device_mode,
109                         uint8_t channel,
110                         PatchPrimaryKey patch_key) {
111
112                 boost::shared_ptr<ChannelNameSet> channel_name_set = find_channel_name_set(model, custom_device_mode, channel);
113
114                 if (channel_name_set) {
115                         return  channel_name_set->previous_patch(patch_key);
116                 } else {
117                         return boost::shared_ptr<Patch>();
118                 }
119         }
120
121         boost::shared_ptr<Patch> next_patch(
122                         std::string model,
123                         std::string custom_device_mode,
124                         uint8_t channel,
125                         PatchPrimaryKey patch_key) {
126
127                 boost::shared_ptr<ChannelNameSet> channel_name_set = find_channel_name_set(model, custom_device_mode, channel);
128
129                 if (channel_name_set) {
130                         return  channel_name_set->next_patch(patch_key);
131                 } else {
132                         return boost::shared_ptr<Patch>();
133                 }
134         }
135
136         std::list<std::string> custom_device_mode_names_by_model(std::string model_name) {
137                 if (model_name != "") {
138                         if (master_device_by_model(model_name)) {
139                                 return master_device_by_model(model_name)->custom_device_mode_names();
140                         }
141                 }
142                 return std::list<std::string>();
143         }
144
145         const MasterDeviceNames::Models& all_models() const { return _all_models; }
146
147         const DeviceNamesByMaker& devices_by_manufacturer() const { return _devices_by_manufacturer; }
148
149 private:
150         bool load_midi_name_document(const std::string& file_path);
151         bool add_midi_name_document(boost::shared_ptr<MIDINameDocument>);
152         bool remove_midi_name_document(const std::string& file_path, bool emit_signal = true);
153
154         void add_midnam_files_from_directory(const std::string& directory_path);
155         void remove_midnam_files_from_directory(const std::string& directory_path);
156
157 private:
158         PBD::Searchpath                         _search_path;
159
160         MidiNameDocuments                       _documents;
161         MIDINameDocument::MasterDeviceNamesList _master_devices_by_model;
162         DeviceNamesByMaker                      _devices_by_manufacturer;
163         MasterDeviceNames::Models               _all_models;
164 };
165
166 } // namespace Name
167
168 } // namespace MIDI
169
170 #endif /* MIDI_PATCH_MANAGER_H_ */