annotate cpack/dml/lib/beets_p2r.pl @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
rev   line source
Daniel@0 1 /* Part of DML (Digital Music Laboratory)
Daniel@0 2 Copyright 2014-2015 Samer Abdallah, University of London
Daniel@0 3
Daniel@0 4 This program is free software; you can redistribute it and/or
Daniel@0 5 modify it under the terms of the GNU General Public License
Daniel@0 6 as published by the Free Software Foundation; either version 2
Daniel@0 7 of the License, or (at your option) any later version.
Daniel@0 8
Daniel@0 9 This program is distributed in the hope that it will be useful,
Daniel@0 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
Daniel@0 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Daniel@0 12 GNU General Public License for more details.
Daniel@0 13
Daniel@0 14 You should have received a copy of the GNU General Public
Daniel@0 15 License along with this library; if not, write to the Free Software
Daniel@0 16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Daniel@0 17 */
Daniel@0 18
Daniel@0 19 :- module(beets_p2r, []).
Daniel@0 20
Daniel@0 21 /** <module> Access to beets database
Daniel@0 22 */
Daniel@0 23
Daniel@0 24
Daniel@0 25 :- use_module(library(prosqlite)).
Daniel@0 26 :- use_module(library(musicbrainz)).
Daniel@0 27 :- use_module(library(semweb/rdf_db)).
Daniel@0 28 :- use_module(library(rdfutils)).
Daniel@0 29 :- use_module(entailment(p2r)).
Daniel@0 30
Daniel@0 31 :- set_prolog_flag(double_quotes,string).
Daniel@0 32
Daniel@0 33 :- rdf_register_prefix(beets,'http://dml.org/beets/props/item/').
Daniel@0 34 :- rdf_register_prefix(beets_album,'http://dml.org/beets/props/album/').
Daniel@0 35 :- rdf_register_prefix(audio,'audio:').
Daniel@0 36 :- rdf_register_prefix(dml,'http://dml.org/dml/').
Daniel@0 37 :- rdf_register_prefix(mb,'http://musicbrainz.org/').
Daniel@0 38
Daniel@0 39 :- setting(db_file,string,"~/lib/beets/music-ro.db","Location of beets database").
Daniel@0 40 :- setting(audio_root,string,"/usr/local/share/Music/","Root of audio file directory tree").
Daniel@0 41
Daniel@0 42
Daniel@0 43 rdf(audio:tail(Rel), rdf:type, mo:'AudioFile'),
Daniel@0 44 rdf(audio:tail(Rel), mo:encoding, literal(Enc)) <==
Daniel@0 45 setting(audio_root,Root),
Daniel@0 46 expand_file_name(Root,[Root1]),
Daniel@0 47 item(Id,path,Path),
Daniel@0 48 item(Id,format,Enc),
Daniel@0 49 atom_concat(Root1,Rel,Path).
Daniel@0 50
Daniel@0 51 rdf(audio:tail(Rel), beets:enc(Prop), literal(Val)) <==
Daniel@0 52 audio_item_path(Id,Rel),
Daniel@0 53 item(Id,Prop,Val1),
Daniel@0 54 ( Prop=path -> exists_file(Val1); true),
Daniel@0 55 as_typed_literal(Val1,Val).
Daniel@0 56
Daniel@0 57 rdf(Signal,rdf:type,mo:'Signal') <==
Daniel@0 58 item(_,mb_trackid,ID), % track id is actually a Recording.
Daniel@0 59 mb_id_uri(recording,ID,Signal).
Daniel@0 60
Daniel@0 61 rdf(Signal,mo:available_as,audio:tail(Rel)) <==
Daniel@0 62 setting(audio_root,Root),
Daniel@0 63 expand_file_name(Root,[Root1]),
Daniel@0 64 item(Id,path,Path),
Daniel@0 65 exists_file(Path),
Daniel@0 66 atom_concat(Root1,Rel,Path),
Daniel@0 67 item(Id,mb_trackid,ID), % track id is actually a Recording.
Daniel@0 68 % need to ask Linkedbrainz to get Signal URI
Daniel@0 69 mb_id_uri(recording,ID,Signal).
Daniel@0 70
Daniel@0 71 rdf(audio:tail(Rel), dml:file_artist, URI) <==
Daniel@0 72 audio_item_path(Id,Rel),
Daniel@0 73 item(Id,mb_artistid,ID),
Daniel@0 74 mb_id_uri(artist,ID,URI).
Daniel@0 75
Daniel@0 76 rdf(audio:tail(Rel), dml:file_release, URI) <==
Daniel@0 77 audio_item_path(Id,Rel),
Daniel@0 78 item(Id,mb_albumid,ID),
Daniel@0 79 mb_id_uri(release,ID,URI).
Daniel@0 80
Daniel@0 81 rdf(audio:tail(Rel), dml:file_recording, URI) <==
Daniel@0 82 audio_item_path(Id,Rel),
Daniel@0 83 item(Id,mb_trackid,ID),
Daniel@0 84 mb_id_uri(recording,ID,URI).
Daniel@0 85
Daniel@0 86 % rdf(URI,mo:release_type,mo:
Daniel@0 87 rdf(URI,rdf:type,mo:'Release') <==
Daniel@0 88 item(_,mb_albumid,ID),
Daniel@0 89 mb_id_uri(release,ID,URI).
Daniel@0 90
Daniel@0 91 audio_item_path(Id,Rel) :-
Daniel@0 92 setting(audio_root,Root),
Daniel@0 93 expand_file_name(Root,[Root1]),
Daniel@0 94 item(Id,path,Path),
Daniel@0 95 atom_concat(Root1,Rel,Path).
Daniel@0 96
Daniel@0 97 :- public import/0.
Daniel@0 98 import :- with_beets_db(assert_all(beets_p2r)).
Daniel@0 99
Daniel@0 100 with_beets_db(Goal) :-
Daniel@0 101 setting(db_file,DBFile),
Daniel@0 102 expand_file_name(DBFile,[Path|_]),
Daniel@0 103 setup_call_cleanup(
Daniel@0 104 sqlite_connect(Path,Con,[alias(beets),ext(''),as_predicates(true),arity(unary),at_module(beets)]),
Daniel@0 105 Goal,
Daniel@0 106 sqlite_disconnect(Con)).
Daniel@0 107
Daniel@0 108 :- dynamic beets:items/1.
Daniel@0 109 item(Id) :- beets:items([id=Id]).
Daniel@0 110
Daniel@0 111 item(Id,path,Path) :-
Daniel@0 112 ( var(Path) -> beets:items([id=Id,path=Path])
Daniel@0 113 ; var(Id) -> sqlite_format_query(beets,"select id from items where path like '~s'"-[Path],row(Id))
Daniel@0 114 ; sqlite_format_query(beets,"select null from items where id=~w and path like '~s'"-[Id,Path],row(_))
Daniel@0 115 ).
Daniel@0 116
Daniel@0 117 item(Id,Prop,Val) :-
Daniel@0 118 table_column(items,Prop), Prop\=path, Prop\=comp,
Daniel@0 119 beets:items([id=Id,Prop=Val]),
Daniel@0 120 \+invalid(Prop,Val).
Daniel@0 121
Daniel@0 122
Daniel@0 123 % album(Id) :- beets:albums([id=Id]).
Daniel@0 124 % album(Id,Prop,Val) :-
Daniel@0 125 % table_column(albums,Prop),
Daniel@0 126 % beets:albums([id=Id,Prop=Val]), Val\=''.
Daniel@0 127
Daniel@0 128 table_column(T,C) :-
Daniel@0 129 sqlite_table_column(beets,T,C1), C=C1.
Daniel@0 130
Daniel@0 131 invalid(_,'').
Daniel@0 132 invalid(Prop,0) :- nonzero(Prop).
Daniel@0 133 invalid(mtime,0.0).
Daniel@0 134
Daniel@0 135 nonzero(bitdepth).
Daniel@0 136 nonzero(bitrate).
Daniel@0 137 nonzero(samplerate).
Daniel@0 138 nonzero(track).
Daniel@0 139 nonzero(tracktotal).
Daniel@0 140 nonzero(disc).
Daniel@0 141 nonzero(disctotal).
Daniel@0 142 nonzero(bpm).
Daniel@0 143 nonzero(day).
Daniel@0 144 nonzero(month).
Daniel@0 145 nonzero(year).
Daniel@0 146 nonzero(original_day).
Daniel@0 147 nonzero(original_month).
Daniel@0 148 nonzero(original_year).
Daniel@0 149
Daniel@0 150 :- public audio_file/3.
Daniel@0 151
Daniel@0 152 audio_file(URI,File,just(T0)) :-
Daniel@0 153 rdf(URI,beets:path,literal(File)), !,
Daniel@0 154 rdf(URI,beets:format,literal(F0)),
Daniel@0 155 format_type(F0,T0).
Daniel@0 156
Daniel@0 157 format_type('MP3',mp3).
Daniel@0 158 format_type('OGG',ogg).
Daniel@0 159 format_type('AAC',aac).
Daniel@0 160 format_type('ALAC',mp4). % !!! ??