aboutsummaryrefslogtreecommitdiff
path: root/music_sampler/actions/wait.py
diff options
context:
space:
mode:
Diffstat (limited to 'music_sampler/actions/wait.py')
-rw-r--r--music_sampler/actions/wait.py54
1 files changed, 53 insertions, 1 deletions
diff --git a/music_sampler/actions/wait.py b/music_sampler/actions/wait.py
index bcee649..31439b8 100644
--- a/music_sampler/actions/wait.py
+++ b/music_sampler/actions/wait.py
@@ -1,4 +1,5 @@
1import threading 1import threading
2import time
2 3
3def run(action, duration=0, music=None, set_wait_id=None, **kwargs): 4def run(action, duration=0, music=None, set_wait_id=None, **kwargs):
4 action.mapping.add_wait(action, wait_id=set_wait_id) 5 action.mapping.add_wait(action, wait_id=set_wait_id)
@@ -8,10 +9,17 @@ def run(action, duration=0, music=None, set_wait_id=None, **kwargs):
8 duration, 9 duration,
9 action.sleep_event.set) 10 action.sleep_event.set)
10 11
12 action.sleep_event_initial_duration = duration
13 action.sleep_event_paused = False
14 action.sleep_event_left_time = duration
15
11 if music is not None: 16 if music is not None:
12 music.wait_end() 17 music.wait_end()
13 18
14 action.sleep_event_timer.start() 19 if duration <= 0 or not action.sleep_event_paused:
20 action.sleep_event_timer.start()
21 action.sleep_event_started_time = time.time()
22
15 action.sleep_event.wait() 23 action.sleep_event.wait()
16 24
17def description(action, duration=0, music=None, set_wait_id=None, **kwargs): 25def description(action, duration=0, music=None, set_wait_id=None, **kwargs):
@@ -34,6 +42,50 @@ def description(action, duration=0, music=None, set_wait_id=None, **kwargs):
34 42
35 return _(message).format(*formats) 43 return _(message).format(*formats)
36 44
45def pause(action, **kwargs):
46 if action.sleep_event_paused:
47 return
48
49 action.sleep_event_paused = True
50
51 if not action.sleep_event_timer.is_alive():
52 return
53
54 action.sleep_event_timer.cancel()
55
56 action.sleep_event_left_time = action.sleep_event_left_time\
57 - (time.time() - action.sleep_event_started_time)
58 if action.sleep_event_left_time < 0:
59 action.sleep_event.set()
60
61def unpause(action, **kwargs):
62 if not action.sleep_event_paused:
63 return
64
65 action.sleep_event_paused = False
66
67 action.sleep_event_timer = threading.Timer(
68 action.sleep_event_left_time,
69 action.sleep_event.set)
70
71 action.sleep_event_timer.start()
72 action.sleep_event_started_time = time.time()
73
74def reset(action, **kwargs):
75 action.sleep_event_timer.cancel()
76
77 action.sleep_event_left_time = action.sleep_event_initial_duration
78
79 if action.sleep_event_paused:
80 return
81
82 action.sleep_event_timer = threading.Timer(
83 action.sleep_event_left_time,
84 action.sleep_event.set)
85
86 action.sleep_event_timer.start()
87 action.sleep_event_started_time = time.time()
88
37def interrupt(action, duration=0, music=None, **kwargs): 89def interrupt(action, duration=0, music=None, **kwargs):
38 if action.sleep_event is not None: 90 if action.sleep_event is not None:
39 action.sleep_event.set() 91 action.sleep_event.set()