git_music_visualize_5.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. def show(self):
  2. run = True
  3. maxAvg = 1
  4. lastMapped = 0
  5. tendency = 0
  6. mapped = 0
  7. while run:
  8. try:
  9. #self.clock.tick(20)
  10. # quit on "window close" and Escape
  11. for evt in pygame.event.get():
  12. if evt.type == KEYDOWN:
  13. if evt.key == K_ESCAPE:
  14. run = False
  15. elif evt.key == K_F11:
  16. pygame.display.toggle_fullscreen()
  17. elif evt.type == QUIT:
  18. run = False
  19. if self.SR.newAudio:
  20. # fourier transformation
  21. fft = self.SR.fft()[1]
  22. self.SR.newAudio = False
  23. avg = reduce(lambda x, y: x + y, fft) / len(fft)
  24. # dynamic maximum
  25. if avg > maxAvg:
  26. maxAvg = avg
  27. # translate range into number of frames
  28. mapped = self.translate(avg, 0, maxAvg, self.minNum, self.maxNum)
  29. # do not update if image does not change
  30. if mapped == lastMapped:
  31. continue
  32. elif mapped > lastMapped:
  33. tendency = 1
  34. elif mapped < lastMapped:
  35. tendency = -1
  36. # smooth transition
  37. if lastMapped - mapped > 2:
  38. mapped = lastMapped - ((lastMapped - mapped)/2)
  39. # save last image
  40. lastMapped = mapped
  41. #print "calcd"
  42. else:
  43. mapped = lastMapped + tendency
  44. if not self.minNum <= mapped <= self.maxNum:
  45. continue
  46. #print "tendency"
  47. # display image
  48. #print self.filename % mapped
  49. img = pygame.image.load(self.filename % mapped).convert()
  50. # use whole screen
  51. img = self.scalePercentage(img, 1)
  52. self.screen.blit(img, (0, 0))
  53. pygame.display.update()
  54. # give it a second until the audio buffer is filled up
  55. time.sleep(0.01)
  56. except:
  57. break
  58. self.SR.close()
  59. if __name__ == "__main__":
  60. #v = Visualization("frames/dough/teig-%d.jpg", 0, 66)
  61. v = Visualization("frames/melon/melon-%d.jpg", 9, 108)
  62. v.show()
  63. pygame.quit()