首页 > 代码库 > 数字信号处理(playthattune.py)

数字信号处理(playthattune.py)

 1 import stdarray
 2 import math
 3 import stddraw
 4 import stdaudio
 5 import stdio
 6 SPS = 44100
 7 CONCERT_A = 440.0
 8 while not stdio.isEmpty():
 9     pitch = stdio.readInt()
10     duration = stdio.readFloat()
11     hz = CONCERT_A * (2 ** (pitch/12.0))
12     n = int(SPS * duration)
13     samples = stdarray.create1D(n+1, 0.0)
14     for i in range(n+1):
15         samples[i] = math.sin(2.0 * math.pi * i * hz / SPS)
16     stdaudio.playSamples(samples)
17 stdaudio.wait()

 

数字信号处理(playthattune.py)