python写 两只老虎.wav 音频文件

import wave
import struct
import math
 
def write_frame(time,freq,framerate,file,wave=0.4,sampwidth=2):
    #time 持续时间 freq 音频频率 framerate采样频率 file 音频文件 wave 音量 sampwidth 采样深度
    t=0#时刻
    step=1.0/framerate #每帧间隔时长
    fw=2.0*math.pi*freq #频率控制参数
    wave=wave*(math.pow(2,sampwidth*8-1)-1)#音量控制
    while t<=time:
        v=int(math.sin(t*fw)*wave)#对波采样  math.sin(t*fw)产生freq频率的正弦波
        t+=step#更新时刻
        #最后这里是与sampwidth的值有关的,下面语句当前仅当sampwidth=2时成立,详细信息参考struct.pack()
        file.writeframesraw(struct.pack("h",v))#写入文件  struct.pack("h",v)将有符号整数v转化成16比特2进制
    

tw=wave.open("./two_tigers.wav","w") #打开或创建./two_tigers.wav
tw.setnchannels(1) #设置声道数 1
tw.setframerate(8000)#设置帧率 8000
tw.setsampwidth(2)#设置采样宽度2B 16bit

#写入声音
#1 2 3 1 1 2 3 1 
write_frame(time=0.5, freq=256, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=288, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=320, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=256, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=256, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=288, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=320, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=256, framerate=8000, file=tw, wave=0.4, sampwidth=2)
 
#3 4 5 - 3 4 5 -
write_frame(time=0.5, freq=320, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=341.33, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=384, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.25, freq=0, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=320, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=341.33, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=384, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.25, freq=0, framerate=8000, file=tw, wave=0.4, sampwidth=2)
 
#56 54 3 1 - 56 54 3 1 -
write_frame(time=0.25, freq=384, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.25, freq=426.67, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.25, freq=384, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.25, freq=341.33, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=320, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=256, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.25, freq=384, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.25, freq=426.67, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.25, freq=384, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.25, freq=341.33, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=320, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=256, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.25, freq=0, framerate=8000, file=tw, wave=0.4, sampwidth=2)
 
#2 6(低音) 1 - 2 6(低音) 1 -
write_frame(time=0.5, freq=288, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=144, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=256, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.25, freq=0, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=288, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=144, framerate=8000, file=tw, wave=0.4, sampwidth=2)
write_frame(time=0.5, freq=256, framerate=8000, file=tw, wave=0.4, sampwidth=2)
 
 
tw.close()
 
# C 1 do 256
# D 2 re 288
# E 3 mi 320
# F 4 fa 341又1/3
# G 5 so 384
# A 6 la 426又2/3
# B 7 si 480
# C 1 (上面一个点)do 512
# C:D=8:9
# D:E=9:10
# E:F=15:16
# F:G=8;9
# G:A=9:10
# A:B=15:16
 
 

参考

https://blog.csdn.net/Moluth/article/details/79793903

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值