2 * Copyright (C) 2008 Hans Baier <hansfbaier@googlemail.com>
3 * Copyright (C) 2009-2014 David Robillard <d@drobilla.net>
4 * Copyright (C) 2009-2016 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2012-2015 Tim Mayberry <mojofunk@gmail.com>
6 * Copyright (C) 2014-2016 Robin Gareus <robin@gareus.org>
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.
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.
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.
23 #include <boost/shared_ptr.hpp>
25 #include <glibmm/fileutils.h>
27 #include "pbd/file_utils.h"
28 #include "pbd/error.h"
30 #include "ardour/midi_patch_manager.h"
32 #include "ardour/search_paths.h"
37 using namespace ARDOUR;
39 using namespace MIDI::Name;
42 MidiPatchManager* MidiPatchManager::_manager = 0;
44 MidiPatchManager::MidiPatchManager ()
46 add_search_path(midi_patch_search_path ());
50 MidiPatchManager::add_search_path (const Searchpath& search_path)
52 for (Searchpath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
54 if (_search_path.contains(*i)) {
55 // already processed files from this path
59 if (!Glib::file_test (*i, Glib::FILE_TEST_EXISTS)) {
63 if (!Glib::file_test (*i, Glib::FILE_TEST_IS_DIR)) {
67 add_midnam_files_from_directory (*i);
69 _search_path.add_directory (*i);
74 MidiPatchManager::add_custom_midnam (const std::string& id, const std::string& midnam)
76 boost::shared_ptr<MIDINameDocument> document;
77 document = boost::shared_ptr<MIDINameDocument>(new MIDINameDocument());
79 if (mxml.read_buffer (midnam, true)) {
80 if (0 == document->set_state (mxml, *mxml.root())) {
81 document->set_file_path ("custom:" + id);
82 add_midi_name_document (document);
90 MidiPatchManager::remove_custom_midnam (const std::string& id)
92 return remove_midi_name_document ("custom:" + id);
96 MidiPatchManager::update_custom_midnam (const std::string& id, const std::string& midnam)
98 remove_midi_name_document ("custom:" + id, false);
99 return add_custom_midnam (id, midnam);
103 MidiPatchManager::add_midnam_files_from_directory(const std::string& directory_path)
105 vector<std::string> result;
106 find_files_matching_pattern (result, directory_path, "*.midnam");
108 info << string_compose(
109 P_("Loading %1 MIDI patch from %2", "Loading %1 MIDI patches from %2", result.size()),
110 result.size(), directory_path)
113 for (vector<std::string>::const_iterator i = result.begin(); i != result.end(); ++i) {
114 load_midi_name_document (*i);
119 MidiPatchManager::remove_search_path (const Searchpath& search_path)
121 for (Searchpath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
123 if (!_search_path.contains(*i)) {
127 remove_midnam_files_from_directory(*i);
129 _search_path.remove_directory (*i);
134 MidiPatchManager::remove_midnam_files_from_directory(const std::string& directory_path)
136 vector<std::string> result;
137 find_files_matching_pattern (result, directory_path, "*.midnam");
139 info << string_compose(
140 P_("Unloading %1 MIDI patch from %2", "Unloading %1 MIDI patches from %2", result.size()),
141 result.size(), directory_path)
144 for (vector<std::string>::const_iterator i = result.begin(); i != result.end(); ++i) {
145 remove_midi_name_document (*i);
150 MidiPatchManager::load_midi_name_document (const std::string& file_path)
152 boost::shared_ptr<MIDINameDocument> document;
154 document = boost::shared_ptr<MIDINameDocument>(new MIDINameDocument(file_path));
157 error << string_compose(_("Error parsing MIDI patch file %1"), file_path)
161 return add_midi_name_document (document);
164 boost::shared_ptr<MIDINameDocument>
165 MidiPatchManager::document_by_model(std::string model_name) const
167 MidiNameDocuments::const_iterator i = _documents.find (model_name);
168 if (i != _documents.end ()) {
171 return boost::shared_ptr<MIDINameDocument> ();
175 MidiPatchManager::add_midi_name_document (boost::shared_ptr<MIDINameDocument> document)
178 for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
179 document->master_device_names_by_model().begin();
180 device != document->master_device_names_by_model().end();
182 if (_documents.find(device->first) != _documents.end()) {
183 warning << string_compose(_("Duplicate MIDI device `%1' in `%2' ignored"),
185 document->file_path()) << endmsg;
189 _documents[device->first] = document;
190 _master_devices_by_model[device->first] = device->second;
192 _all_models.insert(device->first);
193 const std::string& manufacturer = device->second->manufacturer();
194 if (_devices_by_manufacturer.find(manufacturer) ==
195 _devices_by_manufacturer.end()) {
196 MIDINameDocument::MasterDeviceNamesList empty;
197 _devices_by_manufacturer.insert(std::make_pair(manufacturer, empty));
199 _devices_by_manufacturer[manufacturer].insert(
200 std::make_pair(device->first, device->second));
203 // TODO: handle this gracefully.
204 assert(_documents.count(device->first) == 1);
205 assert(_master_devices_by_model.count(device->first) == 1);
209 PatchesChanged(); /* EMIT SIGNAL */
215 MidiPatchManager::remove_midi_name_document (const std::string& file_path, bool emit_signal)
217 bool removed = false;
218 for (MidiNameDocuments::iterator i = _documents.begin(); i != _documents.end();) {
219 if (i->second->file_path() == file_path) {
221 boost::shared_ptr<MIDINameDocument> document = i->second;
223 info << string_compose(_("Removing MIDI patch file %1"), file_path) << endmsg;
225 _documents.erase(i++);
227 for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
228 document->master_device_names_by_model().begin();
229 device != document->master_device_names_by_model().end();
232 _master_devices_by_model.erase(device->first);
234 _all_models.erase(device->first);
236 const std::string& manufacturer = device->second->manufacturer();
238 _devices_by_manufacturer[manufacturer].erase(device->first);
245 if (removed && emit_signal) {
246 PatchesChanged(); /* EMIT SIGNAL */