audio.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright (C) 2001-2007 Python Software Foundation
  2. # Author: Anthony Baxter
  3. # Contact: email-sig@python.org
  4. """Class representing audio/* type MIME documents."""
  5. __all__ = ['MIMEAudio']
  6. from io import BytesIO
  7. from email import encoders
  8. from email.mime.nonmultipart import MIMENonMultipart
  9. class MIMEAudio(MIMENonMultipart):
  10. """Class for generating audio/* MIME documents."""
  11. def __init__(self, _audiodata, _subtype=None,
  12. _encoder=encoders.encode_base64, *, policy=None, **_params):
  13. """Create an audio/* type MIME document.
  14. _audiodata contains the bytes for the raw audio data. If this data
  15. can be decoded as au, wav, aiff, or aifc, then the
  16. subtype will be automatically included in the Content-Type header.
  17. Otherwise, you can specify the specific audio subtype via the
  18. _subtype parameter. If _subtype is not given, and no subtype can be
  19. guessed, a TypeError is raised.
  20. _encoder is a function which will perform the actual encoding for
  21. transport of the image data. It takes one argument, which is this
  22. Image instance. It should use get_payload() and set_payload() to
  23. change the payload to the encoded form. It should also add any
  24. Content-Transfer-Encoding or other headers to the message as
  25. necessary. The default encoding is Base64.
  26. Any additional keyword arguments are passed to the base class
  27. constructor, which turns them into parameters on the Content-Type
  28. header.
  29. """
  30. if _subtype is None:
  31. _subtype = _what(_audiodata)
  32. if _subtype is None:
  33. raise TypeError('Could not find audio MIME subtype')
  34. MIMENonMultipart.__init__(self, 'audio', _subtype, policy=policy,
  35. **_params)
  36. self.set_payload(_audiodata)
  37. _encoder(self)
  38. _rules = []
  39. # Originally from the sndhdr module.
  40. #
  41. # There are others in sndhdr that don't have MIME types. :(
  42. # Additional ones to be added to sndhdr? midi, mp3, realaudio, wma??
  43. def _what(data):
  44. # Try to identify a sound file type.
  45. #
  46. # sndhdr.what() had a pretty cruddy interface, unfortunately. This is why
  47. # we re-do it here. It would be easier to reverse engineer the Unix 'file'
  48. # command and use the standard 'magic' file, as shipped with a modern Unix.
  49. hdr = data[:512]
  50. fakefile = BytesIO(hdr)
  51. for testfn in _rules:
  52. if res := testfn(hdr, fakefile):
  53. return res
  54. else:
  55. return None
  56. def rule(rulefunc):
  57. _rules.append(rulefunc)
  58. return rulefunc
  59. @rule
  60. def _aiff(h, f):
  61. if not h.startswith(b'FORM'):
  62. return None
  63. if h[8:12] in {b'AIFC', b'AIFF'}:
  64. return 'x-aiff'
  65. else:
  66. return None
  67. @rule
  68. def _au(h, f):
  69. if h.startswith(b'.snd'):
  70. return 'basic'
  71. else:
  72. return None
  73. @rule
  74. def _wav(h, f):
  75. # 'RIFF' <len> 'WAVE' 'fmt ' <len>
  76. if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
  77. return None
  78. else:
  79. return "x-wav"