0

I am new to python. I am trying to to call a class's constructor but it is giving me the below error:

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

And I have the same issue with listen() as below. Please discard the rms()andrecord()since they are other functions And here is my code:

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()
AMC
  • 2,642
  • 7
  • 13
  • 35
夏思阳
  • 57
  • 1
  • 4
  • 10
  • Try renaming "rec" to "self". I'm not sure, but that might solve the problem – Rafael Apr 29 '20 at 07:03
  • 6
    Are you sure that’s what the code running actually looks like? (Well, the indentation is wrong, but I’m referring to `__init__(rec)`. The error would make more sense with `__init__(self, rec)`.) – Ry- Apr 29 '20 at 07:04
  • 2
    @Rafael That won't. Using the name `self` is just a convention, you could use anything else instead. – Thierry Lathuille Apr 29 '20 at 07:04
  • Also, please show the stack trace. (Is the error coming from your constructor, or from `pyaudio.PyAudio`?) – Ry- Apr 29 '20 at 07:05
  • @ThierryLathuille Ah ok thanks, I was not sure about it. – Rafael Apr 29 '20 at 07:05

2 Answers2

2

mmh I can not reproduce the error. I only focus on the __init__ method, because that is your crucial part.

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

And my setup

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

So please specify your versions and give some additional informations about your problem.

I assume that you use a constructor like

def __init__(self, rec):
   ...

but you don´t pass any argument to rec. This would explain your error:

Traceback (most recent call last):
  File ".../Test.py", line 20, in <module>
    k = Recorder()
TypeError: __init__() missing 1 required positional argument: 'rec'
Kampi
  • 1,798
  • 18
  • 26
  • Hi, it's the same version of 0.2.11. And I use def __init__(rec) without self. – 夏思阳 Apr 29 '20 at 07:41
  • Then there should be no error (see my example above). – Kampi Apr 29 '20 at 07:46
  • @ Kampi The error is still existed with saying: TypeError: __init__() missing 1 required positional argument: 'rec'. And I am trying to upload mu full code. – 夏思阳 Apr 29 '20 at 07:53
  • Please review your given code. I copy your code and define the unknown variables. With your complete code I got the error `TypeError: read() got an unexpected keyword argument 'execption_on_overflow'` which is different from your error. – Kampi Apr 29 '20 at 07:57
  • https://stackoverflow.com/questions/61497460/error-init-missing-1-required-positional-argument-rec – 夏思阳 Apr 29 '20 at 08:35
-1

I think the problem is in others methods and your code not show all... Record has others methods like "record" and "rms".

def listen(rec):

print('Listening beginning')

while True:

input = rec.stream.read(chunk, execption_on_overflow=False)

rms_val = rec.rms(input) # How I now if your errors it occuring here?

if rms_val > Threshold:

rec.record() # Or them here?

I can't now if your are calling the init() function in this methods...

Daniel A R F
  • 127
  • 7
  • The problem occur in the `__init__` method (see error message) and this method doesn´t use a method named `rms`. Additional you get other error messages with this error. The error message say something like a parameter error and not a unknown method error. – Kampi Apr 29 '20 at 07:32
  • I can't now if your are calling the __init__() function in this methods... – Daniel A R F Apr 29 '20 at 07:48
  • @Daniel A R F. My full code is here: https://stackoverflow.com/questions/61497460/error-init-missing-1-required-positional-argument-rec\ – 夏思阳 Apr 29 '20 at 08:38