git_music_visualize.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import sys, numpy, time
  4. from recorder import *
  5. import pygame, pygame.mouse, logging, os, subprocess, sys
  6. from pygame.locals import *
  7. class Visualization:
  8. def __init__(self, filename, minNum, maxNum):
  9. "Ininitializes a new pygame screen using the framebuffer"
  10. self.w = 800
  11. self.h = 600
  12. self.filename = filename
  13. self.minNum = minNum
  14. self.maxNum = maxNum
  15. self.initPygame()
  16. self.initRecorder()
  17. def initPygame(self):
  18. pygame.init()
  19. self.screen = pygame.display.set_mode((self.w, self.h))
  20. pygame.mouse.set_visible(False)
  21. self.clock = pygame.time.Clock()
  22. def initRecorder(self):
  23. self.SR = SwhRecorder()
  24. self.SR.setup()
  25. self.SR.continuousStart()
  26. def translate(self, value, leftMin, leftMax, rightMin, rightMax):
  27. # Figure out how 'wide' each range is
  28. leftSpan = leftMax - leftMin
  29. rightSpan = rightMax - rightMin
  30. # Convert the left range into a 0-1 range (float)
  31. valueScaled = float(value - leftMin) / float(leftSpan)
  32. # Convert the 0-1 range into a value in the right range.
  33. return rightMin + (valueScaled * rightSpan)
  34. def scalePercentage(self, surface, perc):
  35. screenRect = self.screen.get_rect()
  36. surface = pygame.transform.scale(surface, (int(perc*screenRect.width),
  37. int(perc*screenRect.height)))
  38. return surface
  39. def show(self):
  40. run = True
  41. maxAvg = 1
  42. lastMapped = 0
  43. tendency = 0
  44. mapped = 0
  45. while run:
  46. try:
  47. #self.clock.tick(20)
  48. # quit on "window close" and Escape
  49. for evt in pygame.event.get():
  50. if evt.type == KEYDOWN:
  51. if evt.key == K_ESCAPE:
  52. run = False
  53. elif evt.key == K_F11:
  54. pygame.display.toggle_fullscreen()
  55. elif evt.type == QUIT:
  56. run = False
  57. if self.SR.newAudio:
  58. # fourier transformation
  59. fft = self.SR.fft()[1]
  60. self.SR.newAudio = False
  61. avg = reduce(lambda x, y: x + y, fft) / len(fft)
  62. # dynamic maximum
  63. if avg > maxAvg:
  64. maxAvg = avg
  65. # translate range into number of frames
  66. mapped = self.translate(avg, 0, maxAvg, self.minNum, self.maxNum)
  67. # do not update if image does not change
  68. if mapped == lastMapped:
  69. continue
  70. elif mapped > lastMapped:
  71. tendency = 1
  72. elif mapped < lastMapped:
  73. tendency = -1
  74. # smooth transition
  75. if lastMapped - mapped > 2:
  76. mapped = lastMapped - ((lastMapped - mapped)/2)
  77. # save last image
  78. lastMapped = mapped
  79. #print "calcd"
  80. else:
  81. mapped = lastMapped + tendency
  82. if not self.minNum <= mapped <= self.maxNum:
  83. continue
  84. #print "tendency"
  85. # display image
  86. #print self.filename % mapped
  87. img = pygame.image.load(self.filename % mapped).convert()
  88. # use whole screen
  89. img = self.scalePercentage(img, 1)
  90. self.screen.blit(img, (0, 0))
  91. pygame.display.update()
  92. # give it a second until the audio buffer is filled up
  93. time.sleep(0.01)
  94. except:
  95. break
  96. self.SR.close()
  97. if __name__ == "__main__":
  98. #v = Visualization("frames/dough/teig-%d.jpg", 0, 66)
  99. v = Visualization("frames/melon/melon-%d.jpg", 9, 108)
  100. v.show()
  101. pygame.quit()