-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog.py
147 lines (125 loc) · 3.98 KB
/
prog.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import pystray, array, math, pyaudio, threading
from PIL import Image, ImageDraw
from pystray import Menu as menu, MenuItem as item
from pyaudio import paFloat32 as paf32
class GenerateSinewave(threading.Thread):
def __init__(self, data):
threading.Thread.__init__(self)
self.p = pyaudio.PyAudio()
self.data = data
def run(self):
if (not self.data): return
stream = self.p.open(format=paf32,
channels=1,
rate=44100,
output=True)
while(output_enabled):
stream.write(self.data)
stream.close()
self.p.terminate()
def set_data(self, data):
self.data = data
def create_sinewave(frequency, volume, fs=44100, duration=0.1):
# generate samples, note conversion to float32 array
num_samples = int(fs * duration)
samples = [volume * math.sin(2 * math.pi * k * frequency / fs) for k in range(0, num_samples)]
output_bytes = array.array('f', samples).tobytes()
return output_bytes
output_enabled = False
state_freq = 25
state_vol = 1
def on_clicked(icon, item):
global output_enabled
output_enabled = not item.checked
icon.icon = create_image(64, 64, "green") if output_enabled else create_image(64, 64, "red")
output_bytes = create_sinewave(state_freq*1000, state_vol*0.01)
GenerateSinewave(output_bytes).start()
def create_image(width, height, color1):
# Generate an image and draw a pattern
image = Image.new('RGB', (width, height), color1)
dc = ImageDraw.Draw(image)
return image
def destroy_all():
global output_enabled
output_enabled = False
icon.stop()
def main():
def set_state_freq(v):
def inner(icon, item):
global state_freq
state_freq = v
return inner
def get_state_freq(v):
def inner(item):
return state_freq == v
return inner
def set_state_vol(v):
def inner(icon, item):
global state_vol
state_vol = v
return inner
def get_state_vol(v):
def inner(item):
return state_vol == v
return inner
global icon
icon = pystray.Icon('test', create_image(64, 64, 'red'), menu=menu(
item(
'Enable keepalive output',
on_clicked,
checked=lambda item: output_enabled),
item(
'Frequency',
menu(
item(
'20 kHz',
set_state_freq(20),
checked=get_state_freq(20),
radio=True),
item(
'22 kHz',
set_state_freq(22),
checked=get_state_freq(22),
radio=True),
item(
'25 kHz',
set_state_freq(25),
checked=get_state_freq(25),
radio=True),
item(
'1 kHz (test)',
set_state_freq(1),
checked=get_state_freq(1),
radio=True),
)),
item(
'Volume',
menu(
item(
'0.01%',
set_state_vol(1),
checked=get_state_vol(1),
radio=True),
item(
'0.1%',
set_state_vol(10),
checked=get_state_vol(10),
radio=True),
item(
'0.5%',
set_state_vol(50),
checked=get_state_vol(50),
radio=True),
item(
'1%',
set_state_vol(100),
checked=get_state_vol(100),
radio=True),
)),
item(
'Quit',
action=destroy_all)
))
icon.run()
if __name__ == "__main__":
main()