python编写的midi制作软件

上传人:飞*** 文档编号:60574871 上传时间:2022-03-08 格式:DOCX 页数:8 大小:28.37KB
返回 下载 相关 举报
python编写的midi制作软件_第1页
第1页 / 共8页
python编写的midi制作软件_第2页
第2页 / 共8页
python编写的midi制作软件_第3页
第3页 / 共8页
点击查看更多>>
资源描述
心之所向,所向披靡用Python编写的Mid音频制作软件源代码如下:#Midi.py:#ThisfilecanbeusedtosimplyturnMidiTxtfiletoMidfile.#Author:loupen#Date:2010-7-20#Ifyouwanttouseit,youmustwritetheMidiTxtfilebysomerules.#Readthedocumenttolearnmore.fromstructimport*#ThreeglobalvariablesTrackBegin=TrackEnd=ToneFore=1#writetheMidiHeaderdefMidiHeader(buffer):buffer.write(pack(ccccBBBB,M,T,h,d,0,0Q6)defmsg(buffer,tracks,deltatime):ifdeltatime0xff:buffer.write(pack(BBBBBB,0,1,0,tracks,deltatime8,deltatime&0xff)else:buffer.write(pack(BBBBBB,0,1,0,tracks,0,deltatime)returnbuffer.tell()-3#writetheheaderandtailoftracksdefTrackHeader(buffer,list):buffer.write(pack(cccc,M,r,r,k)list.append(buffer.tell()buffer.write(pack(i,0)defTrackTail(buffer,list):buffer.write(pack(BBBB,0,0xff,0x2f,0)list.append(buffer.tell()#Setthepulse,instrumentandcontrollerdefSetSpeed(buffer,time):time=60.0/int(time)time=int(time*(10*6)buffer.write(pack(BBBBBBB,0,0xff,0x51,3,time16,(time&0xff00)8,time&0xff)return1#Thewords,FenZiandFenMu,arefromChinese.OdefSetPulse(buffer,FenZi,FenMu):FenZi=int(FenZi)FenMu=int(FenMu)ifFenMu=2:FenMu=1elifFenMu=4:FenMu=2elifFenMu=8:FenMu=3else:return0buffer.write(pack(BBBBBBBB,0,0xff,0x58,4,int(FenZi),int(FenMu),0x18,0x08)return1defSetTone(buffer):buffer.write(pack(BBBBBB,0,0xff,0x59,2,0,0)return1defSetNumOrder(buffer,time):buffer.write(pack(BBBB,time24,(time&0xff0000)16,(time&0xff00)8,time&0xff)defSetTrackNum(buffer,ad,n):buffer.seek(ad)buffer.write(pack(B,n+1)defSetInstrument(buffer,trackNum,ITNum):trackNum=int(trackNum)trackNum+=0xc0buffer.write(pack(BBB,0,trackNum,ITNum)defSetController(buffer,trackNum,CtrNum):trackNum=int(trackNum)trackNum+=0xb0buffer.write(pack(BBBB,0,trackNum,11,CtrNum)buffer.write(pack(BBB,0,0xa,0x40)defSetVolume(buffer,trackNum,S):trackNum=int(trackNum)trackNum+=0xb0ifS=H:buffer.write(pack(BBBB,0,trackNum,7,0x7f)buffer.write(pack(BBB,0,0xa,0x40)ifS=L:buffer.write(pack(BBBB,0,trackNum,7,80)buffer.write(pack(BBB,0,0xa,0x40)defSetChanYin(buffer,trackNum):trackNum=int(trackNum)trackNum+=0xb0buffer.write(pack(BBBB,0,trackNum,1,127)#imitatethefunctionofbufferinCdefreadNext(buffer):whileTrue:a=buffer.read(1)ifain,n:passelse:returna#defseekFore(buffer,offset):#buffer.seek(buffer.tell()-offset)#Usethefollowingtwofunctionstodealwiththeeventsdefliftup(buffer,time,note):time=int(time)iftime=128:buffer.write(pack(BBBB,0x80|(time7),0x7f&time,note,0)else:buffer.write(pack(BBB,time,note,0)defpressdown(buffer,num,dict,mark,note,force):globalToneForea=dictnote+(5+mark)*12ifToneFore=1:num+=0x90buffer.write(pack(BBBB,0,num,a,force)else:buffer.write(pack(BBB,0,a,force)ToneFore=adefmain(lp):track=1;deltatime=120speed=;temp=0begin=0;mark=0;time=deltatimeTrackNum=0,1,2,3;ITNum=lp;CtrNum=0x7fdict=0:0,1:0,2:2,3:4,4:5,5:7,6:9,7:11pTxt=open(r.1.txt)s=.%d.mid%lp0pMid=open(s,wb)ifpTxt.read(6)!=”:printThisisnotMidiTxtfile!pMid.close()pTxt.close()return0whileTrue:ifpTxt.read(1)=|:FenMu=pTxt.readpTxt.seek(pTxt.tell()-3)FenZi=pTxt.read(1)n=0whilen8:ifreadNext(pTxt)=:speed+=aa=readNext(pTxt)MidiHeader(pMid)track=msg(pMid,0,deltatime)TrackHeader(pMid,TrackBegin)SetPulse(pMid,FenZi,FenMu)SetTone(pMid)SetSpeed(pMid,speed)TrackTail(pMid,TrackEnd)pMid.seek(TrackBegin0)SetNumOrder(pMid,TrackEnd0-TrackBegin0-4)pMid.seek(TrackEnd0)a=readNext(pTxt)whilea=:pTxt.readline()TrackHeader(pMid,TrackBegin)SetInstrument(pMid,TrackNumn,ITNumn)SetController(pMid,TrackNumn,CtrNum)a=readNext(pTxt)n+=1whileTrue:ifa=|:a=readNext(pTxt)ifa=|:a=readNext(pTxt)ifain,:globalToneForeToneFore=-1;breakelifa=:mark+=1a=readNext(pTxt)elifa=.:ifbegin=0:mark-=1elifbegin=1:time*=3.0/2liftup(pMid,time,ToneFore)begin=0elifbegin=2:a=pTxt.readifain,口:ifa=|:print|istooclosetoNumber!n,pTxt.tell(),pTxt.read(1)time*=3.0/2time+=templiftup(pMid,time,ToneFore)begin=0;temp=0mark=0else:continuea=readNext(pTxt)elifa=Y:time=deltatime/2a=pTxt.readifainJ:ifa=|:print邛istooclosetoNumber!n,pTxt.tell(),pTxt.read(1)ifbegin=2:time+=templiftup(pMid,time,ToneFore)begin=0;temp=0mark=0a=readNext(pTxt)elifa=E:time=deltatime/4a=pTxt.readifain,|:ifa=|:print|istooclosetoNumber!n,pTxt.tell(),pTxt.read(1)ifbegin=2:time+=templiftup(pMid,time,ToneFore)begin=0;temp=0mark=0a=readNext(pTxt)elifa=S:time=deltatime/8a=pTxt.readifainJ:ifa=|:print邛istooclosetoNumber!n,pTxt.tell(),pTxt.read(1)ifbegin=2:time+=templiftup(pMid,time,ToneFore)begin=0;temp=0mark=0a=readNext(pTxt)elif7=a=0:ifbegin=0:ifa=0:force=0else:force=100pressdown(pMid,TrackNumn-1,dict,mark,a,force)time=deltatimemark=0;begin=1a=pTxt.readifain,|:ifa=|:print|istooclosetoNumber!n,pTxt.tell(),pTxt.read(1)liftup(pMid,time,ToneFore)begin=0a=readNext(pTxt)elifbegin=2:time=deltatimea=pTxt.readifain,|:ifa=|:print|istooclosetoNumber!n,pTxt.tell(),pTxt.read(1)time+=templiftup(pMid,time,ToneFore)begin=0;temp=0mark=0a=readNext(pTxt)elifa=A:begin=2temp+=timea=readNext(pTxt)elifa=-:a=pTxt.readifain,|:ifa=|:print|istooclosetoNumber!n,pTxt.tell(),pTxt.read(1)time+=templiftup(pMid,time,ToneFore)begin=0;temp=0mark=0a=readNext(pTxt)elifa=*:SetVolume(pMid,TrackNumn-1,H)ToneFore=1a=readNext(pTxt)elifa=,:SetVolume(pMid,TrackNumn-1,L)ToneFore=1a=readNext(pTxt)elifa=W:SetChanYin(pMid,TrackNumn-1)ToneFore=1a=readNext(pTxt)else:printThisisnotMidiTxtfile!pMid.close()pTxt.close()return0TrackTail(pMid,TrackEnd)pMid.seek(TrackBeginn)SetNumOrder(pMid,TrackEndn-TrackBeginn-4)pMid.seek(TrackEndn)SetTrackNum(pMid,track,n)pMid.close()pTxt.close()printnOK!-LOUPEN”以上文件存为midi.py。#test.py:importmidii=1z=j=int(input(Howmanytracks?)whilei=j:tmp=input(TheInstrumentnumberis:)z.append(int(tmp)i+=1midi.main(z)以上文件存为test.py。以下为mid文本范例【赛马】:1|6.3E5E|6.3E5E|6.3E5E|6.3E5E|6E5E3E5E6E5E3E5E|6E5E3E5E6E5E3E5E|6Y5E6E6Y5E6E|6Y5E6E6Y5E6E|.6Y3Y1Y.6Y|3Y6Y5Y3Y|2E3E2E1E2E3E2E1E|2E3E2E1E2E3E2E1E|.6Y3Y1Y.6Y|3Y6Y5Y3Y|2E3E2E1E2E3E2E1E|2E3E2E1E2E3E2E1E|2.6E1E|2.6E1E|2.6E1E|2.6E1E|2E3E2E1E2E3E2E1E|2E3E2E1E2E3E2E1E|2Y1E2E2Y1E2E|2Y1E2E2Y1E2E|.66|53|25|31|.66|53|25|31|.6.1E2E|.6.1E2E|.6.1E2E|.6.1E2E|.6E2E1E2E.6E2E1E2E|.6E2E1E2E.6E2E1E2E|.6.6Y.6Y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写。#每分钟的节拍数11.1. .II211.1. II/|1/2.3.4人-a-#5b67Y.1YA-A-|# 1。表示升高一个八度。”1表示比1高两个八度。# 2。.在音符前表示降低一个八度。如.6表示比6低两个八度。# 3。.在音符后表示付点音符。如6.# 4。丫表示8分音符,即“一条横线”。如6Y# 5。E表示16分音符,即“二条横线”。如6E# 6。S表示32分音符,即“三条横线”。如6S# 7。#表示升高半音# 8。b表示降低半音# 9。-表示延长一个四分音符。如6A-# 10。人表示两个相同的音相连。如6-表示为6A-注:1。对于表示升降的符号,.后需紧接音符。2。附点音符.,A需紧接在音符之后。A前后两个音必须相同,即6A6或6A-3。Y,E,S需紧接在音符之后。4。空格的作用非常重要,慎用。音符后接空格表示此音符已结束。wm5 sp手机上编辑和测试。5。其他功能,像控制器之类,还有待完善!对于升降音因为很少用就忽略了。编辑后的文本需以1.txt命名。本人刚学python不足之处请高手指教。本文代码在
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业管理 > 营销创新


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!