blob: dda7689a4c87a9b81202edac54f8ae296bdec2f6 [file] [log] [blame]
Barry Warsaw53f8fe42001-10-09 19:41:18 +00001# Author: Anthony Baxter
2
3"""Class representing audio/* type MIME documents.
4"""
5
6import sndhdr
7from cStringIO import StringIO
8
Barry Warsaw524af6f2002-06-02 19:05:08 +00009from email import Errors
10from email import Encoders
11from email.MIMENonMultipart import MIMENonMultipart
Barry Warsaw53f8fe42001-10-09 19:41:18 +000012
13
14
15_sndhdr_MIMEmap = {'au' : 'basic',
16 'wav' :'x-wav',
17 'aiff':'x-aiff',
18 'aifc':'x-aiff',
19 }
20
21# There are others in sndhdr that don't have MIME types. :(
22# Additional ones to be added to sndhdr? midi, mp3, realaudio, wma??
23def _whatsnd(data):
24 """Try to identify a sound file type.
25
26 sndhdr.what() has a pretty cruddy interface, unfortunately. This is why
27 we re-do it here. It would be easier to reverse engineer the Unix 'file'
28 command and use the standard 'magic' file, as shipped with a modern Unix.
29 """
30 hdr = data[:512]
31 fakefile = StringIO(hdr)
32 for testfn in sndhdr.tests:
33 res = testfn(hdr, fakefile)
34 if res is not None:
35 return _sndhdr_MIMEmap.get(res[0])
36 return None
37
38
39
Barry Warsaw524af6f2002-06-02 19:05:08 +000040class MIMEAudio(MIMENonMultipart):
Barry Warsaw53f8fe42001-10-09 19:41:18 +000041 """Class for generating audio/* MIME documents."""
42
43 def __init__(self, _audiodata, _subtype=None,
44 _encoder=Encoders.encode_base64, **_params):
45 """Create an audio/* type MIME document.
46
47 _audiodata is a string containing the raw audio data. If this data
48 can be decoded by the standard Python `sndhdr' module, then the
Barry Warsaw03a75592002-09-30 21:29:10 +000049 subtype will be automatically included in the Content-Type header.
Barry Warsaw53f8fe42001-10-09 19:41:18 +000050 Otherwise, you can specify the specific audio subtype via the
51 _subtype parameter. If _subtype is not given, and no subtype can be
52 guessed, a TypeError is raised.
53
54 _encoder is a function which will perform the actual encoding for
55 transport of the image data. It takes one argument, which is this
56 Image instance. It should use get_payload() and set_payload() to
57 change the payload to the encoded form. It should also add any
Barry Warsaw03a75592002-09-30 21:29:10 +000058 Content-Transfer-Encoding or other headers to the message as
Barry Warsaw53f8fe42001-10-09 19:41:18 +000059 necessary. The default encoding is Base64.
60
61 Any additional keyword arguments are passed to the base class
Barry Warsaw03a75592002-09-30 21:29:10 +000062 constructor, which turns them into parameters on the Content-Type
Barry Warsaw53f8fe42001-10-09 19:41:18 +000063 header.
64 """
65 if _subtype is None:
66 _subtype = _whatsnd(_audiodata)
67 if _subtype is None:
68 raise TypeError, 'Could not find audio MIME subtype'
Barry Warsaw524af6f2002-06-02 19:05:08 +000069 MIMENonMultipart.__init__(self, 'audio', _subtype, **_params)
Barry Warsaw53f8fe42001-10-09 19:41:18 +000070 self.set_payload(_audiodata)
71 _encoder(self)