renix1

Python 3.x with beep

Dec 26th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. import winsound
  4. import sys
  5. import time
  6.  
  7. notes = {'A' : 440, 'B': 494, 'C' : 262, 'D' : 294, 'E' : 330, 'F' : 370, 'G' : 392}
  8.  
  9. def imperial_march():
  10.     """ TOCA MARCHA IMPERIAL DO STAR WARS """
  11.     print("Tocando Imperial March")
  12.     while 1:
  13.         frequencias = [440, 440, 440, 349, 523, 440, 349, 523, 440, 659, 659, 659, 698, 523, 415, 349, 523, 440]
  14.         duracoes = [500, 500, 500, 350, 150, 500, 350, 150, 1000, 500, 500, 500, 350, 150, 500, 350, 150, 1000]
  15.         i = 0
  16.         while i < len(frequencias):
  17.             sys.stdout.write('-')
  18.             sys.stdout.flush()
  19.             winsound.Beep(frequencias[i], duracoes[i])
  20.             i += 1
  21.  
  22. def happy_birthday():
  23.     global notes
  24.     """ TOCA FELIZ ANIVERSARIO """
  25.     print("Tocando Happy Birthday")
  26.     while 1:
  27.         frequencias = [notes['D'], notes['D'], notes['E'], notes['D'], notes['G'], notes['F']\
  28.                        ,notes['D'], notes['D'], notes['E'], notes['D'], notes['A'], notes['G']\
  29.                        ,notes['G'], notes['D'], notes['D'], notes['D'], notes['B'], notes['G']\
  30.                        ,notes['F'], notes['E'], notes['C'], notes['C'], notes['B'], notes['G']\
  31.                        ,notes['A'], notes['G']]
  32.         i = 0
  33.         while i < len(frequencias):
  34.             sys.stdout.write('-')
  35.             sys.stdout.flush()
  36.             winsound.Beep(frequencias[i], int(frequencias[i]//.32))
  37.             i += 1
  38.  
  39. def alphabet():
  40.     global notes
  41.     print("Tocando Alphabet Song")
  42.     while 1:
  43.         frequencias = [notes['C'], notes['D'], notes['E'], notes['F'], notes['G']\
  44.                         ,notes['A'], notes['B'], notes['C'], notes['D']\
  45.                        ,notes['E'], notes['F'], notes['G'], notes['A'], notes['B']]
  46.         i = 0
  47.         while i < len(frequencias):
  48.             sys.stdout.write('-')
  49.             sys.stdout.flush()
  50.             winsound.Beep(frequencias[i], int(frequencias[i]//.32))
  51.             i += 1
  52.  
  53.  
  54. def menu():
  55.     choices = {'happy birthday' : happy_birthday, 'alphabet' : alphabet, 'imperial march' : imperial_march}
  56.     choice = input('Type a name of song: ').lower()
  57.     if choice in choices:
  58.         choices[choice]()
  59.     else:
  60.         print("Doesn't have your choice")
  61.  
  62. if __name__ == '__main__':
  63.     try:
  64.         menu()
  65.     except KeyboardInterrupt:
  66.         print("Exiting", end='')
  67.         for i in range(10):
  68.             sys.stdout.write('.')
  69.             sys.stdout.flush()
  70.             time.sleep(0.020)
  71.         else:
  72.             print()
  73.             sys.exit(9)
Advertisement
Add Comment
Please, Sign In to add comment