练习要点:
1、使用 class 自定义数据类型
2、使用 fysy.file 写文件
3、使用 raw.tostdcall 定义回调函数
代码:
import win.ui;
/*DSG{
{*/
var winform = win.form(text="aardio form";right=759;bottom=469)
winform.add(
button={cls="button";text="开始录音";left=348;top=94;right=562;bottom=214;z=1};
button2={cls="button";text="停止录音";left=351;top=276;right=563;bottom=386;z=2};
edit={cls="edit";left=141;top=118;right=331;bottom=362;edge=1;multiline=1;z=3}
)
/*}}*/
import fsys.file
import bass
MyRecordProc=function(handle,buffer,length,user){
hfile.writeBuffer(buffer,length)
return true;
}
class wavhead{
byte riff[4]="RIFF";
int len=123;
byte cWavFmt[8];
int dwHdrLen=0;
word wFormat=0;
word wNumChannels=0;
int dwSampleRate=0;
int dwBytesPerSec=0;
word wBlockAlign=0;
word wBitsPerSample=0;
byte cData[4];
int dwDataLen=0;
}
winform.button.oncommand = function(id,event){
hfile=fsys.file("C:\Users\Administrator\Desktop\a.wav","w+")
var a = wavhead()
hfile.write(a)
winform.edit.log("文件句柄",hfile?tostring(hfile):"null" ,'\r\n' )
init=::Bass.BASS_RecordInit(-1)
winform.edit.log("初始化",init?"成功":"失败",'\r\n' )
hRecord=::Bass.BASS_RecordStart(44100,2,0,raw.tostdcall(MyRecordProc,"int(addr,ptr,int,int)"),0)
winform.edit.log("录音句柄",hRecord:"null",'\r\n' )
}
winform.button2.oncommand = function(id,event){
::Bass.BASS_ChannelStop(hRecord)
::Bass.BASS_RecordFree()
::Bass.BASS_Free()
var length = hfile.size()-46
var a = wavhead()
a.riff = 'RIFF';
a.len = 36;
a.cWavFmt = 'WAVEfmt ';
a.dwHdrLen = 16;
a.wFormat = 1;
a.wNumChannels = 2;
a.dwSampleRate = 44100;
a.wBlockAlign = 4;
a.dwBytesPerSec = 176400;
a.wBitsPerSample = 16;
a.cData = 'data';
a.dwDataLen=length
hfile.seek("set")
hfile.write(a)
hfile.close()
win.msgbox("录音完毕")
}
winform.show();
win.loopMessage();