diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2016-07-26 16:35:05 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2016-07-26 16:35:05 +0200 |
commit | e4917bcc6c5355a82f05880a389d0a1fd868561d (patch) | |
tree | 56f030600ad200632d0e9030c3b470dcb846215d /helpers/actions | |
parent | db905e0706ab9a1f92102e86f677c66371be4621 (diff) | |
parent | c4f4f2a1d330d8e09021619bbb8dcaac4df0a602 (diff) | |
download | MusicSampler-e4917bcc6c5355a82f05880a389d0a1fd868561d.tar.gz MusicSampler-e4917bcc6c5355a82f05880a389d0a1fd868561d.tar.zst MusicSampler-e4917bcc6c5355a82f05880a389d0a1fd868561d.zip |
Merge branch 'actions_cleanup'
Diffstat (limited to 'helpers/actions')
-rw-r--r-- | helpers/actions/__init__.py | 10 | ||||
-rw-r--r-- | helpers/actions/command.py | 6 | ||||
-rw-r--r-- | helpers/actions/interrupt_wait.py | 5 | ||||
-rw-r--r-- | helpers/actions/pause.py | 10 | ||||
-rw-r--r-- | helpers/actions/play.py | 44 | ||||
-rw-r--r-- | helpers/actions/seek.py | 19 | ||||
-rw-r--r-- | helpers/actions/stop.py | 42 | ||||
-rw-r--r-- | helpers/actions/stop_all_actions.py | 5 | ||||
-rw-r--r-- | helpers/actions/unpause.py | 10 | ||||
-rw-r--r-- | helpers/actions/volume.py | 28 | ||||
-rw-r--r-- | helpers/actions/wait.py | 38 |
11 files changed, 217 insertions, 0 deletions
diff --git a/helpers/actions/__init__.py b/helpers/actions/__init__.py new file mode 100644 index 0000000..ea1e800 --- /dev/null +++ b/helpers/actions/__init__.py | |||
@@ -0,0 +1,10 @@ | |||
1 | from . import command | ||
2 | from . import interrupt_wait | ||
3 | from . import pause | ||
4 | from . import play | ||
5 | from . import seek | ||
6 | from . import stop | ||
7 | from . import stop_all_actions | ||
8 | from . import unpause | ||
9 | from . import volume | ||
10 | from . import wait | ||
diff --git a/helpers/actions/command.py b/helpers/actions/command.py new file mode 100644 index 0000000..96f72fe --- /dev/null +++ b/helpers/actions/command.py | |||
@@ -0,0 +1,6 @@ | |||
1 | def run(action, command="", **kwargs): | ||
2 | # FIXME: todo | ||
3 | pass | ||
4 | |||
5 | def description(action, command="", **kwargs): | ||
6 | return "running command {}".format(command) | ||
diff --git a/helpers/actions/interrupt_wait.py b/helpers/actions/interrupt_wait.py new file mode 100644 index 0000000..36766a2 --- /dev/null +++ b/helpers/actions/interrupt_wait.py | |||
@@ -0,0 +1,5 @@ | |||
1 | def run(action, wait_id=None): | ||
2 | action.mapping.interrupt_wait(wait_id) | ||
3 | |||
4 | def description(action, wait_id=None, **kwargs): | ||
5 | return "interrupt wait with id {}".format(wait_id) | ||
diff --git a/helpers/actions/pause.py b/helpers/actions/pause.py new file mode 100644 index 0000000..bb27734 --- /dev/null +++ b/helpers/actions/pause.py | |||
@@ -0,0 +1,10 @@ | |||
1 | def run(action, music=None, **kwargs): | ||
2 | for music in action.music_list(music): | ||
3 | if music.is_loaded_playing(): | ||
4 | music.pause() | ||
5 | |||
6 | def description(action, music=None, **kwargs): | ||
7 | if music is not None: | ||
8 | return "pausing « {} »".format(music.name) | ||
9 | else: | ||
10 | return "pausing all musics" | ||
diff --git a/helpers/actions/play.py b/helpers/actions/play.py new file mode 100644 index 0000000..fdba95b --- /dev/null +++ b/helpers/actions/play.py | |||
@@ -0,0 +1,44 @@ | |||
1 | def run(action, music=None, fade_in=0, start_at=0, | ||
2 | restart_if_running=False, volume=100, | ||
3 | loop=0, **kwargs): | ||
4 | for music in action.music_list(music): | ||
5 | if restart_if_running: | ||
6 | if music.is_in_use(): | ||
7 | music.stop() | ||
8 | music.play( | ||
9 | volume=volume, | ||
10 | fade_in=fade_in, | ||
11 | start_at=start_at, | ||
12 | loop=loop) | ||
13 | elif not music.is_in_use(): | ||
14 | music.play( | ||
15 | volume=volume, | ||
16 | fade_in=fade_in, | ||
17 | start_at=start_at, | ||
18 | loop=loop) | ||
19 | |||
20 | def description(action, music=None, fade_in=0, start_at=0, | ||
21 | restart_if_running=False, volume=100, loop=0, **kwargs): | ||
22 | message = "starting " | ||
23 | if music is not None: | ||
24 | message += "« {} »".format(music.name) | ||
25 | else: | ||
26 | message += "all musics" | ||
27 | |||
28 | if start_at != 0: | ||
29 | message += " at {}s".format(start_at) | ||
30 | |||
31 | if fade_in != 0: | ||
32 | message += " with {}s fade_in".format(fade_in) | ||
33 | |||
34 | message += " at volume {}%".format(volume) | ||
35 | |||
36 | if loop > 0: | ||
37 | message += " {} times".format(loop + 1) | ||
38 | elif loop < 0: | ||
39 | message += " in loop" | ||
40 | |||
41 | if restart_if_running: | ||
42 | message += " (restarting if already running)" | ||
43 | |||
44 | return message | ||
diff --git a/helpers/actions/seek.py b/helpers/actions/seek.py new file mode 100644 index 0000000..467af7d --- /dev/null +++ b/helpers/actions/seek.py | |||
@@ -0,0 +1,19 @@ | |||
1 | def run(action, music=None, value=0, delta=False, **kwargs): | ||
2 | for music in action.music_list(music): | ||
3 | music.seek(value=value, delta=delta) | ||
4 | |||
5 | def description(action, music=None, value=0, delta=False, **kwargs): | ||
6 | if delta: | ||
7 | if music is not None: | ||
8 | return "moving music « {} » by {:+d}s" \ | ||
9 | .format(music.name, value) | ||
10 | else: | ||
11 | return "moving all musics by {:+d}s" \ | ||
12 | .format(value) | ||
13 | else: | ||
14 | if music is not None: | ||
15 | return "moving music « {} » to position {}s" \ | ||
16 | .format(music.name, value) | ||
17 | else: | ||
18 | return "moving all musics to position {}s" \ | ||
19 | .format(value) | ||
diff --git a/helpers/actions/stop.py b/helpers/actions/stop.py new file mode 100644 index 0000000..88cc66d --- /dev/null +++ b/helpers/actions/stop.py | |||
@@ -0,0 +1,42 @@ | |||
1 | def run(action, music=None, fade_out=0, wait=False, | ||
2 | set_wait_id=None, **kwargs): | ||
3 | previous = None | ||
4 | for music in action.music_list(music): | ||
5 | if music.is_loaded_paused() or music.is_loaded_playing(): | ||
6 | if previous is not None: | ||
7 | previous.stop(fade_out=fade_out) | ||
8 | previous = music | ||
9 | else: | ||
10 | music.stop(fade_out=fade_out) | ||
11 | |||
12 | if previous is not None: | ||
13 | action.waiting_music = previous | ||
14 | previous.stop( | ||
15 | fade_out=fade_out, | ||
16 | wait=wait, | ||
17 | set_wait_id=set_wait_id) | ||
18 | |||
19 | def description(action, music=None, fade_out=0, wait=False, | ||
20 | set_wait_id=None, **kwargs): | ||
21 | |||
22 | message = "stopping " | ||
23 | if music is not None: | ||
24 | message += "music « {} »".format(music.name) | ||
25 | else: | ||
26 | message += "all musics" | ||
27 | |||
28 | if fade_out > 0: | ||
29 | message += " with {}s fadeout".format(fade_out) | ||
30 | if wait: | ||
31 | if set_wait_id is not None: | ||
32 | message += " (waiting the end of fadeout, with id {})"\ | ||
33 | .format(set_wait_id) | ||
34 | else: | ||
35 | message += " (waiting the end of fadeout)" | ||
36 | |||
37 | return message | ||
38 | |||
39 | def interrupt(action, music=None, fade_out=0, wait=False, | ||
40 | set_wait_id=None, **kwargs): | ||
41 | if action.waiting_music is not None: | ||
42 | action.waiting_music.wait_event.set() | ||
diff --git a/helpers/actions/stop_all_actions.py b/helpers/actions/stop_all_actions.py new file mode 100644 index 0000000..f3fc5fb --- /dev/null +++ b/helpers/actions/stop_all_actions.py | |||
@@ -0,0 +1,5 @@ | |||
1 | def run(action, **kwargs): | ||
2 | action.mapping.stop_all_running() | ||
3 | |||
4 | def description(action, **kwargs): | ||
5 | return "stopping all actions" | ||
diff --git a/helpers/actions/unpause.py b/helpers/actions/unpause.py new file mode 100644 index 0000000..5fa88c3 --- /dev/null +++ b/helpers/actions/unpause.py | |||
@@ -0,0 +1,10 @@ | |||
1 | def run(action, music=None, **kwargs): | ||
2 | for music in action.music_list(music): | ||
3 | if music.is_loaded_paused(): | ||
4 | music.unpause() | ||
5 | |||
6 | def description(action, music=None, **kwargs): | ||
7 | if music is not None: | ||
8 | return "unpausing « {} »".format(music.name) | ||
9 | else: | ||
10 | return "unpausing all musics" | ||
diff --git a/helpers/actions/volume.py b/helpers/actions/volume.py new file mode 100644 index 0000000..7dda3c1 --- /dev/null +++ b/helpers/actions/volume.py | |||
@@ -0,0 +1,28 @@ | |||
1 | def run(action, music=None, value=100, fade=0, delta=False, **kwargs): | ||
2 | if music is not None: | ||
3 | music.set_volume(value, delta=delta, fade=fade) | ||
4 | else: | ||
5 | action.mapping.set_master_volume(value, delta=delta, fade=fade) | ||
6 | |||
7 | def description(action, music=None, | ||
8 | value=100, delta=False, fade=0, **kwargs): | ||
9 | message = "" | ||
10 | if delta: | ||
11 | if music is not None: | ||
12 | message += "{:+d}% to volume of « {} »" \ | ||
13 | .format(value, music.name) | ||
14 | else: | ||
15 | message += "{:+d}% to volume" \ | ||
16 | .format(value) | ||
17 | else: | ||
18 | if music is not None: | ||
19 | message += "setting volume of « {} » to {}%" \ | ||
20 | .format(music.name, value) | ||
21 | else: | ||
22 | message += "setting volume to {}%" \ | ||
23 | .format(value) | ||
24 | |||
25 | if fade > 0: | ||
26 | message += " with {}s fade".format(fade) | ||
27 | |||
28 | return message | ||
diff --git a/helpers/actions/wait.py b/helpers/actions/wait.py new file mode 100644 index 0000000..f7d2a78 --- /dev/null +++ b/helpers/actions/wait.py | |||
@@ -0,0 +1,38 @@ | |||
1 | import threading | ||
2 | |||
3 | def run(action, duration=0, music=None, set_wait_id=None, **kwargs): | ||
4 | if set_wait_id is not None: | ||
5 | action.mapping.add_wait_id(set_wait_id, action) | ||
6 | |||
7 | action.sleep_event = threading.Event() | ||
8 | action.sleep_event_timer = threading.Timer(duration, action.sleep_event.set) | ||
9 | |||
10 | if music is not None: | ||
11 | music.wait_end() | ||
12 | |||
13 | action.sleep_event_timer.start() | ||
14 | action.sleep_event.wait() | ||
15 | |||
16 | def description(action, duration=0, music=None, set_wait_id=None, **kwargs): | ||
17 | message = "" | ||
18 | if music is None: | ||
19 | message += "waiting {}s" \ | ||
20 | .format(duration) | ||
21 | elif duration == 0: | ||
22 | message += "waiting the end of « {} »" \ | ||
23 | .format(music.name) | ||
24 | else: | ||
25 | message += "waiting the end of « {} » + {}s" \ | ||
26 | .format(music.name, duration) | ||
27 | |||
28 | if set_wait_id is not None: | ||
29 | message += " (setting id = {})".format(set_wait_id) | ||
30 | |||
31 | return message | ||
32 | |||
33 | def interrupt(action, duration=0, music=None, **kwargs): | ||
34 | if action.sleep_event is not None: | ||
35 | action.sleep_event.set() | ||
36 | action.sleep_event_timer.cancel() | ||
37 | if music is not None: | ||
38 | music.wait_event.set() | ||