Skip to content

Instantly share code, notes, and snippets.

@maximeborges
Last active January 22, 2016 17:38
Show Gist options
  • Save maximeborges/ebe7ac19b923070f119c to your computer and use it in GitHub Desktop.
Save maximeborges/ebe7ac19b923070f119c to your computer and use it in GitHub Desktop.
Draw points cloud and save to stereo wav file
import numpy as np
import matplotlib.pyplot as plt
left = list()
right = list()
# Read data from a file
# dataread = np.fromfile("test.wav", dtype='<i2')
# for i in range(22, 198):
# if i%2:
# right.append(dataread[i])
# else:
# left.append(dataread[i])
fig = plt.figure()
ax = fig.add_subplot(111)
axes = plt.gca()
axes.set_xlim([-32767,32767])
axes.set_ylim([-32767,32767])
ax.plot(left, right, color='r', linewidth=3, marker='o')
ax.grid(True)
def onclick(event):
if event.xdata and event.ydata:
left.append(event.xdata)
right.append(event.ydata)
ax.plot(left, right, color='r', linewidth=3, marker='o')
fig.canvas.draw()
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
data = []
for i in range(len(left)):
if left[i] < 0:
left[i] = left[i] + 64000
if right[i] < 0:
right[i] = right[i] + 64000
data.append(chr(int(right[i])%256) + chr(int(int(right[i])/256)))
data.append(chr(int(left[i])%256) + chr(int(int(left[i])/256)))
length = 36+len(data) # header length
header = [0x5249, 0x4646, (length%256*256+length/256), 0x0000, 0x5741, 0x5645, 0x666D, 0x7420,
0x1000, 0x0000, 0x0100, 0x0200, 0x44AC, 0x0000, 0x10B1, 0x0200,
0x0400, 0x1000, 0x6461, 0x7461, ((len(data)%65536)*2%256*256+len(data)*2//256), (len(data)*2//65536*256 + len(data)*2//16777216)]
t=''
for i in header:
t += chr(i//256) + chr(i%256)
t += ''.join(data)
np.ndarray(shape=(len(t)//2,),dtype='<i2',buffer=t).tofile('OUT.wav')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment