aiplay.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # encoding:utf-8
  2. '''
  3. (ok)
  4. '''
  5. import os
  6. import ctypes
  7. import platform
  8. Voice_Record_Callback = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_int, ctypes.c_int, ctypes.c_int,
  9. ctypes.POINTER(ctypes.c_char))
  10. def read_config_file(filename):
  11. with open(filename, 'r') as file:
  12. return file.read()
  13. class VoiceRecord:
  14. def __init__(self, callback=None):
  15. pwd = os.path.dirname(__file__)
  16. if platform.architecture()[0] == '32bit':
  17. self.dll = ctypes.CDLL(os.path.join(pwd, "aiplay32.dll"))
  18. else:
  19. self.dll = ctypes.CDLL(os.path.join(pwd, "aiplay64.dll"))
  20. self.dll.voice_record_init.argtypes = [ctypes.c_char_p, Voice_Record_Callback]
  21. self.dll.voice_record_init.restype = ctypes.c_int
  22. self.dll.voice_record_start.argtypes = []
  23. self.dll.voice_record_start.restype = ctypes.c_int
  24. self.dll.voice_record_stop.argtypes = []
  25. self.dll.voice_record_stop.restype = ctypes.c_int
  26. self.dll.voice_record_deinit.argtypes = []
  27. self.dll.voice_record_deinit.restype = None
  28. auth_code = read_config_file('auth.cfg')
  29. self.callback = Voice_Record_Callback(callback)
  30. rc = self.dll.voice_record_init(auth_code.encode('utf-8'), self.callback)
  31. print(f'voice_record_init:{rc}')
  32. if rc != 0:
  33. raise Exception("初始化失败")
  34. def __del__(self):
  35. self.dll.voice_record_deinit()
  36. def voice_record_start(self):
  37. return self.dll.voice_record_start()
  38. def voice_record_stop(self):
  39. return self.dll.voice_record_stop()