diff options
author | Marco Ziener <mziener@lavabit.com> | 2010-10-14 10:38:34 +0200 |
---|---|---|
committer | Marco Ziener <mziener@lavabit.com> | 2010-10-14 10:38:34 +0200 |
commit | ebe8ca46d9288899a1c44490e79e7f17eebae545 (patch) | |
tree | 496832a555a2afb968ba260a2f1b1e71598b9311 | |
parent | 9b5db7ab37a4a2527d523ff39bb0c5525553119e (diff) | |
download | erlang-ebe8ca46d9288899a1c44490e79e7f17eebae545.tar.gz erlang-ebe8ca46d9288899a1c44490e79e7f17eebae545.tar.xz erlang-ebe8ca46d9288899a1c44490e79e7f17eebae545.zip |
Updated mediamodule
-rw-r--r-- | media.erl | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/media.erl b/media.erl new file mode 100644 index 0000000..1ff2f23 --- /dev/null +++ b/media.erl @@ -0,0 +1,63 @@ +-module(media). +-export([init/0,insert/3, ask/2, all/0]). + +% Since we are not willing to calculate and deliver all the id3 tags everytime they are requested, +% we try to get something persistent with mnesia. +% Concerning the parsing of id3tags we use the library id3v2 by Brendon Hogger. For detailed information take a +% look at the header of the library. + +% What is an entry in our database made of? By the way the filepath includes the filename. + +-record(track, {artist, title, votes, locked, filepath }). + +% With which application do we play mp3s? + +player(path) -> + {ok, ["/usr/bin/env", "mplayer", "-quiet", path]}. + +% Before this module becomes usable, we must initialize it with the following steps: +% 1. Initialize the mnesiadatabase and create the table within it. +% 2. Parse the mp3s in the working directory and add them to the database +% 3. Get into a loop so the database can be actually queried and files can be played. + +init() -> + mnesia:create_schema([node()]), + mnesia:start(), + mnesia:create_table(track, [{attributes, record_info(fields, track)}]), + io:format("Initialisation of mnesia successful.\n"). + +% Basic insertion of entrys into the database. Some entries are left out because they are 0 or false. + +insert(Artist, Title, Filepath) -> + F = fun() -> + mnesia:write(#track{artist = Artist, title = Title, votes = 0, locked = false, filepath = Filepath}) + end, + mnesia:transaction(F). + +% We want to query in order to simplify the next calls. +ask(Artist, Title) -> + F = fun() -> + mnesia:match_object({track, Artist, Title, '_', '_', '_'}) + end, + {atomic, Results} = mnesia:transaction(F), + Results. + +% Just in case the client is interested in everything we got. + +all() -> + F = fun() -> + mnesia:match({track, '_','_','_','_','_'}) + end, + {atomic, Results} = mnesia:transaction(F), + Results. + +% We want to play mp3s from our database. + +play(Artist, Title, Callback) -> + [Head|_] = ask(Artist, Title), + {_, artist, title, _, _, fp} = Head, + Cmd = "mplayer -quiet " ++ fp, + Port = erlang:open_port({spawn, Cmd}, [exit_status]). + +% We want to execute commands locally + |