0

I am trying to trigger a timer when there is audio and stop the timer when the audio is lower than a set value. There is a reset button that will reset the timer back to zero. The reset button works on the first attempt but the software becomes very slow when the reset button is pressed multiple times.

I am also trying to use the gpio as button to reset the timer but it does not work. My code for the gpio makes the whole code super slow. I am guessing the code is recursing or something. I am a newbie and cannot figure out what is wrong. Here is the code.

Currently running on raspberry pi version 4 with 4gb ram.

Hope someone can give some suggestions.

from tkinter import *
import alsaaudio
import audioop
#from PIL import ImageTk

# For the reset button through the gpio of the raspberry.
from gpiozero import Button as piButton
resetButton = piButton(21)

# Global variables.
started = False
seconds = 0
belowLow = 0

class App():
    
    def reset(self):
        global started
        global belowLow
        global seconds
        self.t.set(0)
        started = False
        seconds = 0
        belowLow = 0
        self.listen()
        
    def start(self):
        global started
        if started == False:
            started = True
            self.timer()
    
    def stop(self):
        global started
        global belowLow
        started = False
        belowLow = 0
     
    def timer(self):
        def seconds_timer():
            global started
            global seconds
            self.d = seconds
            self.t.set(self.d)
            if started:
                self.root.after(1000,self.timer)
                seconds += 1
                
        seconds_timer()
        

    def listen(self):
        def listenActions():
            #Take the audio stream and get the rms value
            input = self.p.read()
            rms_val = audioop.rms(input[1], 2)
            print(f'RMS: {rms_val}')
            
            # If the rms value is higher than 1000, start the timer
            if rms_val > 1000:
                self.start()
                
            #If the rms is below 50, stop timer with delay
            elif rms_val < 50:
                # this is to give a 100 milisecond delay before stoping
                global belowLow 
                if started:
                    belowLow += 1
                if started and belowLow > 100:
                    self.stop()
                    
            #To reset the timer using the gpio button        
            if resetButton.is_held:
                self.reset()
            #print(resetButton.is_held)

            self.root.after(1, self.listen)
            
        listenActions()

            
        
    def __init__(self):
        self.p = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, device='default', cardindex=- 1)
        self.p.setchannels(1)
        self.p.setrate(44100)
        self.p.setformat(alsaaudio.PCM_FORMAT_S16_LE)
        self.p.setperiodsize(160)

        self.root=Tk()
        self.root.title("Testing Testing")
        self.root.geometry("800x450")
        self.t = IntVar()
        self.t.set(0)
        self.lb = Label(self.root,textvariable=self.t,font=("Courier 40 bold"),bg="black", fg="white")
        self.bt3 = Button(self.root,text="Reset",command=self.reset,font=("Arial 12 bold"))
        self.lb.place(x=370,y=100)
        self.bt3.place(x=370,y=200)
        self.label = Label(self.root,text="",font=("Courier 40 bold"))
        self.listen()
        self.root.mainloop()

a = App()
0

Agustina, my classmate, pointed out the mistake.

    def reset(self):
        global started
        global belowLow
        global seconds
        self.t.set(0)
        started = False
        seconds = 0
        belowLow = 0
        self.listen() <-- should not be included

Because of self.listen() it was recursing. I hope I am using the correct term.

The good news is that once it was removed all is working great!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.