I have a list:
beats = ['kick','','','','kick','','','','kick','','','','kick,'','','',]
it's 16 beats, one object one beat
then how to do this?
for i in beats:
if i =='kick':
# play kick
and how to play it with other speed(bpm)?
CodePudding user response:
I'm assuming you want to know how to set the 'bpm' using your loop. The most simple solution would be to just import pythons time module and use time.sleep(BPM)
import time
beats = ['kick','','','','kick','','','','kick','','','','kick']
for i in beats:
if i =='kick':
# play kick
time.sleep(0.25) #Sleeps half a second
Please note that as long as time.sleep is being executed, no other code in your program can be run. You would have to use threading if you want to play multiple instruments at once.
CodePudding user response:
from time import sleep
beats = ['kick','','','','kick','','','','kick','','','','kick','','','']
bpm = 160
for i in beats:
if i == 'kick':
print("kick!")
sleep(60 / bpm)
You can use the sleep method like this, then set the BPM variable and pass it into sleep like this.