Debian Codegraph @

xl/metadata/mod.py

raw
1 # Copyright (C) 2008-2010 Adam Olsen
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2, or (at your option)
6 # any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 #
18 # The developers of the Exaile media player hereby grant permission
19 # for non-GPL compatible GStreamer and Exaile plugins to be used and
20 # distributed together with GStreamer and Exaile. This permission is
21 # above and beyond the permissions granted by the GPL license by which
22 # Exaile is covered. If you modify this code, you may extend this
23 # exception to your version of the code, but you are not obligated to
24 # do so. If you do not wish to do so, delete this exception statement
25 # from your version.
26
27
28 from xl.metadata._base import BaseFormat
29
30 import logging
31 import os
32
33
34 logger = logging.getLogger(__name__)
35
36
37 try:
38 import ctypes
39
40 modplug = ctypes.cdll.LoadLibrary("libmodplug.so.1")
41 modplug.ModPlug_Load.restype = ctypes.c_void_p
42 modplug.ModPlug_Load.argtypes = (ctypes.c_void_p, ctypes.c_int)
43 modplug.ModPlug_GetName.restype = ctypes.c_char_p
44 modplug.ModPlug_GetName.argtypes = (ctypes.c_void_p,)
45 modplug.ModPlug_GetLength.restype = ctypes.c_int
46 modplug.ModPlug_GetLength.argtypes = (ctypes.c_void_p,)
47 except (ImportError, OSError):
48 logger.debug('No support for Mod metadata because libmodplug could not be found.')
49 modplug = None
50
51
52 class ModFormat(BaseFormat):
53 writable = False
54
55 def load(self):
56 if modplug:
57 data = open(self.loc, "rb").read()
58 f = modplug.ModPlug_Load(data, len(data))
59 if f:
60 name = modplug.ModPlug_GetName(f) or os.path.split(self.loc)[-1]
61 length = modplug.ModPlug_GetLength(f) / 1000.0 or -1
62 self.mutagen = {'title': name, '__length': length}
63 else:
64 self.mutagen = {}
65
66 def get_length(self):
67 try:
68 return self.mutagen['__length']
69 except KeyError:
70 return -1
71
72 def get_bitrate(self):
73 return -1
74
75
76 # vim: et sts=4 sw=4