c 20012007 python software foundation anthony baxter contact emailsigpython org class representing audio type mime documents all mimeaudio from email import encoders from email mime nonmultipart import mimenonmultipart class mimeaudiomimenonmultipart create an audio type mime document audiodata contains the bytes for the raw audio data if this data can be decoded as au wav aiff or aifc then the subtype will be automatically included in the contenttype header otherwise you can specify the specific audio subtype via the subtype parameter if subtype is not given and no subtype can be guessed a typeerror is raised encoder is a function which will perform the actual encoding for transport of the image data it takes one argument which is this image instance it should use getpayload and setpayload to change the payload to the encoded form it should also add any contenttransferencoding or other headers to the message as necessary the default encoding is base64 any additional keyword arguments are passed to the base class constructor which turns them into parameters on the contenttype header originally from the sndhdr module there are others in sndhdr that don t have mime types additional ones to be added to sndhdr midi mp3 realaudio wma try to identify a sound file type sndhdr what had a pretty cruddy interface unfortunately this is why we redo it here it would be easier to reverse engineer the unix file command and use the standard magic file as shipped with a modern unix riff len wave fmt len c 2001 2007 python software foundation anthony baxter contact email sig python org class representing audio type mime documents class for generating audio mime documents create an audio type mime document _audiodata contains the bytes for the raw audio data if this data can be decoded as au wav aiff or aifc then the subtype will be automatically included in the content type header otherwise you can specify the specific audio subtype via the _subtype parameter if _subtype is not given and no subtype can be guessed a typeerror is raised _encoder is a function which will perform the actual encoding for transport of the image data it takes one argument which is this image instance it should use get_payload and set_payload to change the payload to the encoded form it should also add any content transfer encoding or other headers to the message as necessary the default encoding is base64 any additional keyword arguments are passed to the base class constructor which turns them into parameters on the content type header originally from the sndhdr module there are others in sndhdr that don t have mime types additional ones to be added to sndhdr midi mp3 realaudio wma try to identify a sound file type sndhdr what had a pretty cruddy interface unfortunately this is why we re do it here it would be easier to reverse engineer the unix file command and use the standard magic file as shipped with a modern unix riff len wave fmt len
__all__ = ['MIMEAudio']

from email import encoders
from email.mime.nonmultipart import MIMENonMultipart


class MIMEAudio(MIMENonMultipart):
    

    def __init__(self, _audiodata, _subtype=None,
                 _encoder=encoders.encode_base64, *, policy=None, **_params):
        
        if _subtype is None:
            _subtype = _what(_audiodata)
        if _subtype is None:
            raise TypeError('Could not find audio MIME subtype')
        MIMENonMultipart.__init__(self, 'audio', _subtype, policy=policy,
                                  **_params)
        self.set_payload(_audiodata)
        _encoder(self)


_rules = []






def _what(data):
    
    
    
    
    
    for testfn in _rules:
        if res := testfn(data):
            return res
    else:
        return None


def rule(rulefunc):
    _rules.append(rulefunc)
    return rulefunc


@rule
def _aiff(h):
    if not h.startswith(b'FORM'):
        return None
    if h[8:12] in {b'AIFC', b'AIFF'}:
        return 'x-aiff'
    else:
        return None


@rule
def _au(h):
    if h.startswith(b'.snd'):
        return 'basic'
    else:
        return None


@rule
def _wav(h):
    
    if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
        return None
    else:
        return "x-wav"