Przeniesienie rozmaitych skryptów z katalogu głównego do scripts.
[wolnelektury.git] / lib / mutagen / oggspeex.py
1 # Ogg Speex support.
2 #
3 # Copyright 2006 Joe Wreschnig <piman@sacredchao.net>
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: oggspeex.py 4275 2008-06-01 06:32:37Z piman $
10
11 """Read and write Ogg Speex comments.
12
13 This module handles Speex files wrapped in an Ogg bitstream. The
14 first Speex stream found is used.
15
16 Read more about Ogg Speex at http://www.speex.org/. This module is
17 based on the specification at http://www.speex.org/manual2/node7.html
18 and clarifications after personal communication with Jean-Marc,
19 http://lists.xiph.org/pipermail/speex-dev/2006-July/004676.html.
20 """
21
22 __all__ = ["OggSpeex", "Open", "delete"]
23
24 from mutagen._vorbis import VCommentDict
25 from mutagen.ogg import OggPage, OggFileType, error as OggError
26 from mutagen._util import cdata
27
28 class error(OggError): pass
29 class OggSpeexHeaderError(error): pass
30
31 class OggSpeexInfo(object):
32     """Ogg Speex stream information.
33
34     Attributes:
35     bitrate - nominal bitrate in bits per second
36     channels - number of channels
37     length - file length in seconds, as a float
38
39     The reference encoder does not set the bitrate; in this case,
40     the bitrate will be 0.
41     """
42
43     length = 0
44
45     def __init__(self, fileobj):
46         page = OggPage(fileobj)
47         while not page.packets[0].startswith("Speex   "):
48             page = OggPage(fileobj)
49         if not page.first:
50             raise OggSpeexHeaderError(
51                 "page has ID header, but doesn't start a stream")
52         self.sample_rate = cdata.uint_le(page.packets[0][36:40])
53         self.channels = cdata.uint_le(page.packets[0][48:52])
54         self.bitrate = cdata.int_le(page.packets[0][52:56])
55         if self.bitrate == -1:
56             self.bitrate = 0
57         self.serial = page.serial
58
59     def pprint(self):
60         return "Ogg Speex, %.2f seconds" % self.length
61
62 class OggSpeexVComment(VCommentDict):
63     """Speex comments embedded in an Ogg bitstream."""
64
65     def __init__(self, fileobj, info):
66         pages = []
67         complete = False
68         while not complete:
69             page = OggPage(fileobj)
70             if page.serial == info.serial:
71                 pages.append(page)
72                 complete = page.complete or (len(page.packets) > 1)
73         data = OggPage.to_packets(pages)[0] + "\x01"
74         super(OggSpeexVComment, self).__init__(data, framing=False)
75
76     def _inject(self, fileobj):
77         """Write tag data into the Speex comment packet/page."""
78
79         fileobj.seek(0)
80
81         # Find the first header page, with the stream info.
82         # Use it to get the serial number.
83         page = OggPage(fileobj)
84         while not page.packets[0].startswith("Speex   "):
85             page = OggPage(fileobj)
86
87         # Look for the next page with that serial number, it'll start
88         # the comment packet.
89         serial = page.serial
90         page = OggPage(fileobj)
91         while page.serial != serial:
92             page = OggPage(fileobj)
93
94         # Then find all the pages with the comment packet.
95         old_pages = [page]
96         while not (old_pages[-1].complete or len(old_pages[-1].packets) > 1):
97             page = OggPage(fileobj)
98             if page.serial == old_pages[0].serial:
99                 old_pages.append(page)
100
101         packets = OggPage.to_packets(old_pages, strict=False)
102
103         # Set the new comment packet.
104         packets[0] = self.write(framing=False)
105
106         new_pages = OggPage.from_packets(packets, old_pages[0].sequence)
107         OggPage.replace(fileobj, old_pages, new_pages)
108
109 class OggSpeex(OggFileType):
110     """An Ogg Speex file."""
111
112     _Info = OggSpeexInfo
113     _Tags = OggSpeexVComment
114     _Error = OggSpeexHeaderError
115     _mimes = ["audio/x-speex"]
116
117     def score(filename, fileobj, header):
118         return (header.startswith("OggS") * ("Speex   " in header))
119     score = staticmethod(score)
120
121 Open = OggSpeex
122
123 def delete(filename):
124     """Remove tags from a file."""
125     OggSpeex(filename).delete()