2

I'm building a metronome in python and I'd like to figure out a way to update the bpm on the fly. Is there a way to structure things so I can update a functions argument(s) while it's running?

My metronome is currently only accepting one argument "bpm" and I want to build a tap-tempo function where I'll use the time delta between two spacebar presses (and releases) to update the arguments' bpm. Here's a simplified version of what I have so far.

def Metronome(bpm):
    """Playback specified bpm."""
    while bpm >= 1 and bpm <= 300:
        playsound('sound.aif')    
        tpm = 60 / bpm
        time.sleep(tpm)  
        
    else:
        print('That\'s not a number between 1 and 300')

I've read a little about multiprocessing / threading but this is all new to me... I was planning to use the pynput module to capture the spacebar presses/releases.

I think that being able to update the tempo (bpm) on the fly is pretty important. Let me know if you have any suggestions on how I can pull this off.

Avi Granite
  • 27
  • 1
  • 5
  • 1
    I'm not that good at threading, but I feel like using classes here might help. – sybrg Jun 01 '21 at 22:37
  • 1
    Maybe [this tutorial](https://www.pythonforthelab.com/blog/handling-and-sharing-data-between-threads/) is helpful for you. – YiFei Jun 01 '21 at 23:37
  • 1
    You can't change the argument once a function/method is called. You can change the *value* of a mutable object. You can change the value of a variable accessible from a function/method as well as from the place where you want to change it. – greybeard Jun 02 '21 at 05:35
  • 1
    You can use an event handler for each time an input is given the bpm is updated. – WMRamadan Oct 05 '21 at 23:36

0 Answers0