-
-
Save steveherrin/35ccf1df8b8f755f234ed903dc63c81c to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import numpy as np | |
import cv2 | |
import sys | |
import scipy.io.wavfile | |
import scipy.signal | |
MIDDLE_RANGE = 2**15 | |
ARBITRARY_MAX_RANGE = 65600 # we use 65600 instead of 65536 so we don't go off the edge | |
MAGIC_NUMBER = 48000 | |
FRAME_RATE = 20 | |
DIMENSION = 800 | |
def sample_to_xy(x0, y0): | |
""" | |
bias towards the middle of the image | |
""" | |
x1 = int((MIDDLE_RANGE - x0) * (DIMENSION / ARBITRARY_MAX_RANGE)) | |
y1 = int((MIDDLE_RANGE - x1) * (DIMENSION / ARBITRARY_MAX_RANGE)) | |
return x1, y1 | |
if __name__ == '__main__': | |
_, IN_FILE, OUT_FILE = sys.argv | |
# Needs to be a regular wav file, not wavex | |
# Also, assuming PCM, 2 channels, 16-bit | |
fs, data = scipy.io.wavfile.read(IN_FILE, 'rb') | |
print('data shape: {}'.format(data.shape)) | |
if False: | |
ratio = int(fs / MAGIC_NUMBER) | |
new_fs = fs / ratio | |
print('fs was {} decimating to {}'.format(fs, new_fs)) | |
fs = new_fs | |
data = scipy.signal.decimate(data, ratio, axis=0) | |
print('data shape: {}'.format(data.shape)) | |
# create an empty dimension x dimension bgr image | |
vis = np.zeros((DIMENSION, DIMENSION, 3), np.uint8) | |
fourcc = cv2.VideoWriter_fourcc(*'XVID') | |
vwobj = cv2.VideoWriter(OUT_FILE, fourcc, FRAME_RATE, (DIMENSION, DIMENSION)) | |
samples_per_frame = int(fs / FRAME_RATE) | |
total_samples = data.shape[0] | |
print('Starting loop {} total samples'.format(total_samples)) | |
print('{} samples per frame'.format(samples_per_frame)) | |
old_point = (0, 0) | |
for sample_count, sample_pair in enumerate(data): | |
point = sample_to_xy(*sample_pair) | |
# Draw a green line segment from old_point to point | |
cv2.line(vis, old_point, point, (0, 255, 0)) | |
old_point = point | |
if (sample_count % 1000) == 0: | |
print('{} samples so far'.format(sample_count)) | |
if (sample_count % samples_per_frame) == 0: | |
cv2.imshow('foo', vis) | |
cv2.waitKey(1) | |
print('Frame out {} samples so far'.format(sample_count)) | |
vwobj.write(vis) | |
vis *= 0.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment