python编写的midi制作软件.doc_第1页
python编写的midi制作软件.doc_第2页
python编写的midi制作软件.doc_第3页
python编写的midi制作软件.doc_第4页
python编写的midi制作软件.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

用Python编写的Mid音频制作软件源代码如下:#Midi.py:#This file can be used to simply turn MidiTxt file to Mid file.#Author:loupen#Date:2010-7-20#If you want to use it, you must write the MidiTxt file by some rules. #Read the document to learn more.from struct import *#Three global variablesTrackBegin=TrackEnd=ToneFore=1#write the MidiHeaderdef MidiHeader(buffer): buffer.write(pack(ccccBBBB,M,T,h,d,0,0,0,6)def msg(buffer,tracks,deltatime): if deltatime0xff: buffer.write(pack(BBBBBB,0,1,0,tracks,deltatime8,deltatime&0xff) else: buffer.write(pack(BBBBBB,0,1,0,tracks,0,deltatime)return buffer.tell()-3#write the header and tail of tracksdef TrackHeader(buffer,list): buffer.write(pack(cccc,M,T,r,k) list.append(buffer.tell() buffer.write(pack(i,0)def TrackTail(buffer,list): buffer.write(pack(BBBB,0,0xff,0x2f,0) list.append(buffer.tell()#Set the pulse , instrument and controllerdef SetSpeed(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) return 1#The words , FenZi and FenMu , are from Chinese . Odef SetPulse(buffer,FenZi,FenMu): FenZi=int(FenZi) FenMu=int(FenMu) if FenMu=2: FenMu=1 elif FenMu=4: FenMu=2 elif FenMu=8: FenMu=3 else: return 0 buffer.write(pack(BBBBBBBB,0,0xff,0x58,4,int(FenZi),int(FenMu),0x18,0x08) return 1def SetTone(buffer): buffer.write(pack(BBBBBB,0,0xff,0x59,2,0,0) return 1def SetNumOrder(buffer,time): buffer.write(pack(BBBB,time24,(time&0xff0000)16,(time&0xff00)8,time&0xff)def SetTrackNum(buffer,ad,n): buffer.seek(ad) buffer.write(pack(B,n+1)def SetInstrument(buffer,trackNum,ITNum): trackNum=int(trackNum) trackNum+=0xc0 buffer.write(pack(BBB,0,trackNum,ITNum)def SetController(buffer,trackNum,CtrNum): trackNum=int(trackNum) trackNum+=0xb0 buffer.write(pack(BBBB,0,trackNum,11,CtrNum) buffer.write(pack(BBB,0,0xa,0x40)def SetVolume(buffer,trackNum,S): trackNum=int(trackNum) trackNum+=0xb0 if S=H: buffer.write(pack(BBBB,0,trackNum,7,0x7f) buffer.write(pack(BBB,0,0xa,0x40) if S=L: buffer.write(pack(BBBB,0,trackNum,7,80) buffer.write(pack(BBB,0,0xa,0x40)def SetChanYin(buffer,trackNum): trackNum=int(trackNum) trackNum+=0xb0 buffer.write(pack(BBBB,0,trackNum,1,127)#imitate the function of buffer in Cdef readNext(buffer): while True: a=buffer.read(1) if a in ,n: pass else: return a#def seekFore(buffer,offset):# buffer.seek(buffer.tell()-offset)#Use the following two functions to deal with the eventsdef liftup(buffer,time,note): time=int(time) if time=128: buffer.write(pack(BBBB,0x80|(time7),0x7f&time,note,0) else: buffer.write(pack(BBB,time,note,0)def pressdown(buffer,num,dict,mark,note,force): global ToneFore a=dictnote+(5+mark)*12 if ToneFore=1: num+=0x90 buffer.write(pack(BBBB,0,num,a,force) else: buffer.write(pack(BBB,0,a,force) ToneFore=adef main(lp): track=1;deltatime=120 speed=;temp=0 begin=0;mark=0;time=deltatime TrackNum=0,1,2,3;ITNum=lp;CtrNum=0x7f dict=0:0,1:0,2:2,3:4,4:5,5:7,6:9,7:11 pTxt=open(r.1.txt) s=.%d.mid %lp0 pMid=open(s,wb) if pTxt.read(6)!=: print This is not MidiTxt file! pMid.close() pTxt.close() return 0 while True: if pTxt.read(1)=|: FenMu=pTxt.read(1) pTxt.seek(pTxt.tell()-3) FenZi=pTxt.read(1) n=0 while n8: if readNext(pTxt)=: speed+=a a=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) while a=: pTxt.readline() TrackHeader(pMid,TrackBegin) SetInstrument(pMid,TrackNumn,ITNumn) SetController(pMid,TrackNumn,CtrNum) a=readNext(pTxt) n+=1 while True: if a=|: a=readNext(pTxt) if a=|: a=readNext(pTxt) if a in ,: global ToneFore ToneFore=-1;break elif a=: mark+=1 a=readNext(pTxt) elif a=.: if begin=0: mark-=1 elif begin=1: time*=3.0/2 liftup(pMid,time,ToneFore) begin=0 elif begin=2: a=pTxt.read(1) if a in ,|: if a=|: print | is too close to Number !n,pTxt.tell(),pTxt.read(1) time*=3.0/2 time+=temp liftup(pMid,time,ToneFore) begin=0;temp=0 mark=0 else:continue a=readNext(pTxt) elif a=Y: time=deltatime/2 a=pTxt.read(1) if a in ,|: if a=|: print | is too close to Number !n,pTxt.tell(),pTxt.read(1) if begin=2:time+=temp liftup(pMid,time,ToneFore) begin=0;temp=0 mark=0 a=readNext(pTxt) elif a=E: time=deltatime/4 a=pTxt.read(1) if a in ,|: if a=|: print | is too close to Number !n,pTxt.tell(),pTxt.read(1) if begin=2:time+=temp liftup(pMid,time,ToneFore) begin=0;temp=0 mark=0 a=readNext(pTxt) elif a=S: time=deltatime/8 a=pTxt.read(1) if a in ,|: if a=|: print | is too close to Number !n,pTxt.tell(),pTxt.read(1) if begin=2:time+=temp liftup(pMid,time,ToneFore) begin=0;temp=0 mark=0 a=readNext(pTxt) elif 7=a=0: if begin=0: if a=0: force=0 else: force=100 pressdown(pMid,TrackNumn-1,dict,mark,a,force) time=deltatime mark=0;begin=1 a=pTxt.read(1) if a in ,|: if a=|: print | is too close to Number !n,pTxt.tell(),pTxt.read(1) liftup(pMid,time,ToneFore) begin=0 a=readNext(pTxt) elif begin=2: time=deltatime a=pTxt.read(1) if a in ,|: if a=|: print | is too close to Number !n,pTxt.tell(),pTxt.read(1) time+=temp liftup(pMid,time,ToneFore) begin=0;temp=0 mark=0 a=readNext(pTxt) elif a=: begin=2 temp+=time a=readNext(pTxt) elif a=-: a=pTxt.read(1) if a in ,|: if a=|: print | is too close to Number !n,pTxt.tell(),pTxt.read(1) time+=temp liftup(pMid,time,ToneFore) begin=0;temp=0 mark=0 a=readNext(pTxt) elif a=*: SetVolume(pMid,TrackNumn-1,H) ToneFore=1 a=readNext(pTxt) elif a=,: SetVolume(pMid,TrackNumn-1,L) ToneFore=1 a=readNext(pTxt) elif a=W: SetChanYin(pMid,TrackNumn-1) ToneFore=1 a=readNext(pTxt) else: print This is not MidiTxt file! pMid.close() pTxt.close() return 0 TrackTail(pMid,TrackEnd) pMid.seek(TrackBeginn) SetNumOrder(pMid,TrackEndn-TrackBeginn-4) pMid.seek(TrackEndn) SetTrackNum(pMid,track,n) pMid.close() pTxt.close() print nOK!-LOUPEN以上文件存为midi.py。#test.py:import midii=1z=j=int(input(How many tracks?)while i=j: tmp=input(The Instrument number is:) z.append(int(tmp) i+=1midi.main(z)以上文件存为test.py。以下为mid文本范例【赛马】: 1| 6. 3E 5E | 6. 3E 5E | 6. 3E 5E | 6. 3E 5E | 6E 5E 3E 5E 6E 5E 3E 5E | 6E 5E 3E 5E 6E 5E 3E 5E | 6Y 5E 6E 6Y 5E 6E | 6Y 5E 6E 6Y 5E 6E | .6Y 3Y 1Y .6Y | 3Y 6Y 5Y 3Y | 2E 3E 2E 1E 2E 3E 2E 1E | 2E 3E 2E 1E 2E 3E 2E 1E | .6Y 3Y 1Y .6Y | 3Y 6Y 5Y 3Y | 2E 3E 2E 1E 2E 3E 2E 1E | 2E 3E 2E 1E 2E 3E 2E 1E | 2. .6E 1E | 2. .6E 1E | 2. .6E 1E | 2. .6E 1E | 2E 3E 2E 1E 2E 3E 2E 1E | 2E 3E 2E 1E 2E 3E 2E 1E | 2Y 1E 2E 2Y 1E 2E | 2Y 1E 2E 2Y 1E 2E | .6 6 | 5 3 | 2 5 |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3E 5E 6E 1E 6E 5E | 3E 2E 3E 5E 6E 1E 6E 5E | 3E 5E 3E 2E 1Y 3Y | .6. 3E 6E | 1Y 6Y 6Y 3Y | 1Y 6Y 6Y 3Y | 1Y 6Y 6Y 3

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论