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: oggspeex.py 4275 2008-06-01 06:32:37Z piman $
11 """Read and write Ogg Speex comments.
13 This module handles Speex files wrapped in an Ogg bitstream. The
14 first Speex stream found is used.
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.
22 __all__ = ["OggSpeex", "Open", "delete"]
24 from mutagen._vorbis import VCommentDict
25 from mutagen.ogg import OggPage, OggFileType, error as OggError
26 from mutagen._util import cdata
28 class error(OggError): pass
29 class OggSpeexHeaderError(error): pass
31 class OggSpeexInfo(object):
32 """Ogg Speex stream information.
35 bitrate - nominal bitrate in bits per second
36 channels - number of channels
37 length - file length in seconds, as a float
39 The reference encoder does not set the bitrate; in this case,
40 the bitrate will be 0.
45 def __init__(self, fileobj):
46 page = OggPage(fileobj)
47 while not page.packets[0].startswith("Speex "):
48 page = OggPage(fileobj)
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:
57 self.serial = page.serial
60 return "Ogg Speex, %.2f seconds" % self.length
62 class OggSpeexVComment(VCommentDict):
63 """Speex comments embedded in an Ogg bitstream."""
65 def __init__(self, fileobj, info):
69 page = OggPage(fileobj)
70 if page.serial == info.serial:
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)
76 def _inject(self, fileobj):
77 """Write tag data into the Speex comment packet/page."""
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)
87 # Look for the next page with that serial number, it'll start
90 page = OggPage(fileobj)
91 while page.serial != serial:
92 page = OggPage(fileobj)
94 # Then find all the pages with the comment packet.
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)
101 packets = OggPage.to_packets(old_pages, strict=False)
103 # Set the new comment packet.
104 packets[0] = self.write(framing=False)
106 new_pages = OggPage.from_packets(packets, old_pages[0].sequence)
107 OggPage.replace(fileobj, old_pages, new_pages)
109 class OggSpeex(OggFileType):
110 """An Ogg Speex file."""
113 _Tags = OggSpeexVComment
114 _Error = OggSpeexHeaderError
115 _mimes = ["audio/x-speex"]
117 def score(filename, fileobj, header):
118 return (header.startswith("OggS") * ("Speex " in header))
119 score = staticmethod(score)
123 def delete(filename):
124 """Remove tags from a file."""
125 OggSpeex(filename).delete()