我是python的新手。我在这里试图做的是调用一个类的构造函数。但这给我一个错误。就是说:
TypeError: __init__() missing 1 required positional argument: 'rec'
而且我对listen()的问题如下。请丢弃rms()
和record()
,因为它们是其他函数,这是我的代码:
class Recorder:
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)
# 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()
我认为问题出在其他方法上,您的代码无法全部显示... Record还有其他方法,例如“ record”和“ rms”。
mmh,我无法重现该错误。我只关注__init__
方法,因为这是您的关键部分。
Test.py
import pyaudio
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
class Recorder:
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)
# listen to the sound
def listen(rec):
print('Listening beginning')
if(__name__ == "__main__"):
k = Recorder()
k.listen()
>> python Test.py
>> Listening beginning
和我的设置
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyaudio
>>> print(pyaudio.__version__)
0.2.11
因此,请指定您的版本并提供有关您的问题的其他信息。