[MicroPython] 平安夜旋律示範
-
# WiFiBoy MicroPython Melody Engine Demo
# Dec 24, 2016, Derek Lai, derek@wifiboy.org
# "Silent Night" with buzzer tricksimport machine, time
snd=machine.PWM(machine.Pin(4,1))
adc=machine.ADC(0)env2=[100,100,100,0,0,0,0,0]
env5=[20,90,20,80,20,60,5,0]
env11=[120,50,30,20,10,0,0,0]
env12=[100,30,10,0,0,0,0,0]f=[round(55*2**(x/12)) for x in range(51)]
mel2=[#silent night
23,0,0,25,23,0,20,0,0,0,0,0,23,0,0,25,23,0,20,0,0,0,0,0,
30,0,0,0,30,0,27,0,0,0,0,0,28,0,0,0,28,0,23,0,0,0,0,0,
25,0,0,0,25,0,28,0,0,27,25,0,23,0,0,25,23,0,20,0,0,0,0,0,
25,0,0,0,25,0,28,0,0,27,25,0,23,0,0,25,23,0,20,0,0,0,0,0,
30,0,0,0,30,0,33,0,0,30,27,0,28,0,0,0,0,0,32,0,0,0,0,0,
28,0,23,0,20,0,23,0,0,21,18,0,16,0,0,0,0,0,0,0,0,0,0,0]
mel2a=[#silent night
35,11,18,37,35,11,32,11,18,11,20,11,35,11,18,37,35,11,32,11,18,11,20,11,
42,9,18,9,42,9,39,11,15,11,15,11,40,11,16,11,40,11,35,11,18,11,16,11,
37,13,16,13,37,13,40,13,16,39,37,9,35,11,18,37,35,11,32,11,18,8,16,11,
37,13,16,13,37,13,40,13,16,39,37,9,35,11,18,37,35,11,32,11,15,11,15,11,
42,11,15,11,42,11,45,11,15,42,39,18,40,11,8,11,16,18,44,11,8,11,16,11,
40,8,35,8,32,16,35,11,9,33,30,11,28,11,16,18,16,11,8,11,16,18,16,0]def stop():
snd.duty(0)def play(dt, mel, env, shift): # melody engine
for j in range(0,len(mel)):
if (mel[j]!=0):
snd.freq(f[mel[j]+shift])
ei=0
for i in range(0,4):
if (ei<8):
snd.duty(env[ei])
time.sleep(dt)
ei=ei+1wifiboy.cls()
wifiboy.colors(0xfe0f, 0xfe0f)
wifiboy.str("WiFiBoy",10,10,1,3)
wifiboy.str("Melody Engine Demo",10,35,3,1)
wifiboy.str("Silent Night",10,60,3,1)play(0.04,mel2,env2,-7)
play(0.05,mel2a,env12,5)
play(0.04,mel2,env5,12)
play(0.05,mel2a,env11,5)
stop()