Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

python - Error:__init__() missing 1 required positional argument: 'rec'

I am new to python. I am trying to do microphone file that ought to detect, listen, record and write the .wav files. However, it is giving me an error while I am trying to run the file. It is saying:

TypeError: __init__() missing 1 required positional argument: 'rec'

And I have the same issue with listen() as below. Here is my code:

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You declare your __init__ as staticmethod and so there is no self (or in your case rec) argument passed into your constructor when you create an object of your class.

k = Recorder() <--- will pass a reference, typical named self, into your __init__

Please take a look at

  1. this
  2. or this
  3. and this

when you want to use a static constructor.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...