This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
""" | ||
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 | ||
from PyQt6 import QtWidgets, QtCore | ||
import pyqtgraph.opengl as gl | ||
import pyqtgraph as pg | ||
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 = QtWidgets.QApplication([]) | ||
pg.mkQApp() | ||
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(5) | ||
|
||
# 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.noise2(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) | ||
|
||
# print(verts) | ||
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'): | ||
QtWidgets.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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Intro | ||
|
||
In this folder I collect lessons learned from my Data Scientis path. | ||
|
||
## File Organization | ||
For now, I will make a file for each month of 2023. | ||
The next year I will evaluate if I should continue with this collection method or how I can re-organizer folders and file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# MAY 2023 | ||
|
||
## Automatic Speech Recognition | ||
|