Example #1
0
def read_spectrogram (exponent = 10, wavname = 'test.wav'):
  """
  read a sound file, say a wav file;
  compute a spectrogram or reassigned spectrogram;
  write image to png file
  """

  width = 1 << exponent
  height = width
  size = height * width

  print "reading sound file"
  sound = formats.read_wav(wavname, size, width)
  image = K.Reals(height, width/2)

  print "transforming data"
  s = K.Spectrogram(exponent)
  for i in range(height):
    s.transform_fwd(sound[i,:],image[i,:])

  print "saving image"
  image = formats.energy_to_loudness(image)
  formats.write_image(image, 'test.png')
Example #2
0
def record_spectrogram(exponent=10):

    width = 1 << exponent
    height = width
    size = height * width

    sound = K.Complexes(width)
    image = K.Reals(height, width / 2)

    s = K.Spectrogram(exponent)
    a = K.Audio(width)

    time = size * a.rate / 60.0
    print "recording & transforming sound for %g seconds..." % time
    a.start()
    for i in range(height):
        a.read(sound)
        s.transform_fwd(sound, image[i, :])
        a.write(sound)  # HACK
    a.stop()

    print "saving image"
    image = formats.energy_to_loudness(image)
    formats.write_image(image, "test.png")