@ -3,12 +3,53 @@ import pyaudio
from pydub import AudioSegment
import threading
import wave
import time
from pyOpenRPA . Utils import Text
def DeviceSystemSoundSearchIndex ( ) :
def DeviceMicrophoneIndex ( ) :
""" L-,W+: Выполнить поиск устройства, с помощью которого можно будет выполнить захват c микрофона.
"""
p = pyaudio . PyAudio ( )
lDeviceInfoDict = p . get_default_input_device_info ( )
lDefaultIndexInt = lDeviceInfoDict [ " index " ]
return lDefaultIndexInt
def DeviceSystemSoundIndex ( ) :
""" L-,W+: Выполнить поиск устройства, с помощью которого можно будет выполнить захват аудио, которое поступает из приложений. Например: аудиоконференции Zoom, whatsapp, telegram и т.д.
"""
pass
p = pyaudio . PyAudio ( )
inInputBool = True
inIsLoopbackBool = True
if inInputBool == True :
lDeviceInfoDict = p . get_default_output_device_info ( )
lDefaultIndexInt = lDeviceInfoDict [ " index " ]
lDefaultNameStr = lDeviceInfoDict [ " name " ]
lCatchIndexInt = None
lCatchDiffRatioFloat = 0.0
for lItemDict in DeviceListGet ( ) :
lCompareBool = False
if lItemDict [ " MaxOutputChannelsInt " ] > 0 :
if inIsLoopbackBool == True and lItemDict [ " HostApiStr " ] == " Windows WASAPI " : lCompareBool = True
elif inIsLoopbackBool == False : lCompareBool = True
if lCompareBool == True :
lDiffRationFloat = Text . SimilarityNoCase ( in1Str = lDefaultNameStr , in2Str = lItemDict [ " NameStr " ] )
if lDiffRationFloat > lCatchDiffRatioFloat : lCatchIndexInt = lItemDict [ " IndexInt " ]
else :
lDeviceInfoDict = p . get_default_output_device_info ( )
lDefaultIndexInt = lDeviceInfoDict [ " index " ]
lDefaultNameStr = lDeviceInfoDict [ " name " ]
lCatchIndexInt = None
lCatchDiffRatioFloat = 0.0
for lItemDict in DeviceListGet ( ) :
lCompareBool = False
if lItemDict [ " MaxInputChannelsInt " ] > 0 :
if inIsLoopbackBool == True and lItemDict [ " HostApiStr " ] == " Windows WASAPI " : lCompareBool = True
elif inIsLoopbackBool == False : lCompareBool = True
if lCompareBool == True :
lDiffRationFloat = Text . SimilarityNoCase ( in1Str = lDefaultNameStr , in2Str = lItemDict [ " NameStr " ] )
if lDiffRationFloat > lCatchDiffRatioFloat : lCatchIndexInt = lItemDict [ " IndexInt " ]
return lCatchIndexInt
def DeviceListGet ( ) :
""" L-,W+: Вернуть список аудио устройст (входящих и исходящих, микрофонов и динамиков).
@ -42,7 +83,7 @@ def DeviceListGet():
class Recorder :
mStatusStr = " 0_READY "
mAudio = pyaudio . PyAudio ( )
mAudio = None
mCaptureThread = None
mStream = None
@ -52,16 +93,22 @@ class Recorder:
mRecordedFramesList = [ ]
mUseLoopbackBool = True
mSampleRateInt = None
mSampleSizeInt = mAudio . get_sample_size ( pyaudio . paInt16 )
mSampleSizeInt = None
mCaptureBool = True
mFile NameStr = " aux "
mFile PathStr = " out "
mFileFormatStr = " mp3 "
def __init__ ( self , inDeviceInt = None ) :
self . mDeviceInt = inDeviceInt
def CaptureStart ( self ) :
if inDeviceInt == None : self . mDeviceInt = DeviceSystemSoundIndex ( )
def CaptureStart ( self , inFilePathStr = " out " , inFileFormatStr = " mp3 " , inDoChunkBool = False ) :
self . mFilePathStr = inFilePathStr
self . mFileFormatStr = inFileFormatStr
self . mAudio = pyaudio . PyAudio ( )
self . mSampleSizeInt = self . mAudio . get_sample_size ( pyaudio . paInt16 )
lDeviceInfoDict = self . mAudio . get_device_info_by_index ( self . mDeviceInt )
#Open stream
self . mSampleRateInt = int ( lDeviceInfoDict [ " defaultSampleRate " ] )
@ -77,32 +124,21 @@ class Recorder:
self . mCaptureThread . start ( )
def __Capture__ ( self ) :
while self . mCaptureBool == True :
while self . mCaptureBool == True :
self . mRecordedFramesList . append ( self . mStream . read ( self . mFramesInt ) )
self . mStream . stop_stream ( )
self . mStream . close ( )
#Close module
self . mAudio . terminate ( )
print ( " done " )
def CaptureStop ( self ) :
self . mCaptureBool = False
self . mCaptureThread . join ( )
print ( " done2 " )
self . CaptureChunk ( )
print ( " done3 " )
def CaptureChunk ( self ) :
print ( " CaptureChunk 1 " )
waveFile = wave . open ( f " { self . mFileNameStr } . { self . mFileFormatStr } " , ' wb ' )
waveFile . setnchannels ( self . mChannelCountInt )
waveFile . setsampwidth ( self . mSampleSizeInt )
waveFile . setframerate ( self . mSampleRateInt )
waveFile . writeframes ( b ' ' . join ( self . mRecordedFramesList ) )
waveFile . close ( )
lSound = AudioSegment (
# Advanced usage, if you have raw audio data:
sound = AudioSegment (
# raw audio data (bytes)
data = b ' ' . join ( self . mRecordedFramesList ) ,
# 2 byte (16 bit) samples
@ -112,11 +148,7 @@ class Recorder:
# stereo
channels = self . mChannelCountInt
)
print ( " CaptureChunk 2 " )
print ( len ( self . mRecordedFramesList ) )
lSound . export ( f " { self . mFileNameStr } . { self . mFileFormatStr } " , format = self . mFileFormatStr )
print ( " CaptureChunk 3 " )
sound . export ( f " { self . mFilePathStr } . { self . mFileFormatStr } " , format = f " { self . mFileFormatStr } " )
self . mRecordedFramesList = [ ]
def FileListGet ( self ) :