# -*- coding:utf-8 -*- import os import sys import time # from device_manager import * from ssat_sdk.utils import LoggingUtil from ssat_sdk.device_manage import AudioCapture class SoundCollectionService(): # 设备是否已打开,成功为0; IsOpen = -1 Service_Name = "sound_collection" # 初始化 def __init__(self): self.status = 0 # 初始化 def initSoundCollection(self): if SoundCollectionService.IsOpen <> 0: SoundCollectionService.IsOpen = AudioCapture.Initialize() if SoundCollectionService.IsOpen <> 0: LoggingUtil.printLog(u"打开声音采集器失败") self.status = 0 # return 0,u"打开声音采集器失败" else: LoggingUtil.printLog(u"打开声音采集器成功") self.status = 1 # return 1,"" # 是否静音(开始通道、采集时长、静音电压) def IsMute(self, collectionTime, muteVoltage): if SoundCollectionService.IsOpen <> 0: SoundCollectionService.IsOpen = AudioCapture.Initialize() percentMute = 0.0 if SoundCollectionService.IsOpen <> 0: LoggingUtil.printLog(u"声音采集器未打开") else: percentMute = AudioCapture.IsMute( 0, collectionTime, muteVoltage) # 返回静音百分比; return percentMute # 检测声音是否有中断; def IsInterrupt(self, collectionTime, muteVoltage, interruptTime): if SoundCollectionService.IsOpen <> 0: SoundCollectionService.IsOpen = AudioCapture.Initialize() isInterrupt = False if SoundCollectionService.IsOpen <> 0: LoggingUtil.printLog(u"声音采集器未打开") else: isInterrupt = AudioCapture.IsInterrupt( 0, collectionTime, muteVoltage, interruptTime) # 返回静音百分比; return isInterrupt # 检测左右声道是否正常; def SoundTrackDetection(self, collectionTime, muteVoltage, interruptTime): if SoundCollectionService.IsOpen <> 0: SoundCollectionService.IsOpen = AudioCapture.Initialize() nResult = 0 if SoundCollectionService.IsOpen <> 0: LoggingUtil.printLog(u"声音采集器未打开") else: nResult = AudioCapture.SoundTrackDetection( 0, collectionTime, muteVoltage, interruptTime) # 返回静音百分比; return nResult # 退出初始化环境 def __def__(self): if SoundCollectionService.IsOpen == 0: SoundCollectionService.IsOpen = AudioCapture.ExitInitialize() # 测试用例 if __name__ == "__main__": # 原接口调用测试 if 0: result = AudioCapture.Initialize() if result == 0: print u"初始化成功" else: print u"初始化失败" if result == 0: print u"\n#是否静音->" time.sleep(1) percentMute = AudioCapture.IsMute( 0, 8000, 1.0) print u"静音百分比:", percentMute time.sleep(2) print u"\n#是否有停顿->" time.sleep(1) bInterrupt = AudioCapture.IsInterrupt(0, 8000, 1.0, 400) if bInterrupt == True: print u"有停顿" else: print u"无停顿" # 类接口调用; if 1: soundcollection = SoundCollectionService() soundcollection.initSoundCollection() if SoundCollectionService.IsOpen == 0: # 静音测试 if 1: print u"\n#是否静音->" time.sleep(1) percentMute = soundcollection.IsMute( 8000, 1.0) print u"静音百分比:", percentMute # 停顿测试 if 1: time.sleep(2) print u"\n#是否有停顿->" time.sleep(1) bInterrupt = soundcollection.IsInterrupt( 5000, 1.0, 400) if bInterrupt == True: print u"有停顿" else: print u"无停顿" # 声道检测 if 1: print u"\n#声道检测" nRet = soundcollection.SoundTrackDetection( 8000, 1.0, 400) if nRet == 0: print u"声道正常" elif nRet == 1: print u"采集失败" elif nRet == -1: print u"左右声道完全无音" elif nRet == -2: print u"左声道无音" elif nRet == -3: print u"右声道无音" elif nRet == -4: print u"左声道停顿" elif nRet == -5: print u"右声道停顿" elif nRet == -6: print u"左右声道都停顿"