-
Notifications
You must be signed in to change notification settings - Fork 220
/
terrain_audio.py
146 lines (121 loc) · 4.51 KB
/
terrain_audio.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
"""
This creates a 3D mesh with perlin noise to simulate
a terrain. The mesh is animated by shifting the noise
to give a "fly-over" effect.
If you don't have pyOpenGL or opensimplex, then:
- conda install -c anaconda pyopengl
- pip install opensimplex
"""
import numpy as np
from opensimplex import OpenSimplex
import pyqtgraph.opengl as gl
from pyqtgraph.Qt import QtCore, QtGui
import struct
import pyaudio
import sys
class Terrain(object):
def __init__(self):
"""
Initialize the graphics window and mesh surface
"""
# setup the view window
self.app = QtGui.QApplication(sys.argv)
self.window = gl.GLViewWidget()
self.window.setWindowTitle('Terrain')
self.window.setGeometry(0, 110, 1920, 1080)
self.window.setCameraPosition(distance=30, elevation=12)
self.window.show()
# constants and arrays
self.nsteps = 1.3
self.offset = 0
self.ypoints = np.arange(-20, 20 + self.nsteps, self.nsteps)
self.xpoints = np.arange(-20, 20 + self.nsteps, self.nsteps)
self.nfaces = len(self.ypoints)
self.RATE = 44100
self.CHUNK = len(self.xpoints) * len(self.ypoints)
self.p = pyaudio.PyAudio()
self.stream = self.p.open(
format=pyaudio.paInt16,
channels=1,
rate=self.RATE,
input=True,
output=True,
frames_per_buffer=self.CHUNK,
)
# perlin noise object
self.noise = OpenSimplex()
# create the veritices array
verts, faces, colors = self.mesh()
self.mesh1 = gl.GLMeshItem(
faces=faces,
vertexes=verts,
faceColors=colors,
drawEdges=True,
smooth=False,
)
self.mesh1.setGLOptions('additive')
self.window.addItem(self.mesh1)
def mesh(self, offset=0, height=2.5, wf_data=None):
if wf_data is not None:
wf_data = struct.unpack(str(2 * self.CHUNK) + 'B', wf_data)
wf_data = np.array(wf_data, dtype='b')[::2] + 128
wf_data = np.array(wf_data, dtype='int32') - 128
wf_data = wf_data * 0.04
wf_data = wf_data.reshape((len(self.xpoints), len(self.ypoints)))
else:
wf_data = np.array([1] * 1024)
wf_data = wf_data.reshape((len(self.xpoints), len(self.ypoints)))
faces = []
colors = []
verts = np.array([
[
x, y, wf_data[xid][yid] * self.noise.noise2d(x=xid / 5 + offset, y=yid / 5 + offset)
] for xid, x in enumerate(self.xpoints) for yid, y in enumerate(self.ypoints)
], dtype=np.float32)
for yid in range(self.nfaces - 1):
yoff = yid * self.nfaces
for xid in range(self.nfaces - 1):
faces.append([
xid + yoff,
xid + yoff + self.nfaces,
xid + yoff + self.nfaces + 1,
])
faces.append([
xid + yoff,
xid + yoff + 1,
xid + yoff + self.nfaces + 1,
])
colors.append([
xid / self.nfaces, 1 - xid / self.nfaces, yid / self.nfaces, 0.7
])
colors.append([
xid / self.nfaces, 1 - xid / self.nfaces, yid / self.nfaces, 0.8
])
faces = np.array(faces, dtype=np.uint32)
colors = np.array(colors, dtype=np.float32)
return verts, faces, colors
def update(self):
"""
update the mesh and shift the noise each time
"""
wf_data = self.stream.read(self.CHUNK)
verts, faces, colors = self.mesh(offset=self.offset, wf_data=wf_data)
self.mesh1.setMeshData(vertexes=verts, faces=faces, faceColors=colors)
self.offset -= 0.05
def start(self):
"""
get the graphics window open and setup
"""
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
def animation(self, frametime=10):
"""
calls the update method to run in a loop
"""
timer = QtCore.QTimer()
timer.timeout.connect(self.update)
timer.start(frametime)
self.start()
if __name__ == '__main__':
t = Terrain()
t.animation()