forked from jeffbryner/pygdrumcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygdrumcloud.py
283 lines (227 loc) · 8.18 KB
/
pygdrumcloud.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/python
# originally pygame '3D Sphere Collisions - Ian Mallett - 2008'
# and bits of kinect python examples
# Now...way different
# jeff bryner 01/2011
from OpenGL.GL import *
from OpenGL.GLU import *
import os, sys, math, random
import pygame
from pygame.locals import *
if sys.platform == 'win32' or sys.platform == 'win64':
#loser..get linux
os.environ['SDL_VIDEO_CENTERED'] = '1'
import GL, ModuleGetInput
import freenect
import numpy as np
import time
from threading import Thread
import thread
import wave
import alsaaudio
clock = pygame.time.Clock()
try:
TEXTURE_TARGET = GL_TEXTURE_RECTANGLE
except:
TEXTURE_TARGET = GL_TEXTURE_RECTANGLE_ARB
pygame.init()
Screen = (640,480)
pygame.display.set_caption('pygame kinect drummachine pointcloud')
pygame.display.set_mode(Screen,OPENGL|DOUBLEBUF|RESIZABLE)
GL.resize(Screen)
GL.init()
rgbtex = glGenTextures(1)
glBindTexture(TEXTURE_TARGET, rgbtex)
glTexImage2D(TEXTURE_TARGET,0,GL_RGB,640,480,0,GL_RGB,GL_UNSIGNED_BYTE,None)
#default states
CameraPos = [0.3,-.40,.80]
CameraRotate = [-15,-15]
ViewMode = 'Fill'
CloudThreshold=1.5 #how much background to show
CloudSample=13 #downsample size, resolution of the clould, higher=less resolution
CloudPointSize=2.5 #point size of points in the cloud
KeyPress = [False]
Blocks = []
def play(filename):
#sys.stdout.write('%d channels, %d sampling rate\n' % (f.getnchannels(),f.getframerate()))
f = wave.open(filename, 'rb')
device = alsaaudio.PCM(card='default', mode=alsaaudio.PCM_NONBLOCK)
# Set attributes
device.setchannels(f.getnchannels())
device.setrate(f.getframerate())
# 8bit is unsigned in wav files
samplewidth=f.getsampwidth()
if samplewidth == 1:
device.setformat(alsaaudio.PCM_FORMAT_U8)
# Otherwise we assume signed data, little endian
elif samplewidth == 2:
device.setformat(alsaaudio.PCM_FORMAT_S16_LE)
elif samplewidth == 3:
device.setformat(alsaaudio.PCM_FORMAT_S24_LE)
elif samplewidth == 4:
device.setformat(alsaaudio.PCM_FORMAT_S32_LE)
else:
raise ValueError('Unsupported format')
#device.setperiodsize(320)
#data = f.readframes(320)
device.setperiodsize(80)
data = f.readframes(80)
while data:
# Read data from stdin
device.write(data)
data = f.readframes(1024)
f.close()
class Block:
def __init__(self,x,y,z,name=None,soundfile=None,quantize=.10):
self.radius = 1.20
#this coordinate system don't match opengl's..it's reverse..so fix the z.
self.x=x
self.y=y
self.z=-z
#quantize the sound so it doesn't fire multiple times per collition
self.lastfire=time.time()
self.quantize=quantize
if name:
self.name=name
else:
self.name="Block" + str(len(Blocks))
if soundfile:
self.soundfile=soundfile
else:
self.soundfile=None
#uncomment for random colors
#self.color=[random.random()-0.5,random.random()-0.5,random.random()-0.5]
self.color=[1,1,1]
self.rotate = [0.0,0.0]
def CollisionDetect(xyz):
for xyzelement in xyz:
for Block in Blocks:
Distance = math.sqrt( ((xyzelement[0]-Block.x)**2) + ((xyzelement[1]-Block.y)**2) + ((xyzelement[2]-Block.z)**2) )
if Distance <= (.065):
#sys.stdout.write("point collide with %s: %f %s %f\n" % (Block.name,Distance,xyzelement,Block.lastfire))
if Block.soundfile and abs(time.time() - Block.lastfire)>Block.quantize :
thread.start_new_thread(play, (Block.soundfile,))
Block.lastfire=time.time()
def project(depth, u, v):
#don't ask me what this equation does..wasn't documented where I found it
#Z = 1.0 / (-0.0030711*depth.flatten() + 3.3309495)
#Z = 2.0/ (-0.0030711*depth.flatten() + 3.3309495)
#overall size/coordinate scale
# #scale of the depth lower=thinner
# #fish-eye scale of close objects vs far lower=close object bigger than far.
Z = 1.0/ (-0.0030711*depth.flatten() + 3.3309495)
#depth
X = (u.flatten()-320) * Z / 620.0
Y = (v.flatten()-320) * Z / 620.0
U = u.flatten()
V = v.flatten()
xyz = np.vstack((X,-Y,-Z)).transpose()
uv = np.vstack((U,V)).transpose()
#determine how much background/foreground to show
#trim off any noise showing up as -z
xyz = xyz[Z>0,:]
uv = uv[Z>0,:]
Z=Z[Z>0]
#how far in the distance to show?
try:
xyz = xyz[Z<CloudThreshold, :]
uv = uv[Z<CloudThreshold, :]
except:
pass
#check for collisions between points and drum blocks
CollisionDetect(xyz)
return xyz, uv
def DrawPointCloud():
global CloudSample,CloudPointSize
#Camera Position/Orient
glTranslatef(CameraPos[0]/2,CameraPos[1]/2,-CameraPos[2]/2)
glRotatef(-CameraRotate[1],1,0,0)
glRotatef(-CameraRotate[0],0,1,0)
DrawBlocks()
#get the kinecty goodness
depth,tstamp=freenect.sync_get_depth()
rgb, tstamp=freenect.sync_get_rgb()
X,Y = np.meshgrid(range(640),range(480))
#downsample size
dd=CloudSample
pointsize=CloudPointSize
q=depth
#to the cloud!
projpts = project(q[::dd,::dd],X[::dd,::dd],Y[::dd,::dd])
xyz, uv = projpts
#use the rgb for texture
if not rgb is None:
rgb_ = (rgb.astype(np.float32) * 4 + 70).clip(200,255).astype(np.uint8)
glBindTexture(TEXTURE_TARGET, rgbtex)
glTexSubImage2D(TEXTURE_TARGET, 0, 0, 0, 640, 480, GL_RGB, GL_UNSIGNED_BYTE, rgb_);
# Draw the points from the kinect
glPointSize(pointsize)
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glVertexPointerf(xyz)
glTexCoordPointerf(uv)
glEnable(TEXTURE_TARGET)
glColor3f(1,1,1)
glDrawElementsui(GL_POINTS, np.array(range(len(xyz))))
#sys.stdout.write("xyz length: " + str(len(xyz)) + '\n')
#sys.stdout.write("uv length:" + str(len(uv)) +'\n')
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
glDisable(TEXTURE_TARGET)
def DrawBlocks():
glPointSize(15.0)
#red block for 0/0/0 to orient
glColor3f(1.0,0.0,0)
glBegin(GL_POINTS)
glVertex3f(0,0,0)
for Block in Blocks:
#pick a color
#glColor3f(random.random()-0.5,random.random()-0.5,random.random()-0.5)
if abs(time.time() - Block.lastfire)>Block.quantize:
glColor3f(Block.color[0],Block.color[1], Block.color[2])
else:
#red=hit
glColor3f(1.0,0,0)
#draw it
glVertex3f(Block.x,Block.y,Block.z)
#end drawing of points
glEnd()
def Draw():
#Clear
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
DrawPointCloud()
#show yourself.
pygame.display.flip()
def GetInput():
global CameraPos, CameraRotate, KeyPress, ViewMode, CloudThreshold,CloudPointSize,CloudSample
CameraPos, CameraRotate, KeyPress, ViewMode, CloudThreshold, CloudSample, CloudPointSize = ModuleGetInput.main(CameraPos, CameraRotate, KeyPress, ViewMode, CloudThreshold, CloudSample, CloudPointSize)
def main():
while True:
GetInput()
Draw()
#need it slower?
#clock.tick(200)
if __name__ == '__main__':
#make drum block placeholders
#put 'em where you want
# opengl coords: +y=up -y=down
# +x=right -x=left
# +z=back -z=forward
#
# (0,1,0)y
# | /(0,0,1)z
# | /
# |/___(1,0,0)
# (0,0,0)x
#Blocks.append(Block(0,-.05,-.55))
#Blocks.append(Block(-.30,-.05,-.55))
#Blocks.append(Block(1,0,1))
#kick='wavs/kickdrum-low.wav'
kick='wavs/kick_01.wav'
hat='wavs/hihat-thin.wav'
snare='wavs/snare-thinlong.wav'
Blocks.append(Block( .35, 0, 1.10,"snare",snare,.25)) #to my left (facing kinect), behind my chair
Blocks.append(Block( 0, 0, 1.10,"kick",kick,.25)) #center
Blocks.append(Block(-.25, 0, 1.10,"hat",hat,.25)) #to my right
main()