1 # A WavPack reader/tagger
3 # Copyright 2006 Joe Wreschnig <piman@sacredchao.net>
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.
9 # $Id: wavpack.py 4275 2008-06-01 06:32:37Z piman $
11 """WavPack reading and writing.
13 WavPack is a lossless format that uses APEv2 tags. Read
14 http://www.wavpack.com/ for more information.
17 __all__ = ["WavPack", "Open", "delete"]
19 from mutagen.apev2 import APEv2File, error, delete
20 from mutagen._util import cdata
22 class WavPackHeaderError(error): pass
24 RATES = [6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000, 32000, 44100,
25 48000, 64000, 88200, 96000, 192000]
27 class WavPackInfo(object):
28 """WavPack stream information.
31 channels - number of audio channels (1 or 2)
32 length - file length in seconds, as a float
33 sample_rate - audio sampling rate in Hz
34 version - WavPack stream version
37 def __init__(self, fileobj):
38 header = fileobj.read(28)
39 if len(header) != 28 or not header.startswith("wvpk"):
40 raise WavPackHeaderError("not a WavPack file")
41 samples = cdata.uint_le(header[12:16])
42 flags = cdata.uint_le(header[24:28])
43 self.version = cdata.short_le(header[8:10])
44 self.channels = bool(flags & 4) or 2
45 self.sample_rate = RATES[(flags >> 23) & 0xF]
46 self.length = float(samples) / self.sample_rate
49 return "WavPack, %.2f seconds, %d Hz" % (self.length, self.sample_rate)
51 class WavPack(APEv2File):
53 _mimes = ["audio/x-wavpack"]
55 def score(filename, fileobj, header):
56 return header.startswith("wvpk") * 2
57 score = staticmethod(score)