aboutsummaryrefslogtreecommitdiff
path: root/music_sampler/actions
diff options
context:
space:
mode:
Diffstat (limited to 'music_sampler/actions')
-rw-r--r--music_sampler/actions/__init__.py10
-rw-r--r--music_sampler/actions/interrupt_wait.py5
-rw-r--r--music_sampler/actions/pause.py10
-rw-r--r--music_sampler/actions/play.py44
-rw-r--r--music_sampler/actions/run_command.py13
-rw-r--r--music_sampler/actions/seek.py19
-rw-r--r--music_sampler/actions/stop.py42
-rw-r--r--music_sampler/actions/stop_all_actions.py14
-rw-r--r--music_sampler/actions/unpause.py10
-rw-r--r--music_sampler/actions/volume.py28
-rw-r--r--music_sampler/actions/wait.py40
11 files changed, 235 insertions, 0 deletions
diff --git a/music_sampler/actions/__init__.py b/music_sampler/actions/__init__.py
new file mode 100644
index 0000000..658cef0
--- /dev/null
+++ b/music_sampler/actions/__init__.py
@@ -0,0 +1,10 @@
1from . import interrupt_wait
2from . import pause
3from . import play
4from . import run_command
5from . import seek
6from . import stop
7from . import stop_all_actions
8from . import unpause
9from . import volume
10from . import wait
diff --git a/music_sampler/actions/interrupt_wait.py b/music_sampler/actions/interrupt_wait.py
new file mode 100644
index 0000000..8f465f0
--- /dev/null
+++ b/music_sampler/actions/interrupt_wait.py
@@ -0,0 +1,5 @@
1def run(action, wait_id=None, **kwargs):
2 action.mapping.interrupt_wait(wait_id)
3
4def description(action, wait_id=None, **kwargs):
5 return "interrupt wait with id {}".format(wait_id)
diff --git a/music_sampler/actions/pause.py b/music_sampler/actions/pause.py
new file mode 100644
index 0000000..bb27734
--- /dev/null
+++ b/music_sampler/actions/pause.py
@@ -0,0 +1,10 @@
1def run(action, music=None, **kwargs):
2 for music in action.music_list(music):
3 if music.is_loaded_playing():
4 music.pause()
5
6def 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/music_sampler/actions/play.py b/music_sampler/actions/play.py
new file mode 100644
index 0000000..fdba95b
--- /dev/null
+++ b/music_sampler/actions/play.py
@@ -0,0 +1,44 @@
1def 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
20def 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/music_sampler/actions/run_command.py b/music_sampler/actions/run_command.py
new file mode 100644
index 0000000..1e80c1e
--- /dev/null
+++ b/music_sampler/actions/run_command.py
@@ -0,0 +1,13 @@
1import shlex, subprocess
2
3def run(action, command="", wait=False, **kwargs):
4 action.process = subprocess.Popen(command, shell=True)
5 if wait:
6 action.process.wait()
7
8def description(action, command="", wait=False, **kwargs):
9 message = "running command {}".format(command)
10 if wait:
11 message += " (waiting for its execution to finish)"
12
13 return message
diff --git a/music_sampler/actions/seek.py b/music_sampler/actions/seek.py
new file mode 100644
index 0000000..467af7d
--- /dev/null
+++ b/music_sampler/actions/seek.py
@@ -0,0 +1,19 @@
1def 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
5def 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/music_sampler/actions/stop.py b/music_sampler/actions/stop.py
new file mode 100644
index 0000000..88cc66d
--- /dev/null
+++ b/music_sampler/actions/stop.py
@@ -0,0 +1,42 @@
1def 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
19def 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
39def 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/music_sampler/actions/stop_all_actions.py b/music_sampler/actions/stop_all_actions.py
new file mode 100644
index 0000000..4ea875a
--- /dev/null
+++ b/music_sampler/actions/stop_all_actions.py
@@ -0,0 +1,14 @@
1def run(action, key_start_time=0, other_only=False, **kwargs):
2 if other_only:
3 action.mapping.stop_all_running(
4 except_key=action.key,
5 key_start_time=key_start_time)
6 else:
7 action.mapping.stop_all_running()
8
9def description(action, other_only=False, **kwargs):
10 message = "stopping all actions"
11 if other_only:
12 message += " except this key"
13
14 return message
diff --git a/music_sampler/actions/unpause.py b/music_sampler/actions/unpause.py
new file mode 100644
index 0000000..5fa88c3
--- /dev/null
+++ b/music_sampler/actions/unpause.py
@@ -0,0 +1,10 @@
1def run(action, music=None, **kwargs):
2 for music in action.music_list(music):
3 if music.is_loaded_paused():
4 music.unpause()
5
6def 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/music_sampler/actions/volume.py b/music_sampler/actions/volume.py
new file mode 100644
index 0000000..7dda3c1
--- /dev/null
+++ b/music_sampler/actions/volume.py
@@ -0,0 +1,28 @@
1def 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
7def 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/music_sampler/actions/wait.py b/music_sampler/actions/wait.py
new file mode 100644
index 0000000..ea42408
--- /dev/null
+++ b/music_sampler/actions/wait.py
@@ -0,0 +1,40 @@
1import threading
2
3def 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(
9 duration,
10 action.sleep_event.set)
11
12 if music is not None:
13 music.wait_end()
14
15 action.sleep_event_timer.start()
16 action.sleep_event.wait()
17
18def description(action, duration=0, music=None, set_wait_id=None, **kwargs):
19 message = ""
20 if music is None:
21 message += "waiting {}s" \
22 .format(duration)
23 elif duration == 0:
24 message += "waiting the end of « {} »" \
25 .format(music.name)
26 else:
27 message += "waiting the end of « {} » + {}s" \
28 .format(music.name, duration)
29
30 if set_wait_id is not None:
31 message += " (setting id = {})".format(set_wait_id)
32
33 return message
34
35def interrupt(action, duration=0, music=None, **kwargs):
36 if action.sleep_event is not None:
37 action.sleep_event.set()
38 action.sleep_event_timer.cancel()
39 if music is not None:
40 music.wait_event.set()