csdn上看到一篇博客“根据乐谱合成钢琴音乐(https://blog.csdn.net/u011478373/article/details/60470332)”,写得不错,非常感兴趣,就把博客中的Python代码拷贝下来运行了一下,结果不行,原因是缺乏了一下关键参数定义,如:

1)wave_data

2)ampli

3)windowsize

分析了一下,将这几个参数补充齐了,删除了部分冗余代码,现在程序可以运行了,可以用Python产生出钢琴音色了,十分好听。由于代码可以运行和调试,可以帮助大家理解音乐生成的原理。下面几张图是生成的钢琴声的波形图、声谱图、谐波特征和衰减特征。代码在后面,大家可以下载运行试试,我用的是Python3.4。

谐波特征和时域衰减特征

波形图

声谱图
import waveimport numpy as np
import math
import matplotlib.pyplot as plt# TO DO: reform it into piano
#-----------------------------------------
#生成正弦波
def gen_sin(amp, f, fs, tau):#(开始值,结束值,个数)nT = np.linspace(0,tau, round(tau/(1.0/fs)))#根据步长生成数组,在指定的间隔内返回均匀间隔的数字,返回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谐波与基频的比例关系
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)#谐波个数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)):#计算音符时长#初始化音符为0pianonote = [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#产生谐波,参数:幅度,频率,8000 ,结束=0.5pianonote = 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---4000pianomusic[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变化规律:0,4,8,12,...32,36for 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()

利用Python生成钢琴音色相关推荐

  1. python 词云_利用Python生成词云

    利用Python生成词云 一.第三方库的安装与介绍 1.1  Python第三方库jieba(中文分词) 1.介绍 "结巴"中文分词:做最好的 Python 中文分词组件. 2.特 ...

  2. python表白代码照片墙-python入会生成照片墙 利用python生成照片墙代码

    本篇文章小编给大家分享一下利用python生成照片墙代码,对大家的学习有一定的帮助,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看. PIL(Python Image Libr ...

  3. 动态照片墙 python 实现_利用python生成照片墙的示例代码

    这篇文章主要介绍了利用python生成照片墙的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 PIL(Python Im ...

  4. python制作图片墙_利用python生成照片墙的示例代码

    PIL(Python Image Library)是python的第三方图像处理库,但是由于其强大的功能与众多的使用人数,几乎已经被认为是python官方图像处理库了.其官方主页为:PIL. PIL历 ...

  5. 利用python生成字符画

    先看看效果图: 转换后: 将图片转换成字符画的原理: 先将图片转换成黑白,然后一行行的读取灰度,建立灰度与字符的映射. 转换代码: def transform(image_file): image_f ...

  6. python照片墙地图_利用python生成照片墙的示例代码

    PIL(Python Image Library)是python的第三方图像处理库,但是由于其强大的功能与众多的使用人数,几乎已经被认为是python官方图像处理库了.其官方主页为:PIL. PIL历 ...

  7. 动态照片墙 python 实现_python入会生成照片墙 利用python生成照片墙代码

    本篇文章小编给大家分享一下利用python生成照片墙代码,对大家的学习有一定的帮助,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看. PIL(Python Image Libr ...

  8. python制作心形照片墙_利用python生成照片墙的示例代码

    PIL(Python Image Library)是python的第三方图像处理库,但是由于其强大的功能与众多的使用人数,几乎已经被认为是python官方图像处理库了.其官方主页为:PIL. PIL历 ...

  9. 利用Python 生成hash值

    一.介绍 如果在Python中需要对用户输入的密码或者其他内容进行加密,首选的方法是生成hash值. 在Python中可以利用二个模块来进行: - crypt - hashlib 二.crypt模块 ...

  10. 推荐一个利用 python 生成 pptx 分析报告的工具包:reportgen

    reportgen v0.1.8 更新介绍 这段时间,我对 reportgen 进行了大工程量的修改和更新.将之前在各个文章中出现的函数进行了封装,同时也对现有工具包的一些逻辑进行了调整. 1.rep ...

最新文章

  1. K8S部署工具:KubeOperator集群规划-手动模式
  2. java beans_java beans的概念及应用?
  3. Java Web下访问外部jar,实例后的Object类型转化的问题
  4. 3. Active Record(活动记录模式)
  5. 总结01-bms-molecular-translation分子翻译大赛
  6. secureCRT 连接虚拟机的时候连接失败
  7. C语言一维数组、二维数组传参
  8. CentOS 8 中安装 Docker运行yum install docker-ce docker-ce-cli containerd.io报错
  9. threejs 效果合成器(EffectComposer)
  10. MacOS搭建golang开发环境
  11. 使用chrome设置代理,果然还是linux 方便呢。想安装一个chrome的插件,通过命令设置代理。
  12. centos恢复图形界面_centos7恢复图形界面_centos7没有图形界面
  13. 语音识别基础算法——动态时间规整算法
  14. UVa Problem 10067 Playing With Wheels (摆弄轮子)
  15. 如何通过华硕路由器官方自带功能实现远程FTP、远程观影、远程同步、远程访问登陆界面,有了IPV6,甚至可以买个域名再实现黑裙远程登录
  16. p20能用鸿蒙吗,华为P20怎么升级鸿蒙系统 P20升级鸿蒙系统步骤教程
  17. html5手机视频直播
  18. 关于等级保护测评,这些你都知道吗
  19. python模块-CV2
  20. 声威芯片服务器,魔兽声威坐骑获得攻略 | 手游网游页游攻略大全

热门文章

  1. 编译原理-词法分析-上下文无关文法
  2. 大数据第一季--Hadoop(day7)-徐培成-专题视频课程
  3. android webview 慢,Android Webview的性能问题
  4. android webview权限申请_android WebView全面总结
  5. MySQL间隙锁死锁问题
  6. 定时任务及分布式定时任务注意事项
  7. Matlab 图例 位置的不同命令
  8. Word怎么转PDF?看完这篇你就知道了
  9. c语言孪生素数问题_c语言 孪生素数
  10. 洛谷试炼场:单词接龙