Example #1
0
def signal_test (small_exponent, large_exponent, signal, filename):
  fft_size = 1 << ((small_exponent + large_exponent) / 2)
  length_windows = fft_size / 2
  length_samples = fft_size * length_windows

  print "creating signal"
  input = K.Complexes(length_samples)

  for i in range(length_samples):
    t = (0.5 + i) / length_samples
    input[i] = signal(t)

  print "transforming signal"
  s = K.Multigram(small_exponent, large_exponent)
  
  length_frames = length_samples / s.size_in
  input = input.reshape((length_frames, s.size_in))
  output = K.Reals(length_frames, s.size_out)

  for i in range(length_frames):
    s.transform_fwd(input[i,:], output[i,:])

  print "writing multigram to %s" % filename

  output = util.energy_to_loudness(output)
  formats.write_image(output, filename)
Example #2
0
def show_signal (size_exponent, factor_exponent, signal, filename):
  size   = 1 << size_exponent
  factor = 1 << factor_exponent
  length_windows = size / 2
  length_samples = size * length_windows

  print "creating signal"
  input = K.transforms.Complexes(length_samples)

  for i in range(length_samples):
    t = (0.5 + i) / length_samples
    input[i] = signal(t)
    #print signal(t), #DEBUG

  print "transforming signal"
  s = K.Supergram(size_exponent, factor_exponent)

  length_frames = length_samples / s.small_size
  input = input.reshape((length_frames, s.small_size))
  output = K.transforms.Reals(length_frames, s.super_size)

  for i in range(length_frames):
    s.transform_fwd(input[i,:], output[i,:])

  print "writing supergram to %s" % filename

  #XXX this fails due to NaNs and Infs in output XXX
  #output[~isfinite(output)] = 0 #DEBUG

  output = K.util.energy_to_loudness(output, 0.1)
  formats.write_image(output, filename).show()
Example #3
0
def show_signal(bank_size, block_size, signal):

    size_in = block_size
    size_out = bank_size

    num_blocks = bank_size * 16 / 9  # arbitrary
    duration = num_blocks * block_size

    min_freq = 0.25 / block_size
    max_freq = 0.25
    pitchgram = K.Pitchgram(size_in, size_out, min_freq, max_freq)

    print "creating signal"
    t = array(range(duration)) / block_size
    sound = K.transforms.Complexes(duration)
    sound[:] = signal(t)
    sound = sound.reshape((num_blocks, block_size))

    print "transforming signal"
    image = K.transforms.Reals(num_blocks, size_out)
    for i in range(num_blocks):
        pitchgram.transform(sound[i, :], image[i, :])

    print "writing pitchgram to pitchgram.png"
    # XXX this fails due to NaNs and Infs in image XXX
    # image[~isfinite(image)] = 0 #DEBUG
    image = K.util.energy_to_loudness(image, 0.1)
    formats.write_image(image, "pitchgram.png").show()
Example #4
0
def file_test (small_exponent = 9, large_exponent = 12):
  "multigram"
  s = K.Multigram(small_exponent, large_exponent)
  size_in = s.size_in
  large_size = s.size_out
  length = s.size_out / 2#arbitrary

  print "reading sound file"
  sound = formats.read_wav('test.wav', size_in*length, size_in)
  image = K.Reals(length, large_size)

  print "transforming data"
  for i in range(length):
    s.transform_fwd(sound[i,:], image[i,:])
  del sound

  print "saving image"
  image = util.energy_to_loudness(image + 1e-5)
  formats.write_image(image, 'test.png')
Example #5
0
def show_file(bank_size=512, block_size=K.DEFAULT_FRAMES_PER_BUFFER):
    "Shows pitchgram of an audio file"

    size_in = block_size
    size_out = bank_size
    length = size_out * 16 / 9  # arbitrary

    pitchgram = K.Pitchgram(size_in, size_out)

    print "reading sound file"
    sound = formats.read_wav("test.wav", size_in * length, size_in)
    image = K.transforms.Reals(length, size_out)

    print "transforming data"
    for i in range(length):
        pitchgram.transform(sound[i, :], image[i, :])
    # del sound

    print "saving image"
    image = K.util.energy_to_loudness(image + 1e-5)
    formats.write_image(image, "test.png").show()
Example #6
0
def show_file (size_exponent = 10, time_exponent = 2, freq_exponent = 3):
  'Shows supergram of an audio file'

  s = K.Supergram(size_exponent, time_exponent, freq_exponent)
  size = s.size
  small_size = s.small_size
  large_size = s.super_size
  length = s.super_size #arbitrary

  print "reading sound file"
  sound = formats.read_wav('test.wav', small_size*length, small_size)
  image = K.transforms.Reals(length, large_size)

  print "transforming data"
  for i in range(length):
    s.transform_fwd(sound[i,:], image[i,:])
  #del sound

  print "saving image"
  image = K.util.energy_to_loudness(image + 1e-5)
  formats.write_image(image, 'test.png').show()
Example #7
0
def signal_test (exponent, signal, filename):
  width = 1 << exponent
  size = width * width

  print "creating signal"
  input = K.Complexes(size)

  for i in range(size):
    t = (i + 0.5) / size
    input[i] = signal(t)

  input = input.reshape((width,width))

  print "transforming signal"
  output = K.Reals(width,width/2)

  s = K.Spectrogram(exponent)
  for i in range(width):
    s.transform_fwd(input[i,:], output[i,:])

  print "writing spactrogram to %s" % filename
  output = util.energy_to_loudness(output)
  formats.write_image(output, filename)
Example #8
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 #9
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")