-
Notifications
You must be signed in to change notification settings - Fork 0
/
KBSound.py
89 lines (71 loc) · 2.19 KB
/
KBSound.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
# import modules
import threading
import random
from pynput.keyboard import Key, Listener
import wx
import wx.adv
# class untuk menampung keyboard listener.
class KBSoundThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.onPress = None
self.onRelease = None
def run(self):
with Listener(
on_press=self.onPress,
on_release=self.onRelease) as listener:
listener.join()
# class untuk memainkan suara keyboard.
class KBSound:
def __init__(self):
self.running = False
self.randomized = False
self.soundArray = []
self.counter = 0
# saat KBSoundThread mendeteksi keyboard di-press.
def onPress(self, key):
self.playSound()
return self.running
# saat KBSoundThread mendeteksi keyboard di-release.
def onRelease(self, key):
return self.running
# tambahkan WAV ke list.
def addSound(self, path):
self.soundArray.append(wx.adv.Sound(path))
# jalankan service ini.
def startService(self):
if len(self.soundArray) <= 0:
return
self.running = True
thr = KBSoundThread()
thr.onPress = self.onPress
thr.onRelease = self.onRelease
thr.start()
# hentikan service ini.
def stopService(self):
self.running = False
# ambil WAV selanjutnya dari list.
def nextSound(self):
if len(self.soundArray) <= 0:
return
nxs = self.soundArray[self.counter]
self.counter += 1
if self.counter >= len(self.soundArray):
self.counter = 0
return nxs
# ambil random WAV selanjutnya dari list.
# dilakukan jika randomize dicentang.
def nextRandomSound(self):
if len(self.soundArray) <= 0:
return
return random.choice(self.soundArray)
# implementasi mainkan suara WAV.
def playSound(self):
if len(self.soundArray) <= 0:
return
sndToplay = None
if self.randomized == True:
sndToplay = self.nextRandomSound()
else:
sndToplay = self.nextSound()
sndToplay.Play(wx.adv.SOUND_ASYNC)