亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

利用Python生成鋼琴音色

系統(tǒng) 2774 0

csdn上看到一篇博客“根據(jù)樂譜合成鋼琴音樂(https://blog.csdn.net/u011478373/article/details/60470332)”,寫得不錯,非常感興趣,就把博客中的Python代碼拷貝下來運行了一下,結(jié)果不行,原因是缺乏了一下關(guān)鍵參數(shù)定義,如:

1)wave_data

2)ampli

3)windowsize

分析了一下,將這幾個參數(shù)補充齊了,刪除了部分冗余代碼,現(xiàn)在程序可以運行了,可以用Python產(chǎn)生出鋼琴音色了,十分好聽。由于代碼可以運行和調(diào)試,可以幫助大家理解音樂生成的原理。下面幾張圖是生成的鋼琴聲的波形圖、聲譜圖、諧波特征和衰減特征。代碼在后面,大家可以下載運行試試,我用的是Python3.4。

利用Python生成鋼琴音色_第1張圖片 諧波特征和時域衰減特征

?

利用Python生成鋼琴音色_第2張圖片 波形圖 利用Python生成鋼琴音色_第3張圖片 聲譜圖
            
              import wave

import numpy as np
import math
import matplotlib.pyplot as plt

# TO DO: reform it into piano
#-----------------------------------------
#生成正弦波
def gen_sin(amp, f, fs, tau):
    #(開始值,結(jié)束值,個數(shù))
    nT = np.linspace(0,tau, round(tau/(1.0/fs)))#根據(jù)步長生成數(shù)組,在指定的間隔內(nèi)返回均勻間隔的數(shù)字,返回num個均勻分布的樣本,在[start, stop]。
    signal =np.array([amp*np.cos(2*np.pi*f*t) for t in nT])
    return signal

#model the harmonic feature in frequency domain
#1~15諧波與基頻的比例關(guān)系
Amp=[1,0.340,0.102,0.085,0.070,0.065,0.028,0.085,0.011,0.030,0.010,0.014,0.012,0.013,0.004]
numharmonic=len(Amp)#諧波個數(shù)

wave_data=np.array([0 for i in range(0,40000)])
wave_data = np.reshape(wave_data,[40000,1]).T
pianomusic=[0 for x in range(0,len(wave_data[0]))]
startpoint=0

#model the piano note attenuation feature in the time domain
#對每個鋼琴音的時域衰減建模
attenuation=[0 for x in range(0, 8000)]
#the attack stage
for i in range(0,200):
    attenuation[i]=i*0.005
#the attenuate stage
#衰減階段
for i in range(200,800):
    attenuation[i]=1-(i-200)*0.001
#the maintain stage
#保持階段    
for i in range(800,4000):
    attenuation[i]=0.4-(i-800)*0.000078
for i in range(4000,8000):
    attenuation[i]=0.15-(i-4000)*0.0000078

#compose each note in each time quantum
nomalizedbasicfreq=[261.63,261.63,261.63,261.63,293.665,293.665,293.665,293.665,329.628,329.628,329.628,329.628,349.228,349.228,349.228,349.228,391.995,391.995,391.995,391.995,440,440,440,440,493.883,493.883,493.883,493.883,523.251,523.251,523.251,523.251,587.33,587.33,587.33,587.33,659.255,659.255,659.255,659.255]
ampli=[(math.pow(2,2*8-1)-1) for i in range(0,40)]
#40個/4=10
notestime=[4,4,4,4,4,4,4,4,4,4]#10個
windowsize=1000
for w in range(0,len(notestime)):
    #計算音符時長
    #初始化音符為0
    pianonote = [0 for x in range(0, windowsize*notestime[w])] #get the length according to the time of the note
    #計算每一個諧音并累加
    for i in range(0, numharmonic): #get the note by add each harmonic by the amplitude comparatively with the basic frequency
        #產(chǎn)生諧波,參數(shù):幅度,頻率,8000 ,結(jié)束=0.5
        pianonote = pianonote + gen_sin(ampli[startpoint] /50* Amp[i], nomalizedbasicfreq[startpoint] * (i + 1), 8000, 0.125*notestime[w])
        #矢量加法
    #attenuate the note with the time domain feature
    #進行衰減
    for k in range(0,windowsize*notestime[w]):#k:0---4000
        pianomusic[startpoint*windowsize+k]=pianonote[k]*attenuation[k]
        #0--4000 startpoint=0
        #4000-8000   =4
        #8000-12000  =8
        #36*1000---36*1000+4000(40000)4萬
    startpoint=startpoint+notestime[w] #record the start point of the next note
    #startpoint變化規(guī)律:0,4,8,12,...32,36

for i in range(0,len(wave_data[0])):
    wave_data[0][i]=pianomusic[i]


#get a wave file
f = wave.open(r"pianomusic.wav", "wb")
#get the channel, sampling width and sampling frequency information
#see details in 2.9 of my report
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(8000)
f.writeframes(wave_data[0].tostring()) #put the data into the wave file
f.close()

print("STEP 9: please see in figure and listen the pianomusic.wav file")
plt.figure()
plt.subplot(211)
plt.plot(Amp)
plt.title(r'frequency domain harmonic feature')
plt.subplot(212)
plt.plot(attenuation)
plt.title(r'time domain attenuation feature')
plt.figure()
plt.plot(pianomusic)
plt.title(r'STEP 9:reform it into piano')
plt.show()

            
          

?


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产一级内谢a级高清毛片 国产一级片毛片 | 亚洲免费国产 | 国产视频毛片 | 不一样的天空在线高清观看 | 五月狠狠亚洲小说专区 | 日韩亚洲欧美综合一区二区三区 | 亚洲美色综合天天久久综合精品 | 麻豆精品久久久 | 国产综合色香蕉精品五月婷 | 狠狠地操| 天天射天天操天天色 | 国产香蕉在线精彩视频 | 波多野结衣一区二区三区高清在线 | 午夜一级精品免费毛片 | 国产欧美日韩一区 | 奇米网在线观看 | 亚洲精品色播一区二区 | 四虎精品国产一区二区三区 | 欧美亚洲国产人成aaa | 一级欧美在线的视频 | 麻豆成人精品国产免费 | 玖玖在线 | 中文精品久久久久国产网址 | 国产精品美女流白浆视频 | 日本亚洲欧美国产日韩ay高清 | 日韩欧美国产一区二区三区四区 | 国产午夜久久精品 | 成年视频xxxxxx在线 | 成年女人毛片免费观看中文w | 四虎影视免费看 | jazzjazz国产精品久久 | 5566中文字幕亚洲精品 | 日韩亚洲欧美综合一区二区三区 | 欧美一级在线全免费 | 亚洲欧美人成人综合在线50p | 美女精品| 夜夜干天天操 | 国产99视频在线观看 | 丁香综合在线 | 精品久久久久久久99热 | 天堂成人在线 |