Przeniesienie rozmaitych skryptów z katalogu głównego do scripts.
[wolnelektury.git] / lib / mutagen / trueaudio.py
1 # True Audio support for Mutagen
2 # Copyright 2006 Joe Wreschnig
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of version 2 of the GNU General Public License as
6 # published by the Free Software Foundation.
7
8 """True Audio audio stream information and tags.
9
10 True Audio is a lossless format designed for real-time encoding and
11 decoding. This module is based on the documentation at
12 http://www.true-audio.com/TTA_Lossless_Audio_Codec_-_Format_Description
13
14 True Audio files use ID3 tags.
15 """
16
17 __all__ = ["TrueAudio", "Open", "delete"]
18
19 from mutagen.id3 import ID3FileType, delete
20 from mutagen._util import cdata
21
22 class error(RuntimeError): pass
23 class TrueAudioHeaderError(error, IOError): pass
24
25 class TrueAudioInfo(object):
26     """True Audio stream information.
27
28     Attributes:
29     length - audio length, in seconds
30     sample_rate - audio sample rate, in Hz
31     """
32
33     def __init__(self, fileobj, offset):
34         fileobj.seek(offset or 0)
35         header = fileobj.read(18)
36         if len(header) != 18 or not header.startswith("TTA"):
37             raise TrueAudioHeaderError("TTA header not found")
38         self.sample_rate = cdata.int_le(header[10:14])
39         samples = cdata.uint_le(header[14:18])
40         self.length = float(samples) / self.sample_rate
41
42     def pprint(self):
43         return "True Audio, %.2f seconds, %d Hz." % (
44             self.length, self.sample_rate)
45
46 class TrueAudio(ID3FileType):
47     """A True Audio file."""
48
49     _Info = TrueAudioInfo
50     _mimes = ["audio/x-tta"]
51
52     def score(filename, fileobj, header):
53         return (header.startswith("ID3") + header.startswith("TTA") +
54                 filename.lower().endswith(".tta") * 2)
55     score = staticmethod(score)
56
57 Open = TrueAudio