123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- def show(self):
- run = True
- maxAvg = 1
- lastMapped = 0
- tendency = 0
- mapped = 0
- while run:
- try:
- #self.clock.tick(20)
- # quit on "window close" and Escape
- for evt in pygame.event.get():
- if evt.type == KEYDOWN:
- if evt.key == K_ESCAPE:
- run = False
- elif evt.key == K_F11:
- pygame.display.toggle_fullscreen()
- elif evt.type == QUIT:
- run = False
- if self.SR.newAudio:
- # fourier transformation
- fft = self.SR.fft()[1]
- self.SR.newAudio = False
- avg = reduce(lambda x, y: x + y, fft) / len(fft)
- # dynamic maximum
- if avg > maxAvg:
- maxAvg = avg
- # translate range into number of frames
- mapped = self.translate(avg, 0, maxAvg, self.minNum, self.maxNum)
- # do not update if image does not change
- if mapped == lastMapped:
- continue
- elif mapped > lastMapped:
- tendency = 1
- elif mapped < lastMapped:
- tendency = -1
- # smooth transition
- if lastMapped - mapped > 2:
- mapped = lastMapped - ((lastMapped - mapped)/2)
- # save last image
- lastMapped = mapped
- #print "calcd"
- else:
- mapped = lastMapped + tendency
- if not self.minNum <= mapped <= self.maxNum:
- continue
- #print "tendency"
- # display image
- #print self.filename % mapped
- img = pygame.image.load(self.filename % mapped).convert()
- # use whole screen
- img = self.scalePercentage(img, 1)
- self.screen.blit(img, (0, 0))
- pygame.display.update()
- # give it a second until the audio buffer is filled up
- time.sleep(0.01)
- except:
- break
- self.SR.close()
- if __name__ == "__main__":
- #v = Visualization("frames/dough/teig-%d.jpg", 0, 66)
- v = Visualization("frames/melon/melon-%d.jpg", 9, 108)
- v.show()
- pygame.quit()
|