[英]Python sounddevice module outputs Traceback (most recent call last):
Need help需要帮忙
What i want:我想要的是:
I want to record microphone and get the duration from a txt file我想录制麦克风并从 txt 文件中获取持续时间
Code:代码:
import sounddevice as sd
import numpy as np
import scipy.io.wavfile as wav
import soundfile as sf
fs=44100
dur = open("duration.txt", "r")
duration = dur.read()
print (duration)
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2,dtype='float64')
print ("Recording Audio")
sd.wait()
sf.write('myfile.wav', myrecording, fs, subtype='PCM_24')
duration.txt contains just a 7 duration.txt 只包含一个 7
Output: Output:
7
Traceback (most recent call last):
File "audio.py", line 9, in <module>
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2,dtype='float64')
File "C:\Users\User\Desktop\portfowardtesting\python\lib\site-packages\sounddevice.py", line 271, in rec
ctx.frames = ctx.check_out(out, frames, channels, dtype, mapping)
File "C:\Users\User\Desktop\portfowardtesting\python\lib\site-packages\sounddevice.py", line 2437, in check_out
out = np.empty((frames, channels), dtype, order='C')
TypeError: 'str' object cannot be interpreted as an integer
You should try to do print(duration * fs)
, this might give you a hint.你应该尝试做print(duration * fs)
,这可能会给你一个提示。
The problem is that the duration
you read from the file is a string, you should first convert it to a number.问题是您从文件中读取的duration
是一个字符串,您应该首先将其转换为数字。
In other words:换句话说:
duration = int(dur.read())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.