From 507e3b9c1cfa86c5292d01c892ecdd4c59572d16 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Thu, 15 Jan 2015 14:35:59 +0000 Subject: [PATCH] Simplify --- spectrogram.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/spectrogram.py b/spectrogram.py index 3e11427..cc42382 100644 --- a/spectrogram.py +++ b/spectrogram.py @@ -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