Automate
PythonMusic supports automation through the Automate class.
Automate allows repeated calls of functions (animating graphics, sweeping a volume or filter, stepping through data, and so on). It uses a single master clock, so everything stays in sync.
For more customizable automation, see the Timer class.
Creating an Automation
To automate the repeated calling of a function use Automate.add():
Automate.add(moveCircle)
Automation starts as soon as the first function is added.
Automation rate is measured in frames (or function calls) per second. The default is 60 frames per second.
For example:
randomlyMovingCircle.py
# Animate a circle by moving it randomly.
from gui import *
from random import *
from timer import *
d = Display("Automation Example", 600, 400)
# initialize circle coordinates
x = 300
y = 200
# create filled red circle
c = Circle(x, y, 25, Color.RED, True)
d.add(c)
def moveCircle():
global x, y # these will be updated
# update global circle coordinates
x = x + randint(-5, 5)
y = y + randint(-5, 5)
# now, move circle
d.move(c, x, y)
# start animation
Automate.add(moveCircle)
Functions
The following Automate functions are available:
| Function | Description |
|---|---|
add(action) |
Calls a function repeatedly based on automation rate (see setRate()). |
remove(action) |
Stop calling a registered function. |
resume() |
Resume automation after a pause. |
pause() |
Pause automation. |
getRate() |
Return how often automation runs. Default is 60 frames per second. |
setRate() |
Set how often automation runs. |
addWithValues(action, values) |
Step a function through a list of values, evenly spaced over time. |
addWithTimedValues(action, values, times) |
Step a function through a list of values, each delivered at its own time. |