Last active
January 1, 2016 10:49
-
-
Save felix021/8134302 to your computer and use it in GitHub Desktop.
一个2B小玩意……缅怀当年高中电脑课上用QBasic的Play函数调用扬声器发声的2B时光。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#coding=utf-8 | |
# WINDOWS ONLY # | |
import winsound | |
import time | |
freq_map = { | |
"C<" : 262, | |
"C<#" : 277, | |
"D<" : 294, | |
"D<#" : 311, | |
"E<" : 330, | |
"F<" : 349, | |
"F<#" : 370, | |
"G<" : 392, | |
"G<#" : 415, | |
"A<" : 440, | |
"A<#" : 466, | |
"B<" : 494, | |
"C" : 523, | |
"C#" : 554, | |
"D" : 587, | |
"D#" : 622, | |
"E" : 659, | |
"F" : 698, | |
"F#" : 740, | |
"G" : 784, | |
"G#" : 831, | |
"A" : 880, | |
"A#" : 932, | |
"B" : 988, | |
"C>" : 1046, | |
"C>#" : 1109, | |
"D>" : 1175, | |
"D>#" : 1245, | |
"E>" : 1318, | |
"F>" : 1397, | |
"F>#" : 1480, | |
"G>" : 1568, | |
"G>#" : 1661, | |
"A>" : 1760, | |
"A>#" : 1865, | |
"B>" : 1976, | |
} | |
simple_map = 'CDEFGAB' | |
def PlayNote(beat, duration=400): | |
if isinstance(beat, float): | |
beat = str(int(beat)) + '#' | |
beat = str(beat) | |
note = beat.rstrip('+-').upper() | |
trail = beat[len(note):] | |
if trail: | |
ratio = 4 * (1 - 0.5 ** len(trail)) | |
if '-' in trail: | |
duration /= ratio | |
else: | |
duration *= ratio | |
if note: | |
if note[0].isdigit(): | |
note = simple_map[int(note[0])-1] + note[1:] | |
winsound.Beep(freq_map[note], int(duration)) | |
else: | |
time.sleep(duration / 1000.0) | |
def PlaySong(song, base_duration=400): | |
for note in song: | |
PlayNote(note, base_duration) | |
test = [ | |
'1<', '2<', '3<', '4<', '5<', '6<', '7<', '1', '', | |
'1', '2', '3', '4', '5', '6', '7', '1>', '', | |
'1>', '2>', '3>', '4>', '5>', '6>', '7>' | |
] | |
#PlaySong(test) | |
happy_birthday = [ | |
1, '1-', '2+', '1+', '4+', '3++', '-', | |
1, '1-', '2+', '1+', '5+', '4++', '-', | |
1, '1-', '1>+', '6+', '4+', '3+', '2++', '-', | |
'6#', '6#-', '6+', '4+', '5+', '4++++' | |
] | |
#PlaySong(happy_birthday, 300) | |
little_star = [ | |
1, 1, 5, 5, 6, 6, '5+', | |
4, 4, 3, 3, 2, 2, '1+', '', | |
5, 5, '4+', 3, 3, '2+', | |
5, 5, 4, 4, 3, 3, '2+', '', | |
1, 1, 5, 5, 6, 6, '5+', | |
4, 4, 3, 3, 2, 2, '1+' | |
] | |
#PlaySong(little_star, 200) | |
tommorow_will_be_better = [ | |
3,4,'5+','5+',5,'5+',5,6,'5-','4+', '-', | |
3,4,'5+','5+',3,2,'1+','2+','-', | |
3,2,1,'1+','1-','1>+','1>+','7+','1>','7+','5+','-', | |
6,5,4,'4+',5,6,5,'5+','', | |
'1>+', '1>+','--', 6, 3, '6+', 5, '5+', '' | |
'6+', '6+', '--', 5, '1+', 2, '3+', '-', | |
2, '1+', '1+', 1, '3+', 3, '3+', '2+', '', | |
'6+', '5+', 3, 2, '6<-', 3, '2-', '1++', '', | |
6, '6+', 7, '1>+', '6+', 7, '7+', '1>', '2>+', '7+', | |
'1>', 7, 6, '5+', 5, 1, 2, '3+', '', | |
'3+', 7, '7+', '3+', '2>+', '2>+', '1>', '7+', '6++', '-', | |
6, '6+', 7, '1>+', '6+', 7, '7+', '1>', '2>+', '7+', | |
'3>++', '2>', '1>-', '1>+', '--', | |
'1>', 7, '6+', '6+', 6, '6+', '---', '5+', '5+', 2, '3+', 2, '1++' | |
] | |
#PlaySong(tommorow_will_be_better, 180) | |
Jay_simple_love = [ | |
'5<',1,2,3,2,3,4,'5+','5-',5,4,'3++','2+','', | |
1,1,'2',3,2,3,4,'5+','5-',5,6,'5++','3+','', | |
'1+',1,'6<',2,'2-','3',3,'', | |
'1+',5,1,'5<','1-','7<',1,'', | |
1,'6<',2,'2-',3,3,'4+','4-',4,3,2,1,'1+' | |
] | |
#PlaySong(Jay_simple_love, 200) | |
Stefanie_what_I_cherish = [ | |
1,2,2,'1+',1,'5+','3+','2+','3+', | |
1,2,2,'1+',1,'5+','3+','2+','1+', | |
] | |
#PlaySong(Stefanie_what_I_cherish, 200) | |
Chrismas = [ | |
'5<--','5<--','5<',3,2,1,'5<+', | |
'5<--','5<--','5<',3,2,1,'6<+', | |
'6<--','6<--','6<',4,3,2,'7<+', '-', | |
5,5,4,2,'3+', | |
'5<--','5<--','5<',3,2,1,'5<+', | |
'5<--','5<--','5<',3,2,1,'6<+', | |
'6<--','6<--','6<',4,3,2,5,'5-','5','5-',6,5,4,2,'1++', | |
3,3,'3+',3,3,'3+', | |
3,'5-',1,2,'3+','-', | |
4,'4-',4,'4-',4,3,'3+', | |
'3--','3--',3,2,2,3,'2+',5,'-', | |
3,3,'3+',3,3,'3+', | |
3,'5-',1,2,'3+','-', | |
4,'4-',4,'4-',4,3,'3+', | |
'3--','3--',5,5,4,2,1 | |
] | |
#PlaySong(Chrismas, 300) | |
Because_of_love = [ | |
'5<','6<','1-',1,'6<',1,2,3,'2++', | |
1,'6<',3,2,'6<',3,'2+','1+','6<++', | |
'6<','7<',1,1,'6<','3+','2+', | |
1,'6<+',1,3,'5+','2++', | |
'2+','3+',5,'3++',5,5,3,2,5,'5++', | |
6,7,'1>-','1>','1>-','1>','1>-','7-',5,'3+', | |
5,3,5,'6++',5,6,'5-',5,'1++', | |
'6<',1,3,2,'6<',3,2,'6<','3+','2++', | |
'3+',2,'3+','1+++' | |
] | |
PlaySong(Because_of_love, 300) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment