Error:__init__() missing 1 required positional argument: rec

2024/11/15 18:34:09

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) / swidthformat = "%dh" % (count)shorts = struct.unpack(format, frame)sum_squares = 0.0for sample in shorts:n = sample * SHORT_NORMALIZEsum_squares += n * nrms = 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 + Timeoutwhile current <= end:data = rec.stream.read(chunk, execption_on_overflow=False)if rec.rms(data) >= Threshold:end = time.time() + Timeoutcurrent = 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()
Answer

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.

https://en.xdnf.cn/q/119022.html

Related Q&A

Maya: Connect two Joint chains with Parent Constraint

So here is a snipit of an IK spine builder Ive been working on. Ive figure out how to make lists to duplicate the bound into an IK chain, what Ive got stuck on however is I want my list and for loop to…

What is the equivalent for onkeydown and onkeyup (Javascript events) in python?

There are events called onkeydown and onkeyup in Javascript. Can anyone please suggest the python equivalent of it?

Matching number string pairs

I have the following sample string:R10666: 273141 C1 + 273141 C2 + 273141 C3 + 273141 C4 + 273141 C5 - 273141 C6I want to obtain:[(273141,C1), ..., (- 273141, C6)]The numbers can be floating point numb…

Turning a text file into a tabular format [duplicate]

This question already has answers here:How do I print parameters of multiple objects in table form? [duplicate](2 answers)Line up columns of numbers (print output in table format)(7 answers)Closed 5 y…

Python: Read file with list as list

I have placed a list in a text file. I want python to read the text file and return the contents as a list. However, it is instead reading the content as a string:Text file:[a,b,c]Python:ids=[]writtenF…

Tkinter scrollbar not scrollable

I followed some tutorial on attaching a scrollbar to a textbox. However, in the tutorial, the scrollbar is really a "bar". When I tried myself, I can only press the arrows to move up or down,…

How to create multiple roles through discord.py bot?

I have been trying to make my discord bot create multiple roles through a command. But it simply doesnt work. Here is what I have done so far: @commands.command()async def create_roles(self, ctx):guild…

python: how do i know when i am on the last for cycle

for i in range(len(results_histogram)):if i!=len(results_histogram)-1:url+=str(results_histogram[i])+,my if statement is checking whether i am on the last loop, but it is not working. what am i doing w…

scrape text in python from https://brainly.co.id/tugas/148

scrape "Jawaban terverifikasi ahli" in green box from the url https://brainly.co.id/tugas/148, possibly the color of green tick icon to the left of it also(tag <use xlink:href="#icon-…

Percentage of how similar strings are in Python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…