66
77__all__ = ['MIMEAudio' ]
88
9- import sndhdr
10-
119from io import BytesIO
1210from email import encoders
1311from email .mime .nonmultipart import MIMENonMultipart
1412
1513
16-
17- _sndhdr_MIMEmap = {'au' : 'basic' ,
18- 'wav' :'x-wav' ,
19- 'aiff' :'x-aiff' ,
20- 'aifc' :'x-aiff' ,
21- }
14+ _tests = []
15+
16+ def _test_aifc_aiff (h , f ):
17+ if not h .startswith (b'FORM' ):
18+ return None
19+ if h [8 :12 ] in {b'AIFC' , b'AIFF' }:
20+ return 'x-aiff'
21+ else :
22+ return None
23+
24+ _tests .append (_test_aifc_aiff )
25+
26+
27+ def _test_au (h , f ):
28+ if h .startswith (b'.snd' ):
29+ return 'basic'
30+ else :
31+ return None
32+
33+ _tests .append (_test_au )
34+
35+
36+ def _test_wav (h , f ):
37+ import wave
38+ # 'RIFF' <len> 'WAVE' 'fmt ' <len>
39+ if not h .startswith (b'RIFF' ) or h [8 :12 ] != b'WAVE' or h [12 :16 ] != b'fmt ' :
40+ return None
41+ else :
42+ return "x-wav"
43+
44+ _tests .append (_test_wav )
45+
2246
2347# There are others in sndhdr that don't have MIME types. :(
2448# Additional ones to be added to sndhdr? midi, mp3, realaudio, wma??
@@ -31,14 +55,14 @@ def _whatsnd(data):
3155 """
3256 hdr = data [:512 ]
3357 fakefile = BytesIO (hdr )
34- for testfn in sndhdr . tests :
58+ for testfn in _tests :
3559 res = testfn (hdr , fakefile )
3660 if res is not None :
37- return _sndhdr_MIMEmap .get (res [0 ])
38- return None
61+ return res
62+ else :
63+ return None
3964
4065
41-
4266class MIMEAudio (MIMENonMultipart ):
4367 """Class for generating audio/* MIME documents."""
4468
@@ -47,7 +71,7 @@ def __init__(self, _audiodata, _subtype=None,
4771 """Create an audio/* type MIME document.
4872
4973 _audiodata is a string containing the raw audio data. If this data
50- can be decoded by the standard Python `sndhdr' module , then the
74+ can be decoded as au, wav, aiff, or aifc , then the
5175 subtype will be automatically included in the Content-Type header.
5276 Otherwise, you can specify the specific audio subtype via the
5377 _subtype parameter. If _subtype is not given, and no subtype can be
0 commit comments