Python 错误:uu init_uu()缺少1个必需的位置参数:';rec&x27;
我是python新手。我试图做麦克风文件,应该检测,听,记录和写的.wav文件。但是,当我尝试运行该文件时,它给了我一个错误。它说:Python 错误:uu init_uu()缺少1个必需的位置参数:';rec&x27;,python,python-3.x,microphone,Python,Python 3.x,Microphone,我是python新手。我试图做麦克风文件,应该检测,听,记录和写的.wav文件。但是,当我尝试运行该文件时,它给了我一个错误。它说: TypeError:\uuuu init\uuuuu()缺少1个必需的位置参数:“rec” 我对listen()有如下相同的问题。这是我的密码: class Recorder: # compute the rms def rms(frame): count = len(frame) / swidth format = "%dh" % (count)
TypeError:\uuuu init\uuuuu()缺少1个必需的位置参数:“rec”
我对listen()有如下相同的问题。这是我的密码:
class Recorder:
# compute the rms
def rms(frame):
count = len(frame) / swidth
format = "%dh" % (count)
shorts = struct.unpack(format, frame)
sum_squares = 0.0
for sample in shorts:
n = sample * SHORT_NORMALIZE
sum_squares += n * n
rms = math.pow(sum_squares / count, 0.5);
return rms * 1000
# create an interface to portaudio
@staticmethod
def __init__(rec):
rec.p= pyaudio.PyAudio()
rec.stream= rec.p.open(format=FORMAT, channels=CHANNELS, rate=RATE,input=True,output=True,frames_per_buffer=chunk)
# record the detected sound
def record(rec):
print('sound detected, record begin')
frames = []
current = time.time()
end = current + Timeout
while current <= end:
data = rec.stream.read(chunk, execption_on_overflow=False)
if rec.rms(data) >= Threshold:
end = time.time() + Timeout
current = time.time()
frames.append(data)
rec.write(b''.join(frames))
# write the recorded sound to .wav file
def write(rec, recording):
files = len(os.listdir(FileNameTmp))
filename = os.path.join(FileNameTmp,time.strftime('%Y_%m_%d-%H_%H_%M_%S.wav'.format(FORMAT)))
filename2 = time.strftime('%Y_%m_%d-%H_%H_%M_%S.wav'.format(FORMAT))
wf = wave.open(filename, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(rec.p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(recording)
wf.close()
print('Written to file: {}'.format(filename))
print('Returning to listening')
# listen to the sound
def listen(rec):
print('Listening beginning')
while True:
input = rec.stream.read(chunk, execption_on_overflow=False)
rms_val = rec.rms(input)
if rms_val > Threshold:
rec.record()
k = Recorder()
k.listen()
类记录器:
#计算rms
def rms(帧):
计数=长度(帧)/开关
format=“%dh”%(计数)
shorts=结构解包(格式、帧)
平方和=0.0
对于短裤样品:
n=样本*短\标准化
平方和+=n*n
rms=数学功率(平方和/计数,0.5);
返回rms*1000
#创建portaudio的接口
@静力学方法
定义初始化(rec):
rec.p=pyaudio.pyaudio()
rec.stream=rec.p.open(format=format,channels=channels,rate=rate,input=True,output=True,frames\u per\u buffer=chunk)
#记录检测到的声音
def记录(rec):
打印('检测到声音,开始录制')
帧=[]
当前=时间。时间()
结束=当前+超时
当电流=阈值时:
结束=时间。时间()+超时
当前=时间。时间()
frames.append(数据)
记录写入(b“”。连接(帧))
#将录制的声音写入.wav文件
def写入(记录、记录):
files=len(os.listdir(FileNameTmp))
filename=os.path.join(FileNameTmp,time.strftime(“%Y\u%m\u%d-%H\u%m\u%S.wav.”格式(format)))
filename2=time.strftime(“%Y\u%m\u%d-%H\u%H\u%m\u%S.wav.”格式(format))
wf=wave.open(文件名“wb”)
wf.设置通道(通道)
wf.setsampwidth(rec.p.get_样本大小(格式))
设置帧率(速率)
wf.writeframes(记录)
wf.close()
打印('写入文件:{}'。格式(文件名))
打印('返回到侦听')
#听这个声音
def侦听(rec):
打印('侦听开始')
尽管如此:
input=rec.stream.read(chunk,execption\u on\u overflow=False)
rms_val=rec.rms(输入)
如果rms_val>阈值:
记录
k=记录器()
k、 听
您将\uuuu init\uuuu
声明为staticmethod
,因此在创建类的对象时,没有self
(或者在您的例子中是rec
)参数传递到构造函数中
k = Recorder() <--- will pass a reference, typical named self, into your __init__
k=Recorder()代码中存在许多问题,其中之一是成员函数的第一个参数必须是self
(如this
指针)。这是因为python的哲学是
显性比隐性好
通常,python类如下所示:
class MyClass:
def __init__(self, val):
self.val = val
def getVal(self):
return self.val
def setVal(self, val):
self.val = val
obj = MyClass(5)
print(obj.getVal()) # prints 5
obj.setVal(4)
print(obj.getVal()) # prints 4
当您将代码重构为上述语法时,您的代码将开始工作。另外,一定要挑选一些系统地介绍python的书:)