Skip to content

Instantly share code, notes, and snippets.

@rfmerrill
Last active August 29, 2016 01:21
Show Gist options
  • Save rfmerrill/77cc96dfff93aaa0c38f010b74fa6564 to your computer and use it in GitHub Desktop.
Save rfmerrill/77cc96dfff93aaa0c38f010b74fa6564 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import numpy as np
import cv2
import sys
import scipy.io.wavfile
import scipy.signal
# Needs to be a regular wav file, not wavex
# Also, assuming PCM, 2 channels, 16-bit
fs, data = scipy.io.wavfile.read(sys.argv[1], 'rb')
dimension = 800
print "data shape:", data.shape
if False:
ratio = int(fs / 48000)
new_fs = fs / ratio
print "fs was", fs, "decimating to", new_fs
fs = new_fs
data = scipy.signal.decimate(data, ratio, axis=0)
print "data shape:", data.shape
# create an empty dimension x dimension bgr image
vis = np.zeros((dimension, dimension, 3), np.uint8)
def sample_to_xy(in_sample_pair):
# bias towards the middle of the image
# we use 65600 instead of 65536 so we don't go off the edge.
x = int((32768 - in_sample_pair[0]) * (dimension / 65600.0))
y = int((32768 - in_sample_pair[1]) * (dimension / 65600.0))
return (x, y)
framerate = 20
fourcc = cv2.VideoWriter_fourcc(*'XVID')
vwobj = cv2.VideoWriter(sys.argv[2], fourcc, framerate, (dimension, dimension))
sample_cnt = 0
samples_per_frame = int(fs / framerate)
total_samples = data.shape[0]
print "Starting loop, ", total_samples, " total samples"
print samples_per_frame, "samples per frame"
old_point = (0, 0)
for sample_pair in 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_cnt % 1000) == 0:
print sample_cnt, " samples so far"
if (sample_cnt % samples_per_frame) == 0:
cv2.imshow("foo", vis)
cv2.waitKey(1)
print "Frame out, ", sample_cnt, " samples so far"
vwobj.write(vis)
vis *= 0.5
sample_cnt += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment