__load__.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # -*- coding: utf-8 -*-
  2. from demeter.core import *
  3. from datetime import *
  4. import time
  5. import uuid
  6. import os
  7. import os.path
  8. import wave
  9. import tempfile
  10. import cProfile
  11. import base64
  12. from pydub import AudioSegment
  13. from uuid import getnode as get_mac
  14. from abc import ABCMeta, abstractmethod
  15. from ctypes import CFUNCTYPE, c_char_p, c_int, cdll
  16. from contextlib import contextmanager
  17. def py_error_handler(filename, line, function, err, fmt):
  18. pass
  19. ERROR_HANDLER_FUNC = CFUNCTYPE(None, c_char_p, c_int, c_char_p, c_int, c_char_p)
  20. c_error_handler = ERROR_HANDLER_FUNC(py_error_handler)
  21. @contextmanager
  22. def no_alsa_error():
  23. try:
  24. asound = cdll.LoadLibrary('libasound.so')
  25. asound.snd_lib_error_set_handler(c_error_handler)
  26. yield
  27. asound.snd_lib_error_set_handler(None)
  28. except:
  29. yield
  30. pass
  31. def getPlayerByFileName(fname):
  32. foo, ext = os.path.splitext(fname)
  33. if ext == '.mp3':
  34. return Demeter.service('sox', 'player')
  35. elif ext == '.wav':
  36. return Demeter.service('wav', 'player')
  37. """
  38. 将 mp3 文件转成 wav
  39. :param file: mp3 文件路径
  40. :return: wav 文件路径
  41. """
  42. def convertMp3ToWav(file):
  43. target = file.replace(".mp3", ".wav")
  44. if not os.path.exists(file):
  45. Demeter.logger.critical("文件错误 {}".format(file))
  46. return None
  47. AudioSegment.from_mp3(file).export(target, format="wav")
  48. return target
  49. """
  50. 将 wav 文件转成 mp3
  51. :param file: wav 文件路径
  52. :return: mp3 文件路径
  53. """
  54. def convertWavToMp3(file):
  55. target = file.replace('.wav', '.mp3')
  56. if not os.path.exists(file):
  57. Demeter.logger.critical("文件错误 {}".format(file))
  58. return None
  59. AudioSegment.from_wav(file).export(target, format="mp3")
  60. return target
  61. """
  62. 从 wav 文件中读取 pcm
  63. :param file: wav 文件路径
  64. :return: pcm 数据
  65. """
  66. def getPcmFromWav(file):
  67. wav = wave.open(file, 'rb')
  68. return wav.readframes(wav.getnframes())
  69. """
  70. 二进制形式写入临时文件
  71. :param data: 二进制数据
  72. :param suffix: 后缀名
  73. :return: 文件保存后的路径
  74. """
  75. def writeTempFile(data, suffix):
  76. with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as f:
  77. f.write(data)
  78. tmpfile = f.name
  79. return tmpfile
  80. # 清理目录
  81. def clean():
  82. temp = Demeter.config['vecan']['temp']
  83. temp_files = os.listdir(temp)
  84. for f in temp_files:
  85. if os.path.isfile(os.path.join(temp, f)) and re.match(r''+Demeter.config['vecan']['outname']+'[\d]*\.wav', os.path.basename(f)):
  86. os.remove(os.path.join(temp, f))
  87. """ 获取缓存的语音 """
  88. def getCache(msg):
  89. md5 = Demeter.md5(msg)
  90. mp3_cache = os.path.join(Demeter.config['vecan']['temp'], md5 + '.mp3')
  91. wav_cache = os.path.join(Demeter.config['vecan']['temp'], md5 + '.wav')
  92. if os.path.exists(mp3_cache):
  93. return mp3_cache
  94. elif os.path.exists(wav_cache):
  95. return wav_cache
  96. return None
  97. """ 获取缓存的语音 """
  98. def saveCache(voice, msg):
  99. foo, ext = os.path.splitext(voice)
  100. md5 = Demeter.md5(msg)
  101. shutil.copyfile(voice, os.path.join(Demeter.config['vecan']['temp'], md5+ext))