sound_service.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 SoundCollectionService():
  9. # 设备是否已打开,成功为0;
  10. IsOpen = -1
  11. Service_Name = "sound_collection"
  12. # 初始化
  13. def __init__(self):
  14. self.status = 0
  15. # 初始化
  16. def initSoundCollection(self):
  17. if SoundCollectionService.IsOpen <> 0:
  18. SoundCollectionService.IsOpen = AudioCapture.Initialize()
  19. if SoundCollectionService.IsOpen <> 0:
  20. LoggingUtil.printLog(u"打开声音采集器失败")
  21. self.status = 0
  22. # return 0,u"打开声音采集器失败"
  23. else:
  24. LoggingUtil.printLog(u"打开声音采集器成功")
  25. self.status = 1
  26. # return 1,""
  27. # 是否静音(开始通道、采集时长、静音电压)
  28. def IsMute(self, collectionTime, muteVoltage):
  29. if SoundCollectionService.IsOpen <> 0:
  30. SoundCollectionService.IsOpen = AudioCapture.Initialize()
  31. percentMute = 0.0
  32. if SoundCollectionService.IsOpen <> 0:
  33. LoggingUtil.printLog(u"声音采集器未打开")
  34. else:
  35. percentMute = AudioCapture.IsMute(
  36. 0, collectionTime, muteVoltage)
  37. # 返回静音百分比;
  38. return percentMute
  39. # 检测声音是否有中断;
  40. def IsInterrupt(self, collectionTime, muteVoltage, interruptTime):
  41. if SoundCollectionService.IsOpen <> 0:
  42. SoundCollectionService.IsOpen = AudioCapture.Initialize()
  43. isInterrupt = False
  44. if SoundCollectionService.IsOpen <> 0:
  45. LoggingUtil.printLog(u"声音采集器未打开")
  46. else:
  47. isInterrupt = AudioCapture.IsInterrupt(
  48. 0, collectionTime, muteVoltage, interruptTime)
  49. # 返回静音百分比;
  50. return isInterrupt
  51. # 检测左右声道是否正常;
  52. def SoundTrackDetection(self, collectionTime, muteVoltage, interruptTime):
  53. if SoundCollectionService.IsOpen <> 0:
  54. SoundCollectionService.IsOpen = AudioCapture.Initialize()
  55. nResult = 0
  56. if SoundCollectionService.IsOpen <> 0:
  57. LoggingUtil.printLog(u"声音采集器未打开")
  58. else:
  59. nResult = AudioCapture.SoundTrackDetection(
  60. 0, collectionTime, muteVoltage, interruptTime)
  61. # 返回静音百分比;
  62. return nResult
  63. # 退出初始化环境
  64. def __def__(self):
  65. if SoundCollectionService.IsOpen == 0:
  66. SoundCollectionService.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 1:
  91. soundcollection = SoundCollectionService()
  92. soundcollection.initSoundCollection()
  93. if SoundCollectionService.IsOpen == 0:
  94. # 静音测试
  95. if 1:
  96. print u"\n#是否静音->"
  97. time.sleep(1)
  98. percentMute = soundcollection.IsMute( 8000, 1.0)
  99. print u"静音百分比:", percentMute
  100. # 停顿测试
  101. if 1:
  102. time.sleep(2)
  103. print u"\n#是否有停顿->"
  104. time.sleep(1)
  105. bInterrupt = soundcollection.IsInterrupt( 5000, 1.0, 400)
  106. if bInterrupt == True:
  107. print u"有停顿"
  108. else:
  109. print u"无停顿"
  110. # 声道检测
  111. if 1:
  112. print u"\n#声道检测"
  113. nRet = soundcollection.SoundTrackDetection( 8000, 1.0, 400)
  114. if nRet == 0:
  115. print u"声道正常"
  116. elif nRet == 1:
  117. print u"采集失败"
  118. elif nRet == -1:
  119. print u"左右声道完全无音"
  120. elif nRet == -2:
  121. print u"左声道无音"
  122. elif nRet == -3:
  123. print u"右声道无音"
  124. elif nRet == -4:
  125. print u"左声道停顿"
  126. elif nRet == -5:
  127. print u"右声道停顿"
  128. elif nRet == -6:
  129. print u"左右声道都停顿"