Advertisement
alseambusher

happy.py

Mar 16th, 2015
12,385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. import scipy.io.wavfile as wavfile
  2. import numpy as np
  3. import pylab as pl
  4. import time
  5. import os
  6. import sys
  7. import subprocess
  8. from scipy import mean
  9. from random import randint
  10.  
  11. # music wav file
  12. FILE = "karaoke.wav"
  13. rate, data = wavfile.read(FILE)
  14. t_total = len(data[:,0])/rate
  15. display_rate = 1500 #number of frames processed in one iteration
  16. sample_size = 120
  17. max_display = 90
  18. data_length  = len(data) #total number of frames
  19. _min = min([abs(x) for x in data[:,0]]) #max amplitude in the wav
  20. _max = max([abs(x) for x in data[:,0]]) #min amplitude in the wav
  21.  
  22. # IMPORTANT: correction factor. Change this value to match the song with equalizer
  23. correction = 0.645
  24.  
  25. # cake settings
  26. cols = int(subprocess.Popen("tput cols",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).stdout.readlines()[0]) #columns in terminal
  27. display_char = "8"
  28. cake_size = 50
  29.  
  30. # flame control
  31. flame_flutter_rate = 50
  32. FLAMES = [ " . ", ".  ", "  ." ]
  33. current_flame = ""
  34.  
  35. os.system("tput civis") #hide cursor
  36.  
  37. # TODO open file with some player. If you are on mac, uncomment following line
  38. #os.system("open "+FILE)
  39.  
  40. for _f in range(data_length/display_rate):
  41.  
  42.     # fluttering effect to candle flames
  43.     if _f%flame_flutter_rate == 0:
  44.         current_flame = (" "*(cols/2 - cake_size/2))+((" "+FLAMES[randint(0,2)]+" ")*(cake_size/5))
  45.     print current_flame
  46.    
  47.     # candles
  48.     print (" "*(cols/2 - cake_size/2))+("  |  "*(cake_size/5))
  49.     # cake top layer
  50.     print (" "*(cols/2 - cake_size/2))+("-"*cake_size)
  51.  
  52.     bucket = []
  53.     mug = []
  54.     # mug contains the current frame samples (absolute values) of given sample_size
  55.     # average of mugs are put into bucket
  56.     for value in data[:,0][_f*display_rate+1:(_f+1)*display_rate]:
  57.         mug.append(abs(value))
  58.         if len(mug) == sample_size:
  59.             bucket.append(mean(mug))
  60.             mug = []
  61.     bucket = [ (float)((x - _min) * max_display)/(_max - _min) for x in bucket ]
  62.  
  63.     # print the equalizer from the bucket
  64.     for value in bucket:
  65.         print (" "*(cols/2 - cake_size/2))+"| "+("8"*(value%(cake_size-2)))+(" "*(cake_size-value-2))+"|"
  66.  
  67.     # bottom crust of the cake
  68.     print (" "*(cols/2 - cake_size/2))+("-"*cake_size)
  69.  
  70.     # print happy birthday message
  71.     os.system("figlet -c -f small Happy Birthday qwerty!")
  72.  
  73.     # sleep to match with the audio
  74.     """
  75.         NOTE: correction has to be multiplied to sleep time
  76.         This is because of several factors like time taken to wake from sleep, type of terminal used..etc
  77.         CHANGE THE VALUE OF correction TO FIT YOUR NEED
  78.     """
  79.     time.sleep(((float)(display_rate * t_total) / data_length)*correction)
  80.  
  81.     # clear screen
  82.     if _f != data_length/display_rate-1:
  83.         os.system("clear")
  84.  
  85. raw_input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement