Added mutagen library.
[wolnelektury.git] / lib / mutagen / optimfrog.py
1 # OptimFROG reader/tagger
2 #
3 # Copyright 2006 Lukas Lalinsky <lalinsky@gmail.com>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation.
8 #
9 # $Id: optimfrog.py 4275 2008-06-01 06:32:37Z piman $
10
11 """OptimFROG audio streams with APEv2 tags.
12
13 OptimFROG is a lossless audio compression program. Its main goal is to
14 reduce at maximum the size of audio files, while permitting bit
15 identical restoration for all input. It is similar with the ZIP
16 compression, but it is highly specialized to compress audio data.
17
18 Only versions 4.5 and higher are supported.
19
20 For more information, see http://www.losslessaudio.org/
21 """
22
23 __all__ = ["OptimFROG", "Open", "delete"]
24
25 import struct
26 from mutagen.apev2 import APEv2File, error, delete
27
28 class OptimFROGHeaderError(error): pass
29
30 class OptimFROGInfo(object):
31     """OptimFROG stream information.
32
33     Attributes:
34     channels - number of audio channels
35     length - file length in seconds, as a float
36     sample_rate - audio sampling rate in Hz
37     """
38
39     def __init__(self, fileobj):
40         header = fileobj.read(76)
41         if (len(header) != 76 or not header.startswith("OFR ") or
42             struct.unpack("<I", header[4:8])[0] not in [12, 15]):
43             raise OptimFROGHeaderError("not an OptimFROG file")
44         (total_samples, total_samples_high, sample_type, self.channels,
45          self.sample_rate) = struct.unpack("<IHBBI", header[8:20])
46         total_samples += total_samples_high << 32
47         self.channels += 1
48         if self.sample_rate:
49             self.length = float(total_samples) / (self.channels * 
50                                                   self.sample_rate)
51         else:
52             self.length = 0.0
53
54     def pprint(self):
55         return "OptimFROG, %.2f seconds, %d Hz" % (self.length,
56                                                    self.sample_rate)
57
58 class OptimFROG(APEv2File):
59     _Info = OptimFROGInfo
60
61     def score(filename, fileobj, header):
62         return (header.startswith("OFR") + filename.endswith(".ofr") +
63                 filename.endswith(".ofs"))
64     score = staticmethod(score)