def recode(self):
pa = PyAudio()
stream = pa.open(format=paInt16, channels=self.nchannel, rate=self.SAMPLING_RATE, input=True, frames_per_buffer=self.NUM_SAMPLES)
save_count = 0
save_buffer = []
time_out = self.TIME_OUT
NO_WORDS=self.NO_WORDS
while True and NO_WORDS:
time_out -= 1
print 'time_out in', time_out # ??NUM_SAMPLES???
string_audio_data = stream.read(self.NUM_SAMPLES) # ???????????
audio_data = np.fromstring(string_audio_data, dtype=np.short)
# ??????????
NO_WORDS -= 1
if np.max(audio_data) > self.UPPER_LEVEL:
NO_WORDS=self.NO_WORDS
print 'self.NO_WORDS ', NO_WORDS
print 'np.max(audio_data) ', np.max(audio_data)
# ????LOWER_LEVEL??????
large_sample_count = np.sum( audio_data > self.LOWER_LEVEL )
# ??????COUNT_NUM??????SAVE_LENGTH??
if large_sample_count > self.COUNT_NUM:
save_count = self.SAVE_LENGTH
else:
save_count -= 1
#print 'save_count',save_count
# ??????????save_buffer?
if save_count < 0:
save_count = 0
elif save_count > 0 :
save_buffer.append( string_audio_data )
else:
pass
# ?save_buffer??????WAV???WAV????????????
if len(save_buffer) > 0 and NO_WORDS==0:
self.Voice_String = save_buffer
save_buffer = []
rospy.loginfo( "Recode a piece of voice successfully!")
#return self.Voice_String
elif len(save_buffer) > 0 and time_out==0:
self.Voice_String = save_buffer
save_buffer = []
rospy.loginfo( "Recode a piece of voice successfully!")
#return self.Voice_String
else:
pass
#rospy.loginfo( '\n\n')
评论列表
文章目录