如何把一些数据转换成MIDI音乐播放出来?

曾经听说过有个日本人把基因组密码的数据转换成电脑音乐,播放出来居然有奇特效果。没有听到过,为何不自己试下?

基因组密码数据我是搞不懂,MIDI也没有接触过,delphi还没有丢生,一切从头开始开始吧。

首先想到的是如何让电脑按自己给的数据发出声音。

网上一搜,找到一个叫MIDIGen的开源程序,正好可以学习。


显然,点1,2部分会发出声音,看它是怎么做的:

1.播放音符:

 //note buttons
procedure TForm1.Button6Click(Sender: TObject);
var
   caller:TButton;
   oct,nv:Integer;
   ni:string;

begin
caller:=TButton(Sender);
SetComponent;

//stop random
StopRand;

//button tag identifies note
nv:=caller.Tag;
//get note string
ni:='mg'+NoteNames[nv];
//octave
oct:=OctTB.Position;

//note value
nv:=nv+(12*oct);

NotePan.Caption:=IntToStr(nv)+' : '+ni+' : '+caller.Caption+IntToStr(oct);

ni:='';
//display percussion name?
if ChList.ItemIndex=9 then
   begin
   if (nv>34) and (nv<82) then
      ni:=IntToStr(nv+93)+' : '+Instruments[nv+93];

   NotePan2.Caption:=ni;
   end;

MidiGen1.PlayNote(nv);
end;

呵呵关键是这句,不用说,nv就是产生的音符。

接下来只要看midigen1是个什么东东,怎么用就可以了?

跟踪发现setcomponent是用来设置midigen1的。

procedure SetComponent;
begin
Form1.MidiGen1.Modulation:=100-Form1.ModTB.Position;   //音调
Form1.MidiGen1.Chorus:=100-Form1.ChorTB.Position;      //多重音
Form1.MidiGen1.Reverb:=100-Form1.RevTB.Position;       //混响
Form1.MidiGen1.PitchBend:=-Form1.PBTB.Position*10;
Form1.MidiGen1.PBDelay:=100-Form1.PBDelayTB.Position;
Form1.MidiGen1.PBDuration:=100-Form1.PBDurationTB.Position;
Form1.MidiGen1.Sustain:=Form1.SusCB.Checked;
Form1.MidiGen1.Loops:=Form1.LoopsUD.Position;
Form1.MidiGen1.Octave:=Form1.OctTB.Position;
Form1.MidiGen1.Duration:=Form1.DurUD.Position*10;
Form1.MidiGen1.Instrument:=TMGInstrument(Form1.InstList.ItemIndex);  //这个是设置乐器的,可以选择多种乐器效果,购强的啊。
Form1.MidiGen1.Volume:=100-Form1.VolTB.Position;
Form1.MidiGen1.Pan:=Form1.PanTB.Position;
Form1.MidiGen1.Channel:=TMGChannel(Form1.ChList.ItemIndex);

end;

经过上述设置之后,MIDI的设备参数设置完成,就可以直接PlayNode了。

经查看,MidiGen1 是这个类TMidiGen的实例,TMidiGen是怎么实现的,源码有,和我只想播放MIDI关系不大,就不需要深究了。

 

 

posted on 2008-09-27 15:35  巴不得飞  阅读(520)  评论(0编辑  收藏  举报