Error:__init__() missing 1 required positional argument: 'rec'
Error:__init__() missing 1 required positional argument: 'rec'
我是 python 的新手。我正在尝试制作应该检测、收听、记录和写入 .wav 文件的麦克风文件。但是,当我尝试 运行 文件时,它给了我一个错误。它在说:
TypeError: __init__() missing 1 required positional argument: '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()
您将 __init__
声明为 staticmethod
,因此在您创建对象时没有 self
(或者在您的情况下是 rec
)参数传递给您的构造函数你的 class.
k = Recorder() <--- will pass a reference, typical named self, into your __init__
请看
当您想使用静态构造函数时。
你的代码有很多问题,其中一个是,成员函数的第一个参数必须是self
(比如this
指针)。这是因为,python的理念是,
Explicit is better than implicit
一般来说,python class 看起来像这样:
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 的书 :)
我是 python 的新手。我正在尝试制作应该检测、收听、记录和写入 .wav 文件的麦克风文件。但是,当我尝试 运行 文件时,它给了我一个错误。它在说:
TypeError: __init__() missing 1 required positional argument: '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()
您将 __init__
声明为 staticmethod
,因此在您创建对象时没有 self
(或者在您的情况下是 rec
)参数传递给您的构造函数你的 class.
k = Recorder() <--- will pass a reference, typical named self, into your __init__
请看
当您想使用静态构造函数时。
你的代码有很多问题,其中一个是,成员函数的第一个参数必须是self
(比如this
指针)。这是因为,python的理念是,
Explicit is better than implicit
一般来说,python class 看起来像这样:
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 的书 :)