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 AudioLibrary::AudioLibrary ()
51 // sfdb_paths.push_back("/Users/taybin/sounds");
53 src = "file:" + get_user_ardour_path() + "sfdb";
55 // workaround for possible bug in raptor that crashes when saving to a
57 touch_file(get_user_ardour_path() + "sfdb");
59 lrdf_read_file(src.c_str());
61 lrdf_statement pattern;
63 pattern.subject = SOUNDFILE;
64 pattern.predicate = RDF_TYPE;
65 pattern.object = RDFS_CLASS;
66 pattern.object_type = lrdf_uri;
68 lrdf_statement* matches = lrdf_matches(&pattern);
70 // if empty DB, create basic schema
76 lrdf_free_statements(matches);
78 XMLNode* state = instant_xml(X_("AudioLibrary"), get_user_ardour_path());
85 AudioLibrary::~AudioLibrary ()
90 AudioLibrary::initialize_db ()
92 // define ardour:Soundfile
93 lrdf_add_triple(src.c_str(), SOUNDFILE, RDF_TYPE, RDFS_CLASS, lrdf_uri);
95 // add intergral fields
96 add_field(_("channels"));
97 add_field(_("samplerate"));
98 add_field(_("resolution"));
99 add_field(_("format"));
103 AudioLibrary::save_changes ()
105 if (lrdf_export_by_source(src.c_str(), src.substr(5).c_str())) {
106 warning << string_compose(_("Could not open %1. Audio Library not saved"), src) << endmsg;
111 AudioLibrary::add_member (string member)
113 string file_uri(string_compose("file:%1", member));
115 lrdf_add_triple(src.c_str(), file_uri.c_str(), RDF_TYPE,
116 SOUNDFILE, lrdf_uri);
120 AudioLibrary::remove_member (string uri)
122 lrdf_remove_uri_matches (uri.c_str());
126 AudioLibrary::search_members_and (vector<string>& members,
127 const map<string,string>& fields)
129 lrdf_statement **head;
130 lrdf_statement* pattern = 0;
131 lrdf_statement* old = 0;
134 map<string,string>::const_iterator i;
135 for (i = fields.begin(); i != fields.end(); ++i){
136 pattern = new lrdf_statement;
137 pattern->subject = "?";
138 pattern->predicate = strdup(field_uri(i->first).c_str());
139 pattern->object = strdup((i->second).c_str());
146 lrdf_uris* ulist = lrdf_match_multi(*head);
147 for (uint32_t j = 0; ulist && j < ulist->count; ++j) {
148 // printf("AND: %s\n", ulist->items[j]);
149 members.push_back(ulist->items[j]);
151 lrdf_free_uris(ulist);
153 compact_vector(members);
159 free(pattern->predicate);
160 free(pattern->object);
162 pattern = pattern->next;
168 AudioLibrary::search_members_or (vector<string>& members,
169 const map<string,string>& fields)
171 map<string,string>::const_iterator i;
173 lrdf_statement pattern;
174 for (i = fields.begin(); i != fields.end(); ++i) {
176 pattern.predicate = strdup(field_uri(i->first).c_str());
177 pattern.object = strdup((i->second).c_str());
178 pattern.object_type = lrdf_literal;
180 lrdf_statement* matched = lrdf_matches(&pattern);
182 lrdf_statement* old = matched;
184 // printf ("OR: %s\n", matched->subject);
185 members.push_back(matched->subject);
186 matched = matched->next;
189 free(pattern.predicate);
190 free(pattern.object);
191 lrdf_free_statements (old);
194 compact_vector(members);
198 AudioLibrary::add_field (string name)
200 string local_field = field_uri(name);
201 lrdf_statement pattern;
202 pattern.subject = strdup(local_field.c_str());
203 pattern.predicate = RDF_TYPE;
204 pattern.object = RDF_BASE "Property";
205 pattern.object_type = lrdf_uri;
207 if(lrdf_exists_match(&pattern)) {
211 // of type rdf:Property
212 lrdf_add_triple(src.c_str(), local_field.c_str(), RDF_TYPE,
213 RDF_BASE "Property", lrdf_uri);
214 // of range ardour:Soundfile
215 lrdf_add_triple(src.c_str(), local_field.c_str(), RDFS_BASE "range",
216 SOUNDFILE, lrdf_uri);
217 // of domain rdf:Literal
218 lrdf_add_triple(src.c_str(), local_field.c_str(), RDFS_BASE "domain",
219 RDF_BASE "Literal", lrdf_uri);
221 set_label (local_field, name);
223 fields_changed(); /* EMIT SIGNAL */
227 AudioLibrary::get_fields (vector<string>& fields)
229 lrdf_statement pattern;
232 pattern.predicate = RDFS_BASE "range";
233 pattern.object = SOUNDFILE;
234 pattern.object_type = lrdf_uri;
236 lrdf_statement* matches = lrdf_matches(&pattern);
238 lrdf_statement* current = matches;
239 while (current != 0) {
240 fields.push_back(get_label(current->subject));
242 current = current->next;
245 lrdf_free_statements(matches);
247 compact_vector(fields);
251 AudioLibrary::remove_field (string name)
253 lrdf_remove_uri_matches(field_uri(name).c_str());
254 fields_changed (); /* EMIT SIGNAL */
258 AudioLibrary::get_field (string uri, string field)
260 lrdf_statement pattern;
262 pattern.subject = strdup(uri.c_str());
264 pattern.predicate = strdup(field_uri(field).c_str());
267 pattern.object_type = lrdf_literal;
269 lrdf_statement* matches = lrdf_matches(&pattern);
270 free(pattern.subject);
271 free(pattern.predicate);
275 object << matches->object;
278 lrdf_free_statements(matches);
283 AudioLibrary::set_field (string uri, string field, string literal)
285 lrdf_statement pattern;
287 pattern.subject = strdup(uri.c_str());
289 string local_field = field_uri(field);
290 pattern.predicate = strdup(local_field.c_str());
293 pattern.object_type = lrdf_literal;
295 lrdf_remove_matches(&pattern);
296 free(pattern.subject);
297 free(pattern.predicate);
299 lrdf_add_triple(src.c_str(), uri.c_str(), local_field.c_str(),
300 literal.c_str(), lrdf_literal);
302 fields_changed(); /* EMIT SIGNAL */
306 AudioLibrary::field_uri (string name)
308 stringstream local_field;
309 local_field << "file:sfdb/fields/" << name;
311 return local_field.str();
315 AudioLibrary::get_label (string uri)
317 lrdf_statement pattern;
318 pattern.subject = strdup(uri.c_str());
319 pattern.predicate = RDFS_BASE "label";
321 pattern.object_type = lrdf_literal;
323 lrdf_statement* matches = lrdf_matches (&pattern);
324 free(pattern.subject);
328 label << matches->object;
331 lrdf_free_statements(matches);
337 AudioLibrary::set_label (string uri, string label)
339 lrdf_statement pattern;
340 pattern.subject = strdup(uri.c_str());
341 pattern.predicate = RDFS_BASE "label";
343 pattern.object_type = lrdf_literal;
345 lrdf_remove_matches(&pattern);
346 free(pattern.subject);
348 lrdf_add_triple(src.c_str(), uri.c_str(), RDFS_BASE "label",
349 label.c_str(), lrdf_literal);
353 AudioLibrary::compact_vector(vector<string>& vec)
355 sort(vec.begin(), vec.end());
356 unique(vec.begin(), vec.end());
360 AudioLibrary::set_paths (vector<string> paths)
364 add_instant_xml(get_state(), get_user_ardour_path());
368 AudioLibrary::get_paths ()
374 AudioLibrary::scan_paths ()
376 if (sfdb_paths.size() < 1) {
380 vector<char *> pathv(sfdb_paths.size());
382 for (i = 0; i < sfdb_paths.size(); ++i) {
383 pathv[i] = new char[sfdb_paths[i].length() +1];
384 sfdb_paths[i].copy(pathv[i], string::npos);
385 pathv[i][sfdb_paths[i].length()] = 0;
389 FTS* ft = fts_open(&pathv[0], FTS_LOGICAL|FTS_NOSTAT|FTS_PHYSICAL|FTS_XDEV, 0);
391 error << strerror(errno) << endmsg;
396 s.predicate = RDF_TYPE;
397 s.object = SOUNDFILE;
398 s.object_type = lrdf_uri;
400 while (FTSENT* file = fts_read(ft)) {
401 if ((file->fts_info & FTS_F) && (safe_file_extension(file->fts_name))) {
403 filename.append(file->fts_accpath);
404 s.subject = strdup(filename.c_str());
405 if (lrdf_exists_match(&s)) {
408 add_member(file->fts_accpath);
409 cout << file->fts_accpath << endl;
416 for (i = 0; i < pathv.size(); ++i) {
424 AudioLibrary::safe_file_extension(string file)
426 return !(file.rfind(".wav") == string::npos &&
427 file.rfind(".aiff")== string::npos &&
428 file.rfind(".aif") == string::npos &&
429 file.rfind(".snd") == string::npos &&
430 file.rfind(".au") == string::npos &&
431 file.rfind(".raw") == string::npos &&
432 file.rfind(".sf") == string::npos &&
433 file.rfind(".cdr") == string::npos &&
434 file.rfind(".smp") == string::npos &&
435 file.rfind(".maud")== string::npos &&
436 file.rfind(".vwe") == string::npos &&
437 file.rfind(".paf") == string::npos &&
438 #ifdef HAVE_COREAUDIO
439 file.rfind(".mp3") == string::npos &&
440 file.rfind(".aac") == string::npos &&
441 file.rfind(".mp4") == string::npos &&
442 #endif // HAVE_COREAUDIO
443 file.rfind(".voc") == string::npos);
447 AudioLibrary::get_state ()
449 XMLNode* root = new XMLNode(X_("AudioLibrary"));
451 for (vector<string>::iterator i = sfdb_paths.begin(); i != sfdb_paths.end(); ++i) {
452 XMLNode* node = new XMLNode(X_("Path"));
453 node->add_property("value", *i);
454 root->add_child_nocopy(*node);
461 AudioLibrary::set_state (const XMLNode& node)
463 if (node.name() != X_("AudioLibrary")) {
464 fatal << "programming error: AudioLibrary: incorrect XML node sent to set_state()" << endmsg;
468 XMLNodeList nodes = node.children(X_("Path"));
470 vector<string> paths;
473 for (XMLNodeConstIterator iter = nodes.begin(); iter != nodes.end(); ++iter) {
476 if ((prop = child->property(X_("value"))) != 0) {
477 paths.push_back(prop->value());