Pete Shadbolt 4 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
3 3
 import matplotlib.image as mpimg
4 4
 import wave
5 5
 from array import array
6
-from progressbar import ProgressBar
7
-
8
-def write_column(output_file, pixels, rescale=.1):
9
-    """ Write a single column of sound """
10
-    curve = np.fft.ifft(pixels, len(pixels)*2).real
11
-    curve = np.array((curve-np.average(curve))*rescale, dtype=int)
12
-    data = array("h", curve).tostring()
13
-    output_file.writeframes(data)
14 6
 
15 7
 
16 8
 def make_wav(image_filename):
17 9
     """ Make a WAV file having a spectrogram resembling an image """
10
+    # Load image
18 11
     image = mpimg.imread(image_filename)
19 12
     image = np.sum(image, axis = 2).T[:, ::-1]
20
-    image = image**2
13
+    image = image**3 # ???
14
+    w, h = image.shape
15
+
16
+    # Fourier transform, normalize, remove DC bias
17
+    data = np.fft.irfft(image, h*2, axis=1).reshape((w*h*2))
18
+    data -= np.average(data)
19
+    data *= (2**15.-1)/np.amax(data)
20
+    data = array("h", np.int_(data)).tostring()
21 21
 
22
+    # Write to disk
22 23
     output_file = wave.open(image_filename+".wav", "w")
23 24
     output_file.setparams((1, 2, 44100, 0, "NONE", "not compressed"))
24
-
25
-    pb = ProgressBar().start()
26
-    n = float(len(image))
27
-    for index, column in enumerate(image):
28
-        write_column(output_file, column)
29
-        pb.update(index*100/n)
30
-
25
+    output_file.writeframes(data)
31 26
     output_file.close()
32 27
     print "Wrote %s.wav" % image_filename
33 28