93f0b7e2356dd16b2abee23ede908ee89de9bd03
[ardour.git] / libs / ardour / midi_patch_manager.cc
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 #include <boost/shared_ptr.hpp>
22
23 #include <glibmm/fileutils.h>
24
25 #include "pbd/file_utils.h"
26 #include "pbd/error.h"
27
28 #include "ardour/session.h"
29 #include "ardour/session_directory.h"
30 #include "ardour/midi_patch_manager.h"
31
32 #include "ardour/search_paths.h"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace MIDI;
39 using namespace MIDI::Name;
40 using namespace PBD;
41
42 MidiPatchManager* MidiPatchManager::_manager = 0;
43
44 MidiPatchManager::MidiPatchManager ()
45 {
46         refresh ();
47 }
48
49 void
50 MidiPatchManager::set_session (Session* s)
51 {
52         SessionHandlePtr::set_session (s);
53         refresh ();
54         add_session_patches ();
55 }
56
57 void
58 MidiPatchManager::add_session_patches ()
59 {
60         if (!_session) {
61                 return;
62         }
63         
64         std::string path_to_patches = _session->session_directory().midi_patch_path();
65
66         if (!Glib::file_test (path_to_patches, Glib::FILE_TEST_EXISTS)) {
67                 return;
68         }
69
70         assert (Glib::file_test (path_to_patches, Glib::FILE_TEST_IS_DIR));
71
72         vector<std::string> result;
73
74         find_files_matching_pattern (result, path_to_patches, "*.midnam");
75
76         info << "Loading " << result.size() << " MIDI patches from " << path_to_patches << endmsg;
77
78         for (vector<std::string>::iterator i = result.begin(); i != result.end(); ++i) {
79                 boost::shared_ptr<MIDINameDocument> document(new MIDINameDocument(*i));
80                 for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
81                                         document->master_device_names_by_model().begin();
82                                 device != document->master_device_names_by_model().end();
83                                 ++device) {
84                         //cerr << "got model " << device->first << endl;
85                         // have access to the documents by model name
86                         _documents[device->first] = document;
87                         // build a list of all master devices from all documents
88                         _master_devices_by_model[device->first] = device->second;
89                         _all_models.insert(device->first);
90                         const std::string& manufacturer = device->second->manufacturer();
91                         if (_devices_by_manufacturer.find(manufacturer) == _devices_by_manufacturer.end()) {
92                                 MIDINameDocument::MasterDeviceNamesList empty;
93                                 _devices_by_manufacturer.insert(std::make_pair(manufacturer, empty));
94                         }
95                         _devices_by_manufacturer[manufacturer].insert(std::make_pair(device->first, device->second));
96
97                         // make sure there are no double model names
98                         // TODO: handle this gracefully.
99                         assert(_documents.count(device->first) == 1);
100                         assert(_master_devices_by_model.count(device->first) == 1);
101                 }
102         }
103 }
104
105 void
106 MidiPatchManager::refresh()
107 {
108         _documents.clear();
109         _master_devices_by_model.clear();
110         _all_models.clear();
111         _devices_by_manufacturer.clear();
112
113         Searchpath search_path = midi_patch_search_path ();
114         vector<std::string> result;
115
116         find_files_matching_pattern (result, search_path, "*.midnam");
117
118         info << "Loading " << result.size() << " MIDI patches from " << search_path.to_string() << endmsg;
119
120         for (vector<std::string>::iterator i = result.begin(); i != result.end(); ++i) {
121                 boost::shared_ptr<MIDINameDocument> document;
122                 try {
123                         document = boost::shared_ptr<MIDINameDocument>(new MIDINameDocument(*i));
124                 } catch (...) {
125                         error << "Error parsing MIDI patch file " << *i << endmsg;
126                         continue;
127                 }
128                 for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
129                              document->master_device_names_by_model().begin();
130                      device != document->master_device_names_by_model().end();
131                      ++device) {
132                         if (_documents.find(device->first) != _documents.end()) {
133                                 warning << string_compose(_("Duplicate MIDI device `%1' in `%2' ignored"),
134                                                           device->first, *i)
135                                         << endmsg;
136                                 continue;
137                         }
138                                 
139                         _documents[device->first]               = document;
140                         _master_devices_by_model[device->first] = device->second;
141
142                         _all_models.insert(device->first);
143                         const std::string& manufacturer = device->second->manufacturer();
144                         if (_devices_by_manufacturer.find(manufacturer) == _devices_by_manufacturer.end()) {
145                                 MIDINameDocument::MasterDeviceNamesList empty;
146                                 _devices_by_manufacturer.insert(std::make_pair(manufacturer, empty));
147                         }
148                         _devices_by_manufacturer[manufacturer].insert(std::make_pair(device->first, device->second));
149                 }
150         }
151
152         if (_session) {
153                 add_session_patches ();
154         }
155 }
156
157 void
158 MidiPatchManager::session_going_away ()
159 {
160         SessionHandlePtr::session_going_away ();
161         _documents.clear();
162         _master_devices_by_model.clear();
163         _all_models.clear();
164         _devices_by_manufacturer.clear();
165 }