Skip to content

Commit 31e7dfd

Browse files
committed
improve examples
1 parent 67e0e1b commit 31e7dfd

File tree

4 files changed

+102
-24
lines changed

4 files changed

+102
-24
lines changed

examples/base.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
from sys import argv
3+
from time import sleep
4+
5+
from pyrilla import core
6+
7+
8+
def main(file_handle):
9+
if len(argv) != 2:
10+
print """
11+
usage: program filename"
12+
"""
13+
exit(1)
14+
15+
else:
16+
filename = argv[1]
17+
ext = filename.rpartition('.')[2]
18+
19+
print("will load %s as %s" % (filename, ext))
20+
file_handle(filename, ext)
21+
22+
23+
def update_till_interrupt(on_interrupt=None):
24+
while True:
25+
try:
26+
core.update()
27+
sleep(0.001)
28+
except KeyboardInterrupt:
29+
print("interrupted")
30+
31+
if on_interrupt:
32+
on_interrupt()
33+
else:
34+
print("unhandled")
35+
break
36+

examples/loop.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,37 @@
11
# -*- coding: utf-8 -*-
2+
from pyrilla import core
3+
4+
from base import main, update_till_interrupt
5+
6+
again = 0
7+
8+
def file_handle(filename, ext):
9+
sound = core.Sound(filename, ext)
10+
voice = core.Voice(sound, loop=True)
11+
12+
voice.play()
13+
14+
def on_interrupt():
15+
"""
16+
Handle subsequent keyboard interrupts:
17+
* 1st - toggle voice
18+
* 2nd - toggle voice again
19+
* 3rd - quit
20+
"""
21+
global again
22+
23+
if again >= 2:
24+
print("quiting")
25+
quit()
26+
27+
else:
28+
again += 1
29+
voice.toggle()
30+
31+
print(on_interrupt.__doc__)
32+
33+
update_till_interrupt(on_interrupt)
34+
35+
36+
if __name__ == "__main__":
37+
main(file_handle)

examples/play.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
11
# -*- coding: utf-8 -*-
2-
from sys import argv
3-
from pyrilla import internal
4-
from time import sleep
2+
from pyrilla import core
3+
4+
from base import main, update_till_interrupt
5+
56

67
def finished(sound):
78
print("finished:", sound)
89
quit()
910

10-
go_on = False
11-
12-
def main():
13-
if len(argv) != 2:
14-
print """
15-
usage: program filename"
16-
"""
17-
exit(1)
18-
else:
19-
filename = argv[1]
20-
ext = filename.rpartition('.')[2]
2111

22-
print("will load %s as %s" % (filename, ext))
23-
s = internal.Sound(filename, ext)
12+
def file_handle(filename, ext):
13+
print("will load %s as %s" % (filename, ext))
14+
sound = core.Sound(filename, ext)
15+
sound.play(finished)
2416

25-
s.play(on_finish=finished)
17+
update_till_interrupt()
2618

27-
try:
28-
while True:
29-
internal.update()
30-
sleep(0.001)
31-
except KeyboardInterrupt:
32-
print("quiting")
3319

3420
if __name__ == "__main__":
35-
main()
21+
main(file_handle)

examples/play_voice.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
11
# -*- coding: utf-8 -*-
2+
from pyrilla import core
3+
4+
from base import main, update_till_interrupt
5+
6+
7+
def finished(sound):
8+
print("finished:", sound)
9+
quit()
10+
11+
12+
def file_handle(filename, ext):
13+
sound = core.Sound(filename, ext)
14+
voice = core.Voice(sound)
15+
16+
voice.play(finished)
17+
update_till_interrupt()
18+
19+
20+
if __name__ == "__main__":
21+
main(file_handle)

0 commit comments

Comments
 (0)