util.py 961 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. from struct import *
  3. __all__=['wav2pcm', 'GetDefaultContext']
  4. def GetDefaultContext():
  5. """
  6. Return Default Context Object
  7. """
  8. return {
  9. 'sdk': {
  10. 'name': 'nls-python-sdk',
  11. 'version': '0.0.1',
  12. 'language': 'python'
  13. }
  14. }
  15. def wav2pcm(wavfile, pcmfile):
  16. """
  17. Turn wav into pcm
  18. Parameters
  19. ----------
  20. wavfile: str
  21. wav file path
  22. pcmfile: str
  23. output pcm file path
  24. """
  25. with open(wavfile, 'rb') as i, open(pcmfile, 'wb') as o:
  26. i.seek(0)
  27. _id = i.read(4)
  28. _id = unpack('>I', _id)
  29. _size = i.read(4)
  30. _size = unpack('<I', _size)
  31. _type = i.read(4)
  32. _type = unpack('>I', _type)
  33. if _id[0] != 0x52494646 or _type[0] != 0x57415645:
  34. raise ValueError('not a wav!')
  35. i.read(32)
  36. result = i.read()
  37. o.write(result)