import Utils as u import winsound as w import random ,time # note constants dict noteDict = {"C":1,"Cis":2,"D":3,"Dis":4, "E":5, "F":6, "Fis":7, \ "G":8, "Gis":9, "A":10, "Ais":11, "B":12} noteList = [ (("Dis", "1"),39.89), (("E", "1"),41.20), (("F", "1"), 43.65), (("Fis","1"), 46.25), (("G", "1"), 49.00), (("Gis", "1"), 51.91), (("A","1"), 55.00), (("Ais","1"), 58.27), (("B","1"), 61.74), (("C","2"), 65.41), (("Cis","2"), 69.30), (("D", "2"), 73.42), (("Dis", "2"), 77.78), (("E", "2"), 82.41), (("F", "2"), 87.31), (("Fis", "2"), 92.50), (("G", "2"), 98.00), (("Gis", "2"), 103.83), (("A", "2"), 110.00), (("Ais", "2"), 116.54), (("B", "2"), 123.47), (("C", "3"), 130.81), (("Cis", "3"), 138.59), (("D", "3"), 146.83), (("Dis", "3"), 155.56), (("E", "3"), 164.81), (("F", "3"), 174.61), (("Fis", "3"), 185.00), (("G", "3"), 196.00), (("Gis", "3"), 207.65), (("A", "3"), 220.00), (("Ais", "3"), 233.08), (("B", "3"), 246.94), (("C", "4"), 261.63), (("Cis", "4"), 277.18), (("D", "4"), 293.66), (("Dis", "4"), 311.13), (("E", "4"), 329.63), (("F", "4"), 349.23), (("Fis", "4"), 369.99), (("G", "4"), 392.00), (("Gis", "4"), 415.30), (("A", "4"), 440.00), (("Ais", "4"), 466.16), (("B", "4"), 493.88), (("C", "5"), 523.25), (("Cis", "5"), 554.37), (("D", "5"), 587.33), (("Dis", "5"), 622.25), (("E", "5"), 659.26), (("F", "5"), 698.46), (("Fis", "5"), 739.99), (("G", "5"), 783.99), (("Gis", "5"), 830.61), (("A", "5"), 880.00), (("Ais", "5"), 932.33), (("B", "5"), 987.77), (("C", "6"), 1046.50), (("Cis", "6"), 1108.73), (("D", "6"), 1174.66), (("Dis", "6"), 1244.51), (("E", "6"), 1318.51), (("F", "6"), 1396.91), (("Fis", "6"), 1479.98), (("G", "6"), 1567.98), (("Gis", "6"), 1661.22), (("A", "6"), 1760.00), (("Ais", "6"), 1864.66), (("B", "6"), 1975.53), (("C", "7"), 2093.00), (("Cis", "7"), 2217.46), (("D", "7"), 2349.32), (("Dis", "7"), 2489.02), (("E", "7"), 2637.02), (("F", "7"), 2793.83), (("Fis", "7"), 2959.96), (("G", "7"), 3135.96), (("Gis", "7"), 3322.44), (("A", "7"), 3520.00), (("Ais", "7"), 3729.31), (("B", "7"), 3951.07), (("C", "8"), 4186.01), (("Cis", "8"), 4434.92), (("D", "8"), 4698.64), (("Dis", "8"), 4978.03)] def getFrequency(name, octave): if octave == 1: nr = noteDict[name] - 4 return noteList[nr][1] elif octave > 1 and octave < 9: start = 8 + (octave - 2)*12 if start < 85: nr = noteDict[name] return noteList[start + nr][1] return 440 small = {1:("C", 4), 2:("D", 4), 3:("E",4), 4:("F",4), 5:("G",4), 6:("A", 4), 7:("B", 4), 8:("C",5)} tempo = 40 max = 3 def getDuration(duration): if duration == "1/64": noted = 0.015625 elif duration == "1/32": noted = 0.03125 elif duration == "1/16": noted = 0.0625 elif duration == "1/8": noted = 0.125 elif duration == "1/4": noted = 0.25 elif duration == "1/2": noted = 0.5 else: noted = duration return int((noted/tempo)*60000.0) def playNote(nr): (note, octave) = small[nr] freq = getFrequency(note, octave) w.Beep(freq, getDuration("1/4")) return nr def replay(buffer, count): for i in range(count, len(buffer)): playNote(buffer[i]) def test(): switch = 1 inkey = raw_input("English/Dutch -- Engels/Nederlands (e/n)") if inkey == "n": dutch = 1 else: dutch = 0 while switch: buffer = [] for i in range(1,max): nr = int(random.uniform(1,9)) buffer.append(playNote(nr)) if dutch: inkey = raw_input("Type een toets (w x c v b n , ;)") else: inkey = raw_input("Enter a key (w x c v b n , ;)") count = 0 while count < (max-1) and inkey <> "q": if inkey == "w": nr = 1 elif inkey == "x": nr = 2 elif inkey == "c": nr = 3 elif inkey == "v": nr = 4 elif inkey == "b": nr = 5 elif inkey == "n": nr = 6 elif inkey == ",": nr = 7 elif inkey == ";": nr = 8 playNote(nr) time.sleep(2) replay(buffer, 0) if nr <> buffer[count]: if dutch: print "Foute noot!!!\n Probeer opnieuw!!!" else: print "Wrong note!!!\n Try again!!!" if dutch: inkey = raw_input("Type een toets (w x c v b n , ;)\ \nNotes are: do re mi fa sol la si do\n") else: inkey = raw_input("Enter a key (w,x,c,v,b,n,,)") else: if dutch: print "Goed zo ho!!!" print "U speelde: " + show(nr) else: print "Well done!!!" print "You played: " + show(nr) count += 1 if count < (max - 1): if dutch: inkey = raw_input("Type een toets (w,x,c,v,b,n,,)") else: inkey = raw_input("Enter a key (w,x,c,v,b,n,,)") if dutch: inkey = raw_input("Wil je opnieuw spelen (j/n)?") else: inkey = raw_input("Do you want to play again (y/n)?") if inkey == "n": switch = 0 def show(nr): if nr == 1: return " do " elif nr == 2: return " re " elif nr == 3: return " mi " elif nr == 4: return " fa " elif nr == 5: return " sol " elif nr == 6: return " la " elif nr == 7: return " si " elif nr == 8: return " do " random.seed() test()