运行以下代码片段:
while True:
print('recording')
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1, dtype=np.int16)
sd.wait() # Wait until recording is finished
write('output.wav', fs, myrecording.astype(np.int16))
sound = AudioSegment.from_file("output.wav", format="wav")
lsound = sound + 35
silence = silence.detect_nonsilent(lsound, min_silence_len=1000, silence_thresh=-15)
silence = [((start/1000),(stop/1000)) for start,stop in silence] #convert to sec
print(silence)
print(len(silence))
if len(silence) == 0:
print('silence')
else:
print('have recording')
lsound.export("loutput.wav", format='wav')
在第一次迭代中,每件事情都很好地工作
recording
[(0.0, 0.502), (1.842, 5.0)]
2
have recording
但在下一次迭代中,得到:
recording
Traceback (most recent call last):
File "sttgft.py", line 43, in <module>
silence = silence.detect_nonsilent(lsound, min_silence_len=1000, silence_thresh=-15)
AttributeError: 'list' object has no attribute 'detect_nonsilent'
尝试使用del silence命令删除“silence”,但随后获取
Traceback (most recent call last):
File "sttgf1.py", line 43, in <module>
silence = silence.detect_nonsilent(lsound, min_silence_len=1000, silence_thresh=-15)
NameError: name 'silence' is not defined
不知道该试什么
silence
,它必须已经存在李>silence = silence.detect_nonsilent(lsound, min_silence_len=1000, silence_thresh=-15)
silence
的值是多少,因为一旦它完成第一次迭代silence
就成为一个列表数据类型,上面提到的同一行代码将执行list.detect_nonsilent
。这就是为什么在第一次迭代之后会出现第一个错误李>愚蠢的错误。在“silence=silent.detect\u nonsilent…”中使用名称“silence” 导致错误的原因。有一次我改为“沉默=沉默。检测非沉默…”,效果非常好
相关问题 更多 >
编程相关推荐