Advertisement
kolyaventuri

Music.py

Apr 26th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. import os, random
  2. try:
  3.     import winsound
  4. except ImportError:
  5.     import os
  6.     def playsound(frequency,duration):
  7.         os.system('beep -f %s -l %s' % (frequency,duration))
  8. else:
  9.     def playsound(frequency,duration):
  10.         winsound.Beep(frequency,duration)
  11.  
  12. major = [523.25,587.33,659.26,698.46,783.99,880.00,978.77,1046.50]
  13. pent = [554.37,622.25,739.99,830.61,932.33]
  14.  
  15. def playSounds(scale, number, length) :
  16.     if scale == 'm' :
  17.         sc = major
  18.     elif scale == 'p' :
  19.         sc = pent
  20.     else :
  21.         sc = major
  22.     for x in range(1, int(number)+1) :
  23.         i = random.randint(1, len(sc))
  24.         playsound(str(sc[i-1]), str(length))
  25.  
  26. def playFile(data, times) :
  27.     for i in range(len(dat)) :
  28.         playsound(str(dat[i]),str(times[i]))
  29.  
  30. scale = raw_input("Major scale(m), Pentatonic scale(p), File(f)?: ")
  31. if scale != 'f' :
  32.     number = raw_input("Number of notes: ")
  33.     length = raw_input("Length of each note: ")
  34.     playSounds(scale, number, length)
  35. else :
  36.     filetoread = raw_input("Location of file: ")
  37.     f = open(filetoread, 'r')
  38.     data = []
  39.     dat = []
  40.     times = []
  41.     time = []
  42.     for line in f:
  43.         data.append(line)
  44.     for i in range(len(data)) :
  45.         data = map(lambda s: s.strip(), data)
  46.         time = data[i].split('- ')
  47.         dat.append(time[0])
  48.         dat = [x.strip(' ') for x in dat]
  49.         for (i, item) in enumerate(dat):
  50.                 if item == "c":
  51.                     dat[i] = '523.25'
  52.                 if item == "d":
  53.                     dat[i] = '587.33'
  54.                 if item == "e":
  55.                     dat[i] = '659.26'
  56.                 if item == "f":
  57.                     dat[i] = '698.46'
  58.                 if item == "g":
  59.                     dat[i] = '783.99'
  60.                 if item == "a":
  61.                     dat[i] = '880.00'
  62.                 if item == "b":
  63.                     dat[i] = '987.77'
  64.                 if item == "c+":
  65.                     dat[i] = '1046.50'
  66.         times.append(time[1])
  67.     playFile(dat, times)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement