Browse Source

Simplify

master
Pete Shadbolt 5 years ago
parent
commit
507e3b9c1c
1 changed files with 11 additions and 16 deletions
  1. +11
    -16
      spectrogram.py

+ 11
- 16
spectrogram.py View File

@@ -3,31 +3,26 @@ import numpy as np
import matplotlib.image as mpimg
import wave
from array import array
from progressbar import ProgressBar

def write_column(output_file, pixels, rescale=.1):
""" Write a single column of sound """
curve = np.fft.ifft(pixels, len(pixels)*2).real
curve = np.array((curve-np.average(curve))*rescale, dtype=int)
data = array("h", curve).tostring()
output_file.writeframes(data)


def make_wav(image_filename):
""" Make a WAV file having a spectrogram resembling an image """
# Load image
image = mpimg.imread(image_filename)
image = np.sum(image, axis = 2).T[:, ::-1]
image = image**2
image = image**3 # ???
w, h = image.shape

# Fourier transform, normalize, remove DC bias
data = np.fft.irfft(image, h*2, axis=1).reshape((w*h*2))
data -= np.average(data)
data *= (2**15.-1)/np.amax(data)
data = array("h", np.int_(data)).tostring()

# Write to disk
output_file = wave.open(image_filename+".wav", "w")
output_file.setparams((1, 2, 44100, 0, "NONE", "not compressed"))

pb = ProgressBar().start()
n = float(len(image))
for index, column in enumerate(image):
write_column(output_file, column)
pb.update(index*100/n)

output_file.writeframes(data)
output_file.close()
print "Wrote %s.wav" % image_filename



Loading…
Cancel
Save