2 Copyright (C) 2003 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <cstdio> // Needed so that libraptor (included in lrdf) won't complain
28 #include <sys/types.h>
34 #include <pbd/compose.h>
36 #include <ardour/ardour.h>
37 #include <ardour/configuration.h>
38 #include <ardour/audio_library.h>
39 #include <ardour/utils.h>
44 using namespace ARDOUR;
47 static char* SOUNDFILE = "http://ardour.org/ontology/Soundfile";
49 string AudioLibrary::state_node_name = "AudioLibrary";
51 AudioLibrary::AudioLibrary ()
53 // sfdb_paths.push_back("/Users/taybin/sounds");
55 src = "file:" + get_user_ardour_path() + "sfdb";
57 // workaround for possible bug in raptor that crashes when saving to a
59 touch_file(get_user_ardour_path() + "sfdb");
61 lrdf_read_file(src.c_str());
63 lrdf_statement pattern;
65 pattern.subject = SOUNDFILE;
66 pattern.predicate = RDF_TYPE;
67 pattern.object = RDFS_CLASS;
68 pattern.object_type = lrdf_uri;
70 lrdf_statement* matches = lrdf_matches(&pattern);
72 // if empty DB, create basic schema
78 lrdf_free_statements(matches);
81 AudioLibrary::~AudioLibrary ()
86 AudioLibrary::initialize_db ()
88 // define ardour:Soundfile
89 lrdf_add_triple(src.c_str(), SOUNDFILE, RDF_TYPE, RDFS_CLASS, lrdf_uri);
91 // add intergral fields
92 add_field(_("channels"));
93 add_field(_("samplerate"));
94 add_field(_("resolution"));
95 add_field(_("format"));
99 AudioLibrary::save_changes ()
101 if (lrdf_export_by_source(src.c_str(), src.substr(5).c_str())) {
102 warning << string_compose(_("Could not open %1. Audio Library not saved"), src) << endmsg;
107 AudioLibrary::add_member (string member)
109 string file_uri(string_compose("file:%1", member));
111 lrdf_add_triple(src.c_str(), file_uri.c_str(), RDF_TYPE,
112 SOUNDFILE, lrdf_uri);
116 AudioLibrary::remove_member (string uri)
118 lrdf_remove_uri_matches (uri.c_str());
122 AudioLibrary::search_members_and (vector<string>& members,
123 const map<string,string>& fields)
125 lrdf_statement **head;
126 lrdf_statement* pattern = 0;
127 lrdf_statement* old = 0;
130 map<string,string>::const_iterator i;
131 for (i = fields.begin(); i != fields.end(); ++i){
132 pattern = new lrdf_statement;
133 pattern->subject = "?";
134 pattern->predicate = strdup(field_uri(i->first).c_str());
135 pattern->object = strdup((i->second).c_str());
142 lrdf_uris* ulist = lrdf_match_multi(*head);
143 for (uint32_t j = 0; ulist && j < ulist->count; ++j) {
144 // printf("AND: %s\n", ulist->items[j]);
145 members.push_back(ulist->items[j]);
147 lrdf_free_uris(ulist);
149 compact_vector(members);
155 free(pattern->predicate);
156 free(pattern->object);
158 pattern = pattern->next;
164 AudioLibrary::search_members_or (vector<string>& members,
165 const map<string,string>& fields)
167 map<string,string>::const_iterator i;
169 lrdf_statement pattern;
170 for (i = fields.begin(); i != fields.end(); ++i) {
172 pattern.predicate = strdup(field_uri(i->first).c_str());
173 pattern.object = strdup((i->second).c_str());
174 pattern.object_type = lrdf_literal;
176 lrdf_statement* matched = lrdf_matches(&pattern);
178 lrdf_statement* old = matched;
180 // printf ("OR: %s\n", matched->subject);
181 members.push_back(matched->subject);
182 matched = matched->next;
185 free(pattern.predicate);
186 free(pattern.object);
187 lrdf_free_statements (old);
190 compact_vector(members);
194 AudioLibrary::add_field (string name)
196 string local_field = field_uri(name);
197 lrdf_statement pattern;
198 pattern.subject = strdup(local_field.c_str());
199 pattern.predicate = RDF_TYPE;
200 pattern.object = RDF_BASE "Property";
201 pattern.object_type = lrdf_uri;
203 if(lrdf_exists_match(&pattern)) {
207 // of type rdf:Property
208 lrdf_add_triple(src.c_str(), local_field.c_str(), RDF_TYPE,
209 RDF_BASE "Property", lrdf_uri);
210 // of range ardour:Soundfile
211 lrdf_add_triple(src.c_str(), local_field.c_str(), RDFS_BASE "range",
212 SOUNDFILE, lrdf_uri);
213 // of domain rdf:Literal
214 lrdf_add_triple(src.c_str(), local_field.c_str(), RDFS_BASE "domain",
215 RDF_BASE "Literal", lrdf_uri);
217 set_label (local_field, name);
219 fields_changed(); /* EMIT SIGNAL */
223 AudioLibrary::get_fields (vector<string>& fields)
225 lrdf_statement pattern;
228 pattern.predicate = RDFS_BASE "range";
229 pattern.object = SOUNDFILE;
230 pattern.object_type = lrdf_uri;
232 lrdf_statement* matches = lrdf_matches(&pattern);
234 lrdf_statement* current = matches;
235 while (current != 0) {
236 fields.push_back(get_label(current->subject));
238 current = current->next;
241 lrdf_free_statements(matches);
243 compact_vector(fields);
247 AudioLibrary::remove_field (string name)
249 lrdf_remove_uri_matches(field_uri(name).c_str());
250 fields_changed (); /* EMIT SIGNAL */
254 AudioLibrary::get_field (string uri, string field)
256 lrdf_statement pattern;
258 pattern.subject = strdup(uri.c_str());
260 pattern.predicate = strdup(field_uri(field).c_str());
263 pattern.object_type = lrdf_literal;
265 lrdf_statement* matches = lrdf_matches(&pattern);
266 free(pattern.subject);
267 free(pattern.predicate);
271 object << matches->object;
274 lrdf_free_statements(matches);
279 AudioLibrary::set_field (string uri, string field, string literal)
281 lrdf_statement pattern;
283 pattern.subject = strdup(uri.c_str());
285 string local_field = field_uri(field);
286 pattern.predicate = strdup(local_field.c_str());
289 pattern.object_type = lrdf_literal;
291 lrdf_remove_matches(&pattern);
292 free(pattern.subject);
293 free(pattern.predicate);
295 lrdf_add_triple(src.c_str(), uri.c_str(), local_field.c_str(),
296 literal.c_str(), lrdf_literal);
298 fields_changed(); /* EMIT SIGNAL */
302 AudioLibrary::field_uri (string name)
304 stringstream local_field;
305 local_field << "file:sfdb/fields/" << name;
307 return local_field.str();
311 AudioLibrary::get_label (string uri)
313 lrdf_statement pattern;
314 pattern.subject = strdup(uri.c_str());
315 pattern.predicate = RDFS_BASE "label";
317 pattern.object_type = lrdf_literal;
319 lrdf_statement* matches = lrdf_matches (&pattern);
320 free(pattern.subject);
324 label << matches->object;
327 lrdf_free_statements(matches);
333 AudioLibrary::set_label (string uri, string label)
335 lrdf_statement pattern;
336 pattern.subject = strdup(uri.c_str());
337 pattern.predicate = RDFS_BASE "label";
339 pattern.object_type = lrdf_literal;
341 lrdf_remove_matches(&pattern);
342 free(pattern.subject);
344 lrdf_add_triple(src.c_str(), uri.c_str(), RDFS_BASE "label",
345 label.c_str(), lrdf_literal);
349 AudioLibrary::compact_vector(vector<string>& vec)
351 sort(vec.begin(), vec.end());
352 unique(vec.begin(), vec.end());
356 AudioLibrary::set_paths (vector<string> paths)
364 AudioLibrary::get_paths ()
370 AudioLibrary::scan_paths ()
372 if (sfdb_paths.size() < 1) {
376 vector<char *> pathv(sfdb_paths.size());
378 for (i = 0; i < sfdb_paths.size(); ++i) {
379 pathv[i] = new char[sfdb_paths[i].length() +1];
380 sfdb_paths[i].copy(pathv[i], string::npos);
381 pathv[i][sfdb_paths[i].length()] = 0;
385 FTS* ft = fts_open(&pathv[0], FTS_LOGICAL|FTS_NOSTAT|FTS_PHYSICAL|FTS_XDEV, 0);
387 error << strerror(errno) << endmsg;
392 s.predicate = RDF_TYPE;
393 s.object = SOUNDFILE;
394 s.object_type = lrdf_uri;
396 while (FTSENT* file = fts_read(ft)) {
397 if ((file->fts_info & FTS_F) && (safe_file_extension(file->fts_name))) {
399 filename.append(file->fts_accpath);
400 s.subject = strdup(filename.c_str());
401 if (lrdf_exists_match(&s)) {
404 add_member(file->fts_accpath);
405 cout << file->fts_accpath << endl;
412 for (i = 0; i < pathv.size(); ++i) {
420 AudioLibrary::safe_file_extension(string file)
422 return !(file.rfind(".wav") == string::npos &&
423 file.rfind(".aiff")== string::npos &&
424 file.rfind(".aif") == string::npos &&
425 file.rfind(".snd") == string::npos &&
426 file.rfind(".au") == string::npos &&
427 file.rfind(".raw") == string::npos &&
428 file.rfind(".sf") == string::npos &&
429 file.rfind(".cdr") == string::npos &&
430 file.rfind(".smp") == string::npos &&
431 file.rfind(".maud")== string::npos &&
432 file.rfind(".vwe") == string::npos &&
433 file.rfind(".paf") == string::npos &&
434 #ifdef HAVE_COREAUDIO
435 file.rfind(".mp3") == string::npos &&
436 file.rfind(".aac") == string::npos &&
437 file.rfind(".mp4") == string::npos &&
438 #endif // HAVE_COREAUDIO
439 file.rfind(".voc") == string::npos);
443 AudioLibrary::get_state ()
445 XMLNode* root = new XMLNode(X_("AudioLibrary"));
447 for (vector<string>::iterator i = sfdb_paths.begin(); i != sfdb_paths.end(); ++i) {
448 XMLNode* node = new XMLNode(X_("Path"));
449 node->add_property("value", *i);
450 root->add_child_nocopy(*node);
457 AudioLibrary::set_state (const XMLNode& node)
459 if (node.name() != X_("AudioLibrary")) {
460 fatal << "programming error: AudioLibrary: incorrect XML node sent to set_state()" << endmsg;
464 XMLNodeList nodes = node.children(X_("Path"));
466 vector<string> paths;
469 for (XMLNodeConstIterator iter = nodes.begin(); iter != nodes.end(); ++iter) {
472 if ((prop = child->property(X_("value"))) != 0) {
473 paths.push_back(prop->value());