-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence_tests.py
94 lines (63 loc) · 2.19 KB
/
sequence_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.slider import Slider
from kivy.core.audio import SoundLoader
from os.path import join, dirname
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
import datetime, threading, time #
seq_should_run = False
class SequencerTimer(object): # http://stackoverflow.com/questions/8418067/python-what-is-the-proper-way-to-pass-arguments-to-threading-thread-instance
def __init__(self, state, tempo):
self.state = state
self.tempo = tempo
self.set_tickinterval()
def set_state(self, state):
self.state = state
def set_tempo(self, tempo):
self.tempo = tempo
self.set_tickinterval()
def set_tickinterval(self):
self.tickinterval = 60 / self.tempo
def toggleState(self):
if self.state != "playing":
self.state = "playing"
timerThread = threading.Thread(target=self.run)
timerThread.daemon = True
timerThread.start()
else:
self.state = "stopped"
def run(self):
next_tick = time.time()
while self.state == "playing":
print datetime.datetime.now()
next_tick = next_tick + 0.01
time.sleep(60 / self.tempo)
seqTimer = SequencerTimer("stopped", 120)
def tick():
while seq_should_run:
print datetime.datetime.now()
time.sleep(60 / self.tempo)
#timerThread = threading.Thread(target=tick)
#timerThread.daemon = True
def ticker(instance):
seqTimer.toggleState()
filename = join(dirname(__file__), 'sounds/1.wav')
sound = SoundLoader.load(filename)
tempo = 120
def playsound(dt):
if sound:
sound.play()
def start_sequnce(instance):
Clock.schedule_interval(playsound, tempo / 60)
def tempo_change(instance, event):
tempo = instance.value
Clock.schedule_interval(playsound, tempo / 60)
class Seq(App):
def build(self):
main_layout = BoxLayout()
main_layout.add_widget(Button(on_press=ticker, tag="play"))
main_layout.add_widget(Slider(min=50, max=200, value=120, on_touch_move=tempo_change))
return main_layout
if __name__ == '__main__':
Seq().run()