python发声-python3-声音处理

阅读: 评论:0

python发声-python3-声⾳处理
先来说下⼆进制读写⽂件,这需要struct库
#⼆进制⽂件读写
importstructa=1b=-1# print(struct.pack("h",b))
# print(struct.pack("i",b))
f=open("bbb.bin","wb")
f.write(struct.pack("h",a))#对a装包,并写⼊
f.write(struct.pack("h",b))
f.close()
f=open("bbb.bin",'rb')
ad(2)
a=struct.unpack("h",b1)#解包操作
ad(2)
b=struct.unpack("h",b2)
print(a,b)
然后是通过python创造⼀段声⾳
importwaveimportmathimportstruct
f=wave.open("v0.wav","w")
f.setframerate(8000)#声⾳频率
f.setnchannels(1)#声道数
f.setsampwidth(2)#声⾳宽度
t=0#时间
v=0.5#⾳量
dt=1/8000.0#录⼊声⾳的时间
while t<5:
s=math.sin(t*math.pi*2*800)*v*32768#设置声⾳频率为-32768到32768,并且为正弦变化曲线s=int(s) fd=struct.pack("h",s)#⼆进制写⼊
f.writeframes(fd)
t+=dt
f.close()
运⽤这个原理可以写⼀个简单的声⾳⽂件,⽐如说两只⽼虎
importwaveimportmathimportstruct
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/srwhile 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#时间流逝
note={"1":262,"2":294,"3":330,"4":349,"5":392,"6":440,"7":494,"6-":220,"0":0}
n=["1","2","3","1","1","2","3","1","0","3","4","5","0","3","4","5","0","5","6","5","4","3","1","0","5","6","5","4","3","1","0","2","6-","1","0","2","6-","1"]
tm=[2,2,2,2,2,2,2,2,1,2,2,2,1.5,2,2,2,2,1,1,1,1,2,2,1,1,1,1,1,1,2,1,2,2,2,2,2,2,2]for i inrange(len(n)):
wv(tm[i]/4.0,note[n[i]])
ff.close()
#河⼤校歌
importwaveimportmathimportstruct
ff=wave.open("henu.wav","w")
ff.setframerate(8000)
ff.setnchannels(1)
ff.setsampwidth(2)
f:声⾳频率
v:⾳量
wf:⼀个可以写⼊的⾳频⽂件
sr:采样率'''tt=0
dt=1.0/srwhile tt<=t:
s=math.sin(tt*math.pi*2*f)*v*math.pow(math.e,-2.5*tt)*32768#采样,调节⾳量,映射到[-2^15,2^15)
s=int(s)
fd=struct.pack("h",s)#转换成8bit⼆进制数据
wf.writeframes(fd)#写⼊⾳频⽂件
tt+=dt#时间流逝
note={"-":0,"0":0,"1---":49,"1#---":52,"2b---":52,"2---":55,"2#---":58,"3b---":58,"3---":62,"4---":65,"4#---":69,"5b---":69,"5---
":73,"5#---":78,"6b---":78,"6---":82,"6#---":87,"7b---":87,"7---":92,"1--":98,"1#--":104,"2b--":104,"2--":110,"2#--":117,"3b--
":117,"3--":123,"4--":131,"4#--":139,"5b--":139,"5--":147,"5#--":156,"6b--":156,"6--":165,"6#--":175,"7b--":175,"7--":185,"1-
":196,"1#-":208,"2b-":208,"2-":220,"2#-":233,"3b-":233,"3-":247,"4-":262,"4#-":277,"5b-":277,"5-":294,"5#-":311,"6b-
":311,"6-":330,"6#-":349,"7b-":349,"7-
":370,"1":392,"1#":415,"2b":415,"2":440,"2#":466,"3b":466,"3":494,"4":523,"4#":554,"5b":554,"5":587,"5#":622,"6b":622,"6":659,"
n=["5-","1","1","1","7-","1","3","3","3","2","5","5","6","5","4","3","2","-","3","2","1","7-","6-","7-","1","2","3","4","6","5","4","3","2","3","1","5","-","2","3","4","4","6","6","5","4","3","4","2","3","5","2","3","2","7-","5-","1","3","5","6","5","-","6","4","2","-","2","3","4","2","3","-","6-","7-","1","-"]
tm=
[2,2,2,1,1,2,2,2,1,1,3,1,2,2,2,2,2,2,3,1,1,1,2,3,1,1,1,2,2,2,2,2,2,1,1,2,2,3,1,2,2,3,1,1,1,2,2,2,2,2,3,1,1,1,2,1,1,1,1,2,2,2,2,2,2,1,1,1,1 i inrange(len(n)):
wv(tm[i]/2.0,note[n[i]])
ff.close()
接下来是⽐较⾼级的了,显⽰声⾳的⾳波
import matplotlib.pyplot as plt
import wave
import struct
fig=plt.figure()
ax=fig.add_subplot(1,1,1)#如果参数是349的意思是:将画布分割成3⾏4列,图像画在从左到右从上到下的第9块
plt.ion()#使matplotlib的显⽰模式转换为交互(interactive)模式。即使在脚本中遇到plt.show(),代码还是会继续执⾏。
read_size=200#每次绘画的帧数
视频显示器
pass_f=0#跳过前pass的帧数
draw_channel=0
stay_time=1.0
file=wave.open("henu.wav","r")
frame_framerate()
frames = frames()#得到频率
hannels()#得到声道数焊接钢板
draw_channel=draw_channel %channels
i=0
x_data=[x / read_size for x in range(read_size)]
fmt="h" * (read_size * channels)
乙基氯化物while i
adframes(read_size)
i+=read_size
if pass_f > 0:
pass_f-=1
continue
f_data = struct .unpack(fmt,fs) # f_data中包含多个声道的声⾳
y_data =[]
for j in range(0,len(f_data)):
if j%channels==draw_channel:
y_data.append(f_data[j]/32768.0)
lines = ax.plot(x_data, y_data, 'g-', lw=1)
plt.pause(stay_time)
ve(lines[0])
file.close()
这个是拟合⼀个图像的曲线,机器学习
#训练⼀个多项式,来拟合⼀条曲线
from PIL importImageimportmatplotlib.pyplot as pltimportnumpy as npimportrandomimportmathimporttensorflow as tf pix2=math.pi*2img=np.array(Image.open("./p2.bmp"),dtype=np.int32)#(249, 277, 3)图像的 ⾼度,宽度,通道rgb#Y中点125 data=[]
a=img[:,:,0]+img[:,:,1]+img[:,:,2]
h,w,_=np.shape(img)
nw=-1
for j inrange(w):for i inrange(h):if a[i,j]<200:
data.append([j*1.0/w,(125.0-i)*2.0/w])if j%13==0 and nw!=j:
nw=j
data_len=len(data)#y=a1*sin(2pix*b1)+a2*sin(2pix*b2)+a3*sin(2pix*b3)...
#画图
fig=plt.figure()#⽣成图像框
ax=fig.add_subplot(1,1,1)#编号
ax.scatter([ w[0] for w in data],[w[1] for w in data],linewidths=1)
plt.ion()>>>>>>>>>>>#模型
w_size=8batch_size=64x=tf.placeholder(dtype=tf.float32, shape=[None,1])
y=tf.placeholder(dtype=tf.float32, shape=[None,1])
w=tf.uncated_normal(shape=
[w_size,1],mean=0.0,stddev=2/w_size,dtype=tf.float32))#stant([[(i+1)*2*math.pi for i in range(w_size)]],tf.float32) c=tf.stant([[(i+1)*2*math.pi for i inrange(w_size)]],tf.float32))
res=tf.matmul(tf.sin(tf.matmul(x,c)),w)
duce_mean(tf.abs((res-y)))
train_ain.AdamOptimizer(0.02).minimize(loss)
init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)for i in range(1,10000):
rnddata=[random.randint(1,data_len-1) for w inrange(batch_size)]
x_d=[[data[p][0]] for p inrnddata]
y_d=[[data[p][1]] for p inrnddata]
ls,st=sess.run([loss,train_step],feed_dict={x:x_d,y:y_d})print(i,":",ls)if i%100==0: x_data=[[j/500.0] for j in range(500)]
y_data,ww,cc=sess.run([res,w,c],feed_dict={x:x_data})print(ww)print(cc)电工工具包
lines=ax.plot([j[0] for j in x_data],[j[0] for j in y_data],'r-',lw=1)
plt.pause(1)
机器人电主轴ve(lines[0])
最后是你输⼊⽂字然后让它发⾳,前提是你要有汉字表和发⾳表
t2pinyin={}
yunmu="āáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ"
yindiao="123412341234123412341234"
rp="aaaaooooeeeeiiiiuuuuvvvv"
for i in range(1,10):#总共有⼗个⽂件,循环打开
fn="./pinyin1/h"+str(i)+".txt"
py=open(fn,"r",encoding="utf-8")#打开⽂件名 只读 utf-8
adline() #读取⼀⾏,再次调⽤就是下⼀⾏,先把第⼀⾏⽂字读进来
line2="" #读拼⾳
while len(line2)<=10:#查哪⼀⾏是拼⾳
adline()
place(" ","")#替换换⾏
place(" ","").replace("ü","v")
py.close()
pinyin=line2.split(" ")#变换成列表
for j in range(len(line1)):
word=line1[j]#word和pyj照应
pyj=pinyin[j]

本文发布于:2023-05-20 22:09:33,感谢您对本站的认可!

本文链接:https://patent.en369.cn/patent/4/107412.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:曲线   模式   进制
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 369专利查询检索平台 豫ICP备2021025688号-20 网站地图