-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_raw.py
More file actions
242 lines (182 loc) · 7.45 KB
/
client_raw.py
File metadata and controls
242 lines (182 loc) · 7.45 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import pickle
import socket, traceback
import csv
import pyaudio
import wave
import struct
import math
import os
import numpy as np
import pygame
from pygame.locals import * #This enhances all keybord inputs
import threading
HEADERSIZE = 10
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 1243))
while True:
full_msg = b''
new_msg = True
while True:
msg = s.recv(16)
if new_msg:
print("new msg len:",msg[:HEADERSIZE])
msglen = int(msg[:HEADERSIZE])
new_msg = False
print(f"full message length: {msglen}")
full_msg += msg
print(len(full_msg))
if len(full_msg)-HEADERSIZE == msglen:
print("full msg recvd")
print(full_msg[HEADERSIZE:])
print(pickle.loads(full_msg[HEADERSIZE:]))
new_msg = True
full_msg = b""
# import socket
# HEADERSIZE = 10
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# s.connect((socket.gethostname(), 1241))
# while True:
# full_msg = ''
# new_msg = True
# while True:
# msg = s.recv(16)
# if new_msg:
# print("new msg len:",msg[:HEADERSIZE])
# msglen = int(msg[:HEADERSIZE])
# new_msg = False
# print(f"full message length: {msglen}")
# full_msg += msg.decode("utf-8")
# print(len(full_msg))
# if len(full_msg)-HEADERSIZE == msglen:
# print("full msg recvd")
# print(full_msg[HEADERSIZE:])
# new_msg = True
# # Streaming Data via UDP setup
# host = ''
# port = 5555
# s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# s.bind((host, port))
#Keyboard setup through Pygame
pygame.init() #Initializes queue of keyboard inputs
pygame.display.set_mode((400,400)) #A window must be created in order for pygame to work
condition1True = 0 #List of Bool variables based on keyboard inputs
condition2True = 0
condition3True = 0
condition4True = 0
condition5True = 0
# Go through wave file
print ('* Playing...')
for i in range(0, num_blocks): # Until end of Song
# Get block of samples from wave file
input_string = wf.readframes(BLOCKSIZE) # BLOCKSIZE = number of frames read
# Convert binary string to tuple of numbers
input_tuple = struct.unpack('h' * BLOCKSIZE, input_string)
# (h: two bytes per sample (WIDTH = 2))
#************************ READ DATA FROM SENSORS ****************************
try:
message, address = s.recvfrom(8192)
message = message[:-1] # -1 index gives the last element which is a # based on the app we are using
#Read data element-wise from sensors
reader = csv.reader(message.split('\n'), delimiter=',')
for lineData in reader:
xGyro = float(lineData[0])
yGyro = float(lineData[1])
zGyro = float(lineData[2])
xG = float(lineData[3])
yG = float(lineData[4])
zG = float(lineData[5])
xLinAcc = float(lineData[6])
yLinAcc = float(lineData[7])
zLinAcc = float(lineData[8])
xRot = float(lineData[9])
yRot = float(lineData[10])
zRot = float(lineData[11])
except (KeyboardInterrupt, SystemExit):# set KeyboardInterrupt, SystemExit in "try clause" if needed
raise
except:
traceback.print_exc()
#print zGyro, zG
#************************** END OF SENSOR DATA ****************************
#************************** READ KEYBOARD DATA ****************************
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a: condition1True = 1
if event.key == pygame.K_v: condition2True = 1
if event.key == pygame.K_e: condition3True = 1
if event.key == pygame.K_f: condition4True = 1
if event.key == pygame.K_r: condition5True = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_a: condition1True = 0
if event.key == pygame.K_v: condition2True = 0
if event.key == pygame.K_e: condition3True = 0
if event.key == pygame.K_f: condition4True = 0
if event.key == pygame.K_r: condition5True = 0
#************************** END OF KEYBOARD DATA ****************************
#************************ APPLY EFFECTS & CONDITIONS ************************
#Initialize output block to ADD effects
output_block1 = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero
output_block2 = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero
output_block3 = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero
output_block4 = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero
output_block5 = [0.0 for i in range(BLOCKSIZE)] # Initialize to zero
noCondition = 1 #Since Multiples conditions might happen keep track if no condition occurs
countConditions = 0
#Condition 1 --for--> AMPLITUDE MODULATION
if (zG > 7 or condition1True):
duckFreq = 400 # AM frequency
output_block1 = func_duck(input_tuple, RATE, duckFreq)
noCondition = 0
countConditions += 1
#Condition 2 --for--> VIBRATO
if (zGyro > 0.5 or condition2True):
vibratoFreq = 10
vibratoAmplitude = 0.6 # Keep < 1
output_block2 = func_vibrato(input_tuple, RATE, vibratoFreq, vibratoAmplitude)
noCondition = 0
countConditions += 1
#Condition 3 --for--> ECHO
if ((abs(xRot)+abs(zRot)) > 2 or condition3True):
Gdp = 1
Gff = 2
echo = int(buffer_MAX/2) #1/2 of the distance of its buffer
output_block3 = func_echo(input_tuple, Gdp, Gff, echo)
noCondition = 0
countConditions += 1
#Condition 4 --for--> FEEDBACK
if (math.sqrt(xLinAcc**2 + zLinAcc**2) > 3 or condition4True):
Gdp = 1
Gff = 2
Gfb = 0.4
output_block4 = func_feedback(input_tuple, Gdp, Gff, Gfb)
noCondition = 0
countConditions += 1
#Conditon 5 --for--> ROBOTIZATION
if(yG > 7 or condition5True):
output_block5 = robotization(input_tuple)
noCondition = 0
countConditions += 1
#NO Condition
if noCondition:
output_block = input_tuple
else:
output_block = [output_block1, output_block2, output_block3, output_block4, output_block5]
output_block = [sum(x) for x in zip(*output_block)]
output_block = [clip16(x) for x in output_block]
#*********************** END OF EFFECTS & CONDITIONS **********************
# Convert values to binary string
output_string = struct.pack('h' * BLOCKSIZE, *output_block)
# Write binary string to audio output stream
stream.write(output_string)
print('* Done *')
stream.stop_stream()
stream.close()
p.terminate()
wf.close()
# if keys[pygame.K_LEFT]:
# x -= vel
# win.fill((0,0,0)) # Fills the screen with black
#pygame.draw.rect(win, (255,0,0), (x, y, width, height))
#pygame.display.update()