-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
108 lines (81 loc) · 2.59 KB
/
main.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
import warnings
import socket
from time import sleep
import math
from gpiozero import Button
from time import time
#! important: make a file called secret in the same directory and write your IP as a string.
#! if you want to run the Pure Data and Python in the same machine, you may also write 'localhost'
#! e.g. IP = "192.168.1.1" or IP = "localhost"
from secret import IP
def addtime(times):
if type(times) != list:
raise(TypeError)
t = time()
if len(times) == 0:
tdiff = 0 # initial seed
else:
tdiff = t - times[-1][0]
return (t, tdiff)
def averagetimes(times):
averagetime = sum([row[1] for row in times])/float(len(times))
bpm = (1.0/(averagetime/60.0))
return (averagetime, bpm)
class PDSocket:
"""PD-PI Socket"""
def __init__(self, sock=None):
if sock is None:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
def connect(self, host, port):
self.sock.connect((host, port))
def close(self):
self.sock.close()
def send(self, msg):
pdmsg = ';'.join([f'{k} {v}' for k, v in msg.items()])
sent = self.sock.sendall(bytes(pdmsg + ';', 'ascii'))
if sent == 0:
raise RuntimeError("socket connection broken")
def receive(self):
msg = str(self.sock.recv(1024), 'ascii')
if msg == b'':
raise RuntimeError("socket connection broken")
return msg[:-2].split(';\n')
def main(host=IP, port=7000):
times = []
def got_pressed():
times.append(addtime(times))
if len(times) > 1:
if times[0][1] == 0 or len(times) > 16:
del times[0]
(averagetime, bpm) = averagetimes(times)
bpmValue = math.floor(bpm)
print(bpm, bpmValue)
socket.send({'tempo': bpmValue})
print(f"> Connecting to '{host}' at port '{port}'")
socket = PDSocket()
socket.connect(host, port)
bpin = 26
button = Button(bpin)
button.when_pressed = got_pressed
# ?inputs
start = 1 # switch
emotion = 3 # mood
dis = 0 # dissonance
dspToggle = 1 # dsp
#! killswitch
#start = 0
#dsp = 0
values = {'start': start, 'emotion': emotion, 'dis': dis, 'dspToggle': dspToggle}
try:
print("init values")
socket.send(values)
while True:
sleep(10)
print("done")
except KeyboardInterrupt:
print("> Closing socket")
socket.close()
if __name__ == '__main__':
main()