python最先接触的是爬虫,毕业设计时,去网上爬取了两个多小时的数据导入了MongoDB数据库
声音处理和图像处理是我一直想接触的领域
一起学习吧,与君共勉。
import wave
import math
import struct
a=["1","1#,2b","2","2#,3b","3","4","4#,5b","5","5#,6b","6","6#,7b","7"]
b=["---","--","-","","+","++","+++"]
ai=0
bi=0
voice_map={}
for i in range(-45+7,38-7):
note=a[ai].split(",")
f=b[bi]
freq=440*math.pow(2.0,i/12.0)
if freq-int(freq)>=0.5:
freq=int(freq)+1
else:
freq=int(freq)
key=note[0]+f
voice_map[key]=freq
# print(key,freq)
if len(note)>1:
key=note[1]+f
voice_map[key]=freq
# print(key,freq)
ai+=1
if ai>=len(a):
ai=0
bi+=1
ff=wave.open("v1.wav","w")
ff.setframerate(8000)
ff.setnchannels(1)
ff.setsampwidth(2)
def wv(t=0,f=0,v=0.5,wf=ff,sr=8000):
'''
t:写入时长
f:声音频率
v:音量
wf:一个可以写入的音频文件
sr:采样率
'''
tt=0
dt=1.0/sr
while tt<=t:
s=math.sin(tt*math.pi*2*f)*v*32768#采样,调节音量,映射到[-2^15,2^15)
s=int(s)
fd=struct.pack("h",s)#转换成8bit二进制数据
wf.writeframes(fd)#写入音频文件
tt+=dt#时间流逝
n=[
"5-","1","1","1","7-",
"1","3","3","3","2",
"5","5","6","5",
"4","3","2"
]
tm=[
1,1,1,0.5,0.5,
1,1,1,0.5,0.5,
1.5,0.5,1,1,
1,1,2
]
for i in range(len(n)):
wv(tm[i]/1.0,voice_map[n[i]])
ff.close()