zoukankan      html  css  js  c++  java
  • 用python写一首歌

    import wave

    import math
    import struct
    ff=wave.open("v1.wav","w")
    ff.setframerate(9000)
    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 #时间流逝
     
    note={"1":440,"2":493.88,"3":554.37,"4":587.33,"5":659.33,"6":739.99,"7":830.61,"6-":329.63,"0":0}
    n=[
        "1","2","3","1","1","2","3","1",
        "3","4","5","0","3","4","5","0",
        "5","6","5","4","3","1","5","6","5","4","3","1",
        "2","6-","1","0","2","6-","1","0"
    ]
    tm=[
        1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,
        0.5,0.5,0.5,0.5,1,1,0.5,0.5,0.5,0.5,1,1,
        1,1,1,1,1,1,1,1,
    ]
     
    for i in range(len(n)):
        wv(tm[i]/2.0,note[n[i]])
     
    ff.close()
  • 相关阅读:
    忙碌进度条
    WPF异步调用WCF
    Oracle 数据类型及存储方式(二)
    C# 与Oracle Data Type Mappings
    【CEO来信】任正非寄语2010:开放、妥协与灰度
    数据库数据类型
    asccii 表
    SQlServer2008 表结构查询
    Oracle 数据类型及存储方式(六)
    应用特征备份
  • 原文地址:https://www.cnblogs.com/fisherpau/p/14275410.html
Copyright © 2011-2022 走看看