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 ()
87 add_instant_xml(get_state(), get_user_ardour_path());
91 AudioLibrary::initialize_db ()
93 // define ardour:Soundfile
94 lrdf_add_triple(src.c_str(), SOUNDFILE, RDF_TYPE, RDFS_CLASS, lrdf_uri);
96 // add intergral fields
97 add_field(_("channels"));
98 add_field(_("samplerate"));
99 add_field(_("resolution"));
100 add_field(_("format"));
104 AudioLibrary::save_changes ()
106 if (lrdf_export_by_source(src.c_str(), src.substr(5).c_str())) {
107 warning << string_compose(_("Could not open %1. Audio Library not saved"), src) << endmsg;
112 AudioLibrary::add_member (string member)
114 string file_uri(string_compose("file:%1", member));
116 lrdf_add_triple(src.c_str(), file_uri.c_str(), RDF_TYPE,
117 SOUNDFILE, lrdf_uri);
121 AudioLibrary::remove_member (string uri)
123 lrdf_remove_uri_matches (uri.c_str());
127 AudioLibrary::search_members_and (vector<string>& members,
128 const map<string,string>& fields)
130 lrdf_statement **head;
131 lrdf_statement* pattern = 0;
132 lrdf_statement* old = 0;
135 map<string,string>::const_iterator i;
136 for (i = fields.begin(); i != fields.end(); ++i){
137 pattern = new lrdf_statement;
138 pattern->subject = "?";
139 pattern->predicate = strdup(field_uri(i->first).c_str());
140 pattern->object = strdup((i->second).c_str());
147 lrdf_uris* ulist = lrdf_match_multi(*head);
148 for (uint32_t j = 0; ulist && j < ulist->count; ++j) {
149 // printf("AND: %s\n", ulist->items[j]);
150 members.push_back(ulist->items[j]);
152 lrdf_free_uris(ulist);
154 compact_vector(members);
160 free(pattern->predicate);
161 free(pattern->object);
163 pattern = pattern->next;
169 AudioLibrary::search_members_or (vector<string>& members,
170 const map<string,string>& fields)
172 map<string,string>::const_iterator i;
174 lrdf_statement pattern;
175 for (i = fields.begin(); i != fields.end(); ++i) {
177 pattern.predicate = strdup(field_uri(i->first).c_str());
178 pattern.object = strdup((i->second).c_str());
179 pattern.object_type = lrdf_literal;
181 lrdf_statement* matched = lrdf_matches(&pattern);
183 lrdf_statement* old = matched;
185 // printf ("OR: %s\n", matched->subject);
186 members.push_back(matched->subject);
187 matched = matched->next;
190 free(pattern.predicate);
191 free(pattern.object);
192 lrdf_free_statements (old);
195 compact_vector(members);
199 AudioLibrary::add_field (string name)
201 string local_field = field_uri(name);
202 lrdf_statement pattern;
203 pattern.subject = strdup(local_field.c_str());
204 pattern.predicate = RDF_TYPE;
205 pattern.object = RDF_BASE "Property";
206 pattern.object_type = lrdf_uri;
208 if(lrdf_exists_match(&pattern)) {
212 // of type rdf:Property
213 lrdf_add_triple(src.c_str(), local_field.c_str(), RDF_TYPE,
214 RDF_BASE "Property", lrdf_uri);
215 // of range ardour:Soundfile
216 lrdf_add_triple(src.c_str(), local_field.c_str(), RDFS_BASE "range",
217 SOUNDFILE, lrdf_uri);
218 // of domain rdf:Literal
219 lrdf_add_triple(src.c_str(), local_field.c_str(), RDFS_BASE "domain",
220 RDF_BASE "Literal", lrdf_uri);
222 set_label (local_field, name);
224 fields_changed(); /* EMIT SIGNAL */
228 AudioLibrary::get_fields (vector<string>& fields)
230 lrdf_statement pattern;
233 pattern.predicate = RDFS_BASE "range";
234 pattern.object = SOUNDFILE;
235 pattern.object_type = lrdf_uri;
237 lrdf_statement* matches = lrdf_matches(&pattern);
239 lrdf_statement* current = matches;
240 while (current != 0) {
241 fields.push_back(get_label(current->subject));
243 current = current->next;
246 lrdf_free_statements(matches);
248 compact_vector(fields);
252 AudioLibrary::remove_field (string name)
254 lrdf_remove_uri_matches(field_uri(name).c_str());
255 fields_changed (); /* EMIT SIGNAL */
259 AudioLibrary::get_field (string uri, string field)
261 lrdf_statement pattern;
263 pattern.subject = strdup(uri.c_str());
265 pattern.predicate = strdup(field_uri(field).c_str());
268 pattern.object_type = lrdf_literal;
270 lrdf_statement* matches = lrdf_matches(&pattern);
271 free(pattern.subject);
272 free(pattern.predicate);
276 object << matches->object;
279 lrdf_free_statements(matches);
284 AudioLibrary::set_field (string uri, string field, string literal)
286 lrdf_statement pattern;
288 pattern.subject = strdup(uri.c_str());
290 string local_field = field_uri(field);
291 pattern.predicate = strdup(local_field.c_str());
294 pattern.object_type = lrdf_literal;
296 lrdf_remove_matches(&pattern);
297 free(pattern.subject);
298 free(pattern.predicate);
300 lrdf_add_triple(src.c_str(), uri.c_str(), local_field.c_str(),
301 literal.c_str(), lrdf_literal);
303 fields_changed(); /* EMIT SIGNAL */
307 AudioLibrary::field_uri (string name)
309 stringstream local_field;
310 local_field << "file:sfdb/fields/" << name;
312 return local_field.str();
316 AudioLibrary::get_label (string uri)
318 lrdf_statement pattern;
319 pattern.subject = strdup(uri.c_str());
320 pattern.predicate = RDFS_BASE "label";
322 pattern.object_type = lrdf_literal;
324 lrdf_statement* matches = lrdf_matches (&pattern);
325 free(pattern.subject);
329 label << matches->object;
332 lrdf_free_statements(matches);
338 AudioLibrary::set_label (string uri, string label)
340 lrdf_statement pattern;
341 pattern.subject = strdup(uri.c_str());
342 pattern.predicate = RDFS_BASE "label";
344 pattern.object_type = lrdf_literal;
346 lrdf_remove_matches(&pattern);
347 free(pattern.subject);
349 lrdf_add_triple(src.c_str(), uri.c_str(), RDFS_BASE "label",
350 label.c_str(), lrdf_literal);
354 AudioLibrary::compact_vector(vector<string>& vec)
356 sort(vec.begin(), vec.end());
357 unique(vec.begin(), vec.end());
361 AudioLibrary::set_paths (vector<string> paths)
367 AudioLibrary::get_paths ()
373 AudioLibrary::scan_paths ()
375 if (sfdb_paths.size() < 1) {
379 vector<char *> pathv(sfdb_paths.size());
381 for (i = 0; i < sfdb_paths.size(); ++i) {
382 pathv[i] = new char[sfdb_paths[i].length() +1];
383 sfdb_paths[i].copy(pathv[i], string::npos);
384 pathv[i][sfdb_paths[i].length()] = 0;
388 FTS* ft = fts_open(&pathv[0], FTS_LOGICAL|FTS_NOSTAT|FTS_PHYSICAL|FTS_XDEV, 0);
390 error << strerror(errno) << endmsg;
395 s.predicate = RDF_TYPE;
396 s.object = SOUNDFILE;
397 s.object_type = lrdf_uri;
399 while (FTSENT* file = fts_read(ft)) {
400 if ((file->fts_info & FTS_F) && (safe_file_extension(file->fts_name))) {
402 filename.append(file->fts_accpath);
403 s.subject = strdup(filename.c_str());
404 if (lrdf_exists_match(&s)) {
407 add_member(file->fts_accpath);
408 cout << file->fts_accpath << endl;
415 for (i = 0; i < pathv.size(); ++i) {
423 AudioLibrary::safe_file_extension(string file)
425 return !(file.rfind(".wav") == string::npos &&
426 file.rfind(".aiff")== string::npos &&
427 file.rfind(".aif") == string::npos &&
428 file.rfind(".snd") == string::npos &&
429 file.rfind(".au") == string::npos &&
430 file.rfind(".raw") == string::npos &&
431 file.rfind(".sf") == string::npos &&
432 file.rfind(".cdr") == string::npos &&
433 file.rfind(".smp") == string::npos &&
434 file.rfind(".maud")== string::npos &&
435 file.rfind(".vwe") == string::npos &&
436 file.rfind(".paf") == string::npos &&
437 #ifdef HAVE_COREAUDIO
438 file.rfind(".mp3") == string::npos &&
439 file.rfind(".aac") == string::npos &&
440 file.rfind(".mp4") == string::npos &&
441 #endif // HAVE_COREAUDIO
442 file.rfind(".voc") == string::npos);
446 AudioLibrary::get_state ()
448 XMLNode* root = new XMLNode(X_("AudioLibrary"));
450 for (vector<string>::iterator i = sfdb_paths.begin(); i != sfdb_paths.end(); ++i) {
451 XMLNode* node = new XMLNode(X_("Path"));
452 node->add_property("value", *i);
453 root->add_child_nocopy(*node);
460 AudioLibrary::set_state (const XMLNode& node)
462 if (node.name() != X_("AudioLibrary")) {
463 fatal << "programming error: AudioLibrary: incorrect XML node sent to set_state()" << endmsg;
467 XMLNodeList nodes = node.children(X_("Path"));
469 vector<string> paths;
472 for (XMLNodeConstIterator iter = nodes.begin(); iter != nodes.end(); ++iter) {
475 if ((prop = child->property(X_("value"))) != 0) {
476 paths.push_back(prop->value());