-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhoneConnect.py
61 lines (45 loc) · 1.52 KB
/
PhoneConnect.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
import time
from osc import *
class PhoneConnect:
# normal variable values for phone in upright position
NORMAL_XZ = 65
NORMAL_Y = 35
NORMAL_PITCH = 80
SHAKETRIGGER = 7 # the lower the trigger, the more sensitive the program
def __init__(self):
oscIn = OscIn( 57110 ) # receive messages from OSC clients on port 57110
self.xAccel = None
self.yAccel = None
self.zAccel = None
self.pitch = None
self.roll = None
self.yaw = None
self.beatTimes=[]
oscIn.onInput("/.*", self.update)
def update(self,message):
OSCaddress = message.getAddress()
args = message.getArguments()
if OSCaddress == "/accelerometer":
self.xAccel,self.yAccel,self.zAccel = args
if OSCaddress == "/gyro":
self.pitch,self.roll,self.yaw = args[3:]
def getBeat(self):
"""Reads the phone's incoming accelerometer and gyroscope data.
Detects beat by waiting for phone accelerometer to shake while still being
in a relatively upright position (bc tilting gives false accel reads). Averages
interval between first 4 beats to get beat"""
prevTime=0
beatTimes = []
while len(beatTimes)<4:
if ( (self.yAccel - self.NORMAL_Y > self.SHAKETRIGGER) and (self.pitch > 95) ):
currentTime = time.time()
interval = currentTime-prevTime
if interval > .65:
prevTime = currentTime
beatTimes.append(interval)
print "Beat captured in", 3-(len(beatTimes)-1)
total=0
for i in range(1,len(beatTimes)):
total = total+beatTimes[i]
averageBeat = total/(float(len(beatTimes)-1))*1000
return averageBeat