sound_manager.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # -*- coding:utf-8 -*-
  2. import os
  3. import sys
  4. import time
  5. # from device_manager import *
  6. from ssat_sdk.utils import LoggingUtil
  7. from ssat_sdk.device_manage import AudioCapture
  8. class SoundManager():
  9. # 设备是否已打开,成功为0;
  10. IsOpen = -1
  11. Service_Name = "sound_collection"
  12. # 初始化
  13. def __init__(self):
  14. self.status = 0
  15. if SoundManager.IsOpen <> 0:
  16. SoundManager.IsOpen = AudioCapture.Initialize()
  17. if SoundManager.IsOpen <> 0:
  18. LoggingUtil.printLog(u"打开声音采集器失败")
  19. self.status = 0
  20. # return 0,u"打开声音采集器失败"
  21. else:
  22. LoggingUtil.printLog(u"打开声音采集器成功")
  23. self.status = 1
  24. # 是否静音(开始通道、采集时长、静音电压)
  25. def IsMute(self, collectionTime, muteVoltage):
  26. if SoundManager.IsOpen <> 0:
  27. SoundManager.IsOpen = AudioCapture.Initialize()
  28. percentMute = 0.0
  29. if SoundManager.IsOpen <> 0:
  30. LoggingUtil.printLog(u"声音采集器未打开")
  31. else:
  32. percentMute = AudioCapture.IsMute(
  33. 0, collectionTime, muteVoltage)
  34. print u'IsMute:静音百分比:',percentMute
  35. # 返回静音百分比;
  36. return percentMute
  37. # 检测声音是否有中断;
  38. def IsInterrupt(self, collectionTime, muteVoltage, interruptTime):
  39. if SoundManager.IsOpen <> 0:
  40. SoundManager.IsOpen = AudioCapture.Initialize()
  41. isInterrupt = False
  42. if SoundManager.IsOpen <> 0:
  43. LoggingUtil.printLog(u"声音采集器未打开")
  44. else:
  45. isInterrupt = AudioCapture.IsInterrupt(
  46. 0, collectionTime, muteVoltage, interruptTime)
  47. print u'IsInterrupt:是否中断:',isInterrupt
  48. # 返回是否中断;
  49. return isInterrupt
  50. # 检测左右声道是否正常;
  51. def SoundTrackDetection(self, collectionTime, muteVoltage, interruptTime):
  52. if SoundManager.IsOpen <> 0:
  53. SoundManager.IsOpen = AudioCapture.Initialize()
  54. nResult = 0
  55. if SoundManager.IsOpen <> 0:
  56. LoggingUtil.printLog(u"声音采集器未打开")
  57. else:
  58. nResult = AudioCapture.SoundTrackDetection(
  59. 0, collectionTime, muteVoltage, interruptTime)
  60. print u'SoundTrackDetection:结果值',nResult
  61. # 返回结果值;
  62. return nResult
  63. # 退出初始化环境
  64. def __def__(self):
  65. if SoundManager.IsOpen == 0:
  66. SoundManager.IsOpen = AudioCapture.ExitInitialize()
  67. # 测试用例
  68. if __name__ == "__main__":
  69. # 原接口调用测试
  70. if 0:
  71. result = AudioCapture.Initialize()
  72. if result == 0:
  73. print u"初始化成功"
  74. else:
  75. print u"初始化失败"
  76. if result == 0:
  77. print u"\n#是否静音->"
  78. time.sleep(1)
  79. percentMute = AudioCapture.IsMute( 0, 8000, 1.0)
  80. print u"静音百分比:", percentMute
  81. time.sleep(2)
  82. print u"\n#是否有停顿->"
  83. time.sleep(1)
  84. bInterrupt = AudioCapture.IsInterrupt(0, 8000, 1.0, 400)
  85. if bInterrupt == True:
  86. print u"有停顿"
  87. else:
  88. print u"无停顿"
  89. # 类接口调用;
  90. if 0:
  91. soundcollection = SoundManager()
  92. if SoundManager.IsOpen == 0:
  93. # 静音测试
  94. if 1:
  95. print u"\n#是否静音->"
  96. time.sleep(1)
  97. percentMute = soundcollection.IsMute( 3600000, 1.0)
  98. print u"静音百分比:", percentMute
  99. # 停顿测试
  100. if 1:
  101. time.sleep(2)
  102. print u"\n#是否有停顿->"
  103. time.sleep(1)
  104. bInterrupt = soundcollection.IsInterrupt( 3600000, 1.0, 400)
  105. if bInterrupt == True:
  106. print u"有停顿"
  107. else:
  108. print u"无停顿"
  109. # 声道检测
  110. if 1:
  111. print u"\n#声道检测"
  112. nRet = soundcollection.SoundTrackDetection( 3600000, 1.0, 400)
  113. if nRet == 0:
  114. print u"声道正常"
  115. elif nRet == 1:
  116. print u"采集失败"
  117. elif nRet == -1:
  118. print u"左右声道完全无音"
  119. elif nRet == -2:
  120. print u"左声道无音"
  121. elif nRet == -3:
  122. print u"右声道无音"
  123. elif nRet == -4:
  124. print u"左声道停顿"
  125. elif nRet == -5:
  126. print u"右声道停顿"
  127. elif nRet == -6:
  128. print u"左右声道都停顿"
  129. # 压测;
  130. if 1:
  131. soundcollection = SoundManager()
  132. if SoundManager.IsOpen == 0:
  133. while True:
  134. # 静音测试
  135. if 1:
  136. print u"\n#是否静音->"
  137. percentMute = soundcollection.IsMute( 10000, 1.0)
  138. print u"静音百分比:", percentMute
  139. # 停顿测试
  140. if 1:
  141. print u"\n#是否有停顿->"
  142. bInterrupt = soundcollection.IsInterrupt( 10000, 1.0, 400)
  143. if bInterrupt == True:
  144. print u"有停顿"
  145. else:
  146. print u"无停顿"
  147. # 声道检测
  148. if 1:
  149. print u"\n#声道检测"
  150. nRet = soundcollection.SoundTrackDetection( 10000, 1.0, 400)
  151. if nRet == 0:
  152. print u"声道正常"
  153. elif nRet == 1:
  154. print u"采集失败"
  155. elif nRet == -1:
  156. print u"左右声道完全无音"
  157. elif nRet == -2:
  158. print u"左声道无音"
  159. elif nRet == -3:
  160. print u"右声道无音"
  161. elif nRet == -4:
  162. print u"左声道停顿"
  163. elif nRet == -5:
  164. print u"右声道停顿"
  165. elif nRet == -6:
  166. print u"左右声道都停顿"
  167. #end while
  168. #end if