1 # True Audio support for Mutagen
2 # Copyright 2006 Joe Wreschnig
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.
8 """True Audio audio stream information and tags.
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
14 True Audio files use ID3 tags.
17 __all__ = ["TrueAudio", "Open", "delete"]
19 from mutagen.id3 import ID3FileType, delete
20 from mutagen._util import cdata
22 class error(RuntimeError): pass
23 class TrueAudioHeaderError(error, IOError): pass
25 class TrueAudioInfo(object):
26 """True Audio stream information.
29 length - audio length, in seconds
30 sample_rate - audio sample rate, in Hz
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
43 return "True Audio, %.2f seconds, %d Hz." % (
44 self.length, self.sample_rate)
46 class TrueAudio(ID3FileType):
47 """A True Audio file."""
50 _mimes = ["audio/x-tta"]
52 def score(filename, fileobj, header):
53 return (header.startswith("ID3") + header.startswith("TTA") +
54 filename.lower().endswith(".tta") * 2)
55 score = staticmethod(score)