Ardour  9.0-pre0-582-g084a23a80d
midi_patch_manager.h
Go to the documentation of this file.
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/event_loop.h"
29 #include "pbd/pthread_utils.h"
30 #include "pbd/signals.h"
31 #include "pbd/search_path.h"
32 
34 
35 namespace MIDI
36 {
37 
38 namespace Name
39 {
40 
42 {
44 private:
47  MidiPatchManager& operator= (const MidiPatchManager&);
48 
50 
51 public:
52  typedef std::map<std::string, std::shared_ptr<MIDINameDocument> > MidiNameDocuments;
53  typedef std::map<std::string, MIDINameDocument::MasterDeviceNamesList> DeviceNamesByMaker;
54 
56 
58  if (_manager == 0) {
59  _manager = new MidiPatchManager();
60  }
61  return *_manager;
62  }
63 
65 
66  bool add_custom_midnam (const std::string& id, char const*);
67  bool update_custom_midnam (const std::string& id, char const*);
68  bool remove_custom_midnam (const std::string& id);
69  bool is_custom_model (const std::string& model) const;
70 
71  void add_search_path (const PBD::Searchpath& search_path);
72 
73  void remove_search_path (const PBD::Searchpath& search_path);
74 
75  std::shared_ptr<MIDINameDocument> document_by_model(std::string model_name) const;
76 
77  std::shared_ptr<MasterDeviceNames> master_device_by_model(std::string model_name)
78  { return _master_devices_by_model[model_name]; }
79 
80  std::shared_ptr<ChannelNameSet> find_channel_name_set(
81  std::string model,
82  std::string custom_device_mode,
83  uint8_t channel) {
84  std::shared_ptr<MIDI::Name::MasterDeviceNames> master_device = master_device_by_model(model);
85 
86  if (master_device != 0 && custom_device_mode != "") {
87  return master_device->channel_name_set_by_channel(custom_device_mode, channel);
88  } else {
89  return std::shared_ptr<ChannelNameSet>();
90  }
91  }
92 
93  std::shared_ptr<Patch> find_patch(
94  std::string model,
95  std::string custom_device_mode,
96  uint8_t channel,
97  PatchPrimaryKey patch_key) {
98 
99  std::shared_ptr<ChannelNameSet> channel_name_set = find_channel_name_set(model, custom_device_mode, channel);
100 
101  if (channel_name_set) {
102  return channel_name_set->find_patch(patch_key);
103  } else {
104  return std::shared_ptr<Patch>();
105  }
106  }
107 
108  std::shared_ptr<Patch> previous_patch(
109  std::string model,
110  std::string custom_device_mode,
111  uint8_t channel,
112  PatchPrimaryKey patch_key) {
113 
114  std::shared_ptr<ChannelNameSet> channel_name_set = find_channel_name_set(model, custom_device_mode, channel);
115 
116  if (channel_name_set) {
117  return channel_name_set->previous_patch(patch_key);
118  } else {
119  return std::shared_ptr<Patch>();
120  }
121  }
122 
123  std::shared_ptr<Patch> next_patch(
124  std::string model,
125  std::string custom_device_mode,
126  uint8_t channel,
127  PatchPrimaryKey patch_key) {
128 
129  std::shared_ptr<ChannelNameSet> channel_name_set = find_channel_name_set(model, custom_device_mode, channel);
130 
131  if (channel_name_set) {
132  return channel_name_set->next_patch(patch_key);
133  } else {
134  return std::shared_ptr<Patch>();
135  }
136  }
137 
138  std::list<std::string> custom_device_mode_names_by_model(std::string model_name) {
139  if (model_name != "") {
140  if (master_device_by_model(model_name)) {
141  return master_device_by_model(model_name)->custom_device_mode_names();
142  }
143  }
144  return std::list<std::string>();
145  }
146 
147  const MasterDeviceNames::Models& all_models() const { return _all_models; }
148 
149  const DeviceNamesByMaker& devices_by_manufacturer() const { return _devices_by_manufacturer; }
150 
154  const std::function<void()>& slot,
155  PBD::EventLoop* event_loop);
156 private:
157  bool load_midi_name_document(const std::string& file_path);
158  bool add_midi_name_document(std::shared_ptr<MIDINameDocument>);
159  bool remove_midi_name_document(const std::string& file_path, bool emit_signal = true);
160 
161  void add_midnam_files_from_directory(const std::string& directory_path);
162  void remove_midnam_files_from_directory(const std::string& directory_path);
163 
164 private:
166 
171 
172  Glib::Threads::Mutex _lock;
176  void load_midnams ();
177 };
178 
179 } // namespace Name
180 
181 } // namespace MIDI
182 
183 #endif /* MIDI_PATCH_MANAGER_H_ */
std::map< std::string, std::shared_ptr< MasterDeviceNames > > MasterDeviceNamesList
Definition: midnam_patch.h:472
std::set< std::string > Models
Definition: midnam_patch.h:408
bool remove_custom_midnam(const std::string &id)
const DeviceNamesByMaker & devices_by_manufacturer() const
MidiPatchManager(const MidiPatchManager &)
void remove_search_path(const PBD::Searchpath &search_path)
bool add_custom_midnam(const std::string &id, char const *)
std::map< std::string, std::shared_ptr< MIDINameDocument > > MidiNameDocuments
bool remove_midi_name_document(const std::string &file_path, bool emit_signal=true)
static MidiPatchManager & instance()
void add_midnam_files_from_directory(const std::string &directory_path)
std::map< std::string, MIDINameDocument::MasterDeviceNamesList > DeviceNamesByMaker
void remove_midnam_files_from_directory(const std::string &directory_path)
std::shared_ptr< ChannelNameSet > find_channel_name_set(std::string model, std::string custom_device_mode, uint8_t channel)
MIDINameDocument::MasterDeviceNamesList _master_devices_by_model
MasterDeviceNames::Models _all_models
bool load_midi_name_document(const std::string &file_path)
bool add_midi_name_document(std::shared_ptr< MIDINameDocument >)
static MidiPatchManager * _manager
std::shared_ptr< MIDINameDocument > document_by_model(std::string model_name) const
std::shared_ptr< Patch > find_patch(std::string model, std::string custom_device_mode, uint8_t channel, PatchPrimaryKey patch_key)
void add_search_path(const PBD::Searchpath &search_path)
bool is_custom_model(const std::string &model) const
std::shared_ptr< MasterDeviceNames > master_device_by_model(std::string model_name)
std::list< std::string > custom_device_mode_names_by_model(std::string model_name)
DeviceNamesByMaker _devices_by_manufacturer
const MasterDeviceNames::Models & all_models() const
std::shared_ptr< Patch > previous_patch(std::string model, std::string custom_device_mode, uint8_t channel, PatchPrimaryKey patch_key)
bool update_custom_midnam(const std::string &id, char const *)
void maybe_use(PBD::ScopedConnectionList &clist, PBD::EventLoop::InvalidationRecord *ir, const std::function< void()> &slot, PBD::EventLoop *event_loop)
std::shared_ptr< Patch > next_patch(std::string model, std::string custom_device_mode, uint8_t channel, PatchPrimaryKey patch_key)
PBD::Signal< void()> PatchesChanged
#define LIBARDOUR_API