龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > VB开发 >

利用VB测声卡

时间:2009-12-30 15:42来源:未知 作者:admin 点击:
分享到:
在一个多媒体应用程序中,如果涉及对声音的播放与操作,那么我们就有必要先对用户系统中的声卡及真功能进行一下测试。幸好有VB,所以我们要实现这些功能并不用费多大力气(也就
在一个多媒体应用程序中,如果涉及对声音的播放与操作,那么我们就有必要先对用户系统中的声卡及真功能进行一下测试。幸好有VB,所以我们要实现这些功能并不用费多大力气(也就是吃顿饭的力气),在下面的程序中我们将利用VB调用两个windowsApi函数--Waveoutgetnumdevs()和Waveoutgetdev-capS()来访问设备驱动程序,获取有关信息,实现上述目的。OK,Let'sGo!一、我们先要捡测一下声卡是否存在
  1.新建一工程并添加模块Module1.bas,在其声明部分加入如下代码:
  DeclareFunctionWaveoutgetnumdevsLib"Winmm.Dll"()asLong
  PublicConstMb_ok=&H40
  2.在窗体上添加一个命令按钮cmdtest,设置Caption的属性为“测试声卡”
  3.在窗体的通用声明部分加入一函数testcard,代码如下:
  PublicFunctionTestcard()AsBoolean
  DimYAslong
  DimFindAsStringFind=“FiedSoundBlasterCard"
  Y=Waveoutgetnumdevs()
  IfY>0Then
  Testcard=True
  Msgbox"啥啥,我找到你了--声卡!",Mb_ok,Find
  Else
  Testcard=Falsc
  Msgbox"未发现设备",Mb_ok,Find
  Endif
  EndFunction
  4.在命令按钮的单击事件中加入代码:
  PrivatesubCmdtest_Click()
  DimExistentAsBoolean
  Existent=Testcard
  Endsub
  现在你可以运行这个程序试试看了,它会检测你的系统中是否有声卡的存在。二、测试声卡的功能
  既然已经发现了声卡的存在,接下来就要测试一下它的功能。为什么?举个例子来说,老式声卡支持的采样率和位分辨率是远不及现在声卡的,如果你试图用只有8位分辨率和22.05KHz采样率的声卡来播放44.1KHz、16位立体声的声音文件,嘿嘿……有你好看(其实也没啥大不了的)。好,你大胆的往下看。
  1.在窗体上加入picturebox控件picture1。
  2.在Module1.bass的声名节中加入代码:
  DeclareFunctionWaveoutgetdevcapsLib"Winmm.dll"Alias"Waveoutgetdevcapsa"(ByvaIUdcviceidAsLong,LpcapsAsWaveOutcaps,ByvaIUsizeAsLong)AsLong
  '参数1指定被测设备。由于一台PC上装有几个音频设备是完全可能的,所以Windows自动给每个设备编号,第一个可用设备号为0。
  '参数2是一个Waveoutcaps结构的指针。
  '多数3是第二个参数的大小。
  PublicConstMaxpnamelen=32
  PublicConstWave_Format_1m08=&H1
  PublicConstWavp_Format_1ml6=&H4
  PublicConstWave_Format_1s08=&H2
  PublicConstWave_Format_1sl6=&H8
  PublicConstWavc_Format_2m0B=&H1O
  PublicConstWave_Format_2m16=&H40
  PublicConstWave_Format_2s08=&H20
  PublicConstWave_Format_2s16=&H80
  PublicConstWave_Format_4m08=&H100
  PublicConstWave_Format_4ml6=&H400
  PublicConstWave_Format_4s08=&H200
  PublicConstWave_Format_4s16=&H800
  PublicConstWavecaps_Lrvolume=&H8
  PublicConstWavecaps_Pitch=&H1
  PublicConstWavecaps_Playbackrate=&H2
  PublicConstWavecaps_Sync=&H10
  PublicConstWavecaps_Volume=&H4
  
  TypeWaveoutCaps
  WmidAsInteger'设备驱动程序厂商标识
  WpidAsInteger'声卡厂商标识
  VdriverversionAsLong'驱动程序版本号,高字节为主版本号,低字节为次版本号
  SzpnameAsString*Maxpnamelen'产品名称
  DwformatsAsLong'支持的wave格式,每一位代表一种格式
  WchannelsAsInteger'返回整型值1(单声道)或2(立体声)
  DwsupportAsLong'设备支持的扩展输出功能
  EndType
  3.在窗体的声明节内增加两个函数:
  '函数listwaveformat检测波形音频支持的格式
  PublicFunctionListwaveformat(AboutwaveAslong)AsString
  DimWaveformatAsString
  SelectCaseAboutwave
  CaseWave_Format_1m08
  Waveformat="11.025khz,Mono,8bit,11kb/Ps"
  CaseWave_Format_1m16
  Waveformat="11.025khz,Mono,16bit,22kb/Ps"
  CaseWave_Format_1s08
  Waveformat="11.025khz,Stereo,8bit,22kb/Ps"
  CaseWave_Format_1s16
  Waveformat="11.025khz,Stereo,16bit,43kb/Ps"
  Casewave_Format_2m08
  Waveformat="22.05khz,Mono,8bit,22kb/Ps"
  CaseWavc_Format_2m16
  Waveformat="22.05khz.Mono,16bit,43kb/Ps"
  CaseWave_Format_2s16
  Waveformat="22.05khz,Stereo,8bit,43kb/Ps"
  CaseWave_Format_2s16
  Waveformat="22.05khz,Stereo,16bit,86kb/Ps"
  CaseWave_Format_4m08
  Waveformat="44.1khz,Mono,8bit,43kb/Ps"
  CaseWave_Format_4m16
  Wavcformat="44.lkhz,Mono,16bit,86KB/Ps"
  CaseWave_Format_4s08
  Waveformat="44.lkhz,Stereo,8bit,86kb/Ps"
  CaseWavc_Format_4s16
  Waveformat="44.lkhz.Stereo,16bit,172kb/Ps"
  EndSelect
  Listwaveformat=Waveformat
  EndFunction
  '函数Listwavesupport检测设备支持的扩展输出功能
  PublicFunctionListwavesupport(AboutwaveAslong)AsString
  DimWavefunAsString
  SclectCaseAboutwave
  CaseWavecaps_Pitch
  Wavefun="SupportPitch"
  CascWavecaps_Playbackrate
  Wavefun="SupportPlayback"
  CaseWavecaps_Volume
  Wavefun="SupportVolumeControl"
  CsaeWavecaps_Lrvolume
  Wavefun="SupportLeft-RightChannals"
  CsaeWavecaps_sync
  Wavcfun="SupportSynchronization"
  EndSelect
  Listwavesupport=Wavefun
  EndFunction
  4.修改cmdtest_Click事件的代码为:
  PrivateSubCmdtest_Click()
  DimExistentAsBoolean
  DimConsequenceAslong
  DimReturncapsAsWaveoutcaps
  DimRainverAsLong
  DimLesservcrAslong
  DimPnameAsString*32
  DimAboutwaveAslong
  DimChannelAsString*2
  DimIAslnteger
  Existent=Testcard
  IfExistentThen
  Consequence=Waveoutgetdevcaps(0,Returncaps,Len(Returncaps))IfConsequence=0Then
  Mainver=Returncaps.Vdriverversion256
  Lesserver=Returncaps.VdriverversionMod256
  '因为API在返回Returncaps.szpname时在返回值与空格之间会插入一个空的终止符,用Rtrim$会返回一个0终止字符串,所以我们采用Instr Left$的方法.
  Pname=Left$(Returncaps.Szpname,Instr(Returncaps.Szpname,Chrr$(0))-1)
  Channe1=Str$(Returncaps.Wchannels)
  Picture1.Print"产品名称:";Pname
  Picture1.Print"产品Id:";Returncaps.Wpid
  Picture1.Print"驱动程序Id:";Returncaps.Wrmid
  Picture1.Print"驱动程序版本:";Mainver;".";LesserverPicture1.Print"输出声道:";Channel
  Picture1.Print"支持格式列表:"
  ForI=0TO11
  IfReturncaps.DwformatsAnd(2^I)Then
  Picture1.PrintListwaveformat(2^I)
  Endif
  NextI
  Picture1.Print"扩展输出功能列表:"
  Forl=0To4
  IfReturncaps.DwsupportAnd(2^I)Then
  Picture1.PrintListwavesupport(2^I)
  Endif
  NextI
  Endif
  Else
  End
  Endif
  EndSub
  5.为Form_load事件加入代码:
  PrivateSubForm_Load()Picture1.ClsEndSub本程序在Win95(osr2)、VB5企业版下调试通过,在win3.2下仅仅两个API函数略有改变,照猫画虎即可。
  好了,工作已经全部做完了。现在你要做的只是按下F5,我也要随风而去了,各位看官后会有期,隐也。->

精彩图集

赞助商链接