Skip to content
Snippets Groups Projects
Commit ba1fd3b0 authored by iggy's avatar iggy
Browse files

basic sequencer and popcorn

parent 2359953f
No related branches found
No related tags found
No related merge requests found
import event
import hardware
from synth import tinysynth
import math
def xy_from_polar(r,deg):
#rad = deg/180*math.pi
return( (
r * math.sin(deg), #x
r * math.cos(deg) #y
) )
ctx = hardware.get_ctx()
ctx.text_align = ctx.CENTER
ctx.text_baseline = ctx.MIDDLE
synth = tinysynth(440,1)
synth.decay(25)
popcorn = [9,7,9,5,0,5,-3,999]
def on_step(data):
note = popcorn[data["step"]]
if note != 999:
synth.tone(note)
synth.start()
ctx.rgb(1,1,0).rectangle(-120,-120,240,240).fill()
(x,y) = xy_from_polar(90,-2*math.pi/8*data["step"]+math.pi)
size=180
ctx.rgb(0.8,0.8,0)
ctx.round_rectangle(
x-size/2,
y-size/2,
size,size,size//2
).fill()
ctx.move_to(x,y).rgb(0.5,0.5,0).text("{}".format(data["step"]))
hardware.display_update()
event.Sequence(bpm=160, steps=8, action=on_step, loop=True)
event.the_engine.eventloop()
...@@ -126,6 +126,22 @@ class EventTimed(Event): ...@@ -126,6 +126,22 @@ class EventTimed(Event):
def __repr__(self): def __repr__(self):
return ("event on tick {} ({})".format(self.deadline,self.name)) return ("event on tick {} ({})".format(self.deadline,self.name))
class Sequence():
def __init__(self,bpm=60,loop=True,steps=16,action=None):
self.bpm = bpm
if not action:
self.action = lambda data: print("step {}".format(data.get("step")))
else:
self.action = action
stepsize_ms = int(60*1000/bpm)
for i in range(steps):
EventTimed(stepsize_ms*i,name="seq{}".format(i),action=self.action, data={'step':i})
if loop:
EventTimed(stepsize_ms*steps,name="loop",action=lambda data: Sequence(bpm=bpm,loop=loop,steps=steps,action=action))
global the_engine global the_engine
the_engine = Engine() the_engine = Engine()
...@@ -136,5 +152,10 @@ Event(name="baz", ...@@ -136,5 +152,10 @@ Event(name="baz",
condition=lambda data: data.get('type')=="captouch" condition=lambda data: data.get('type')=="captouch"
) )
#Sequence(action=axelf)
print (the_engine.events_timed) print (the_engine.events_timed)
#e.eventloop() #the_engine.eventloop()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment