首页 > 编程 > .NET > 正文

VB.NET 实现DirectSound9 (5) DS3D2

2024-07-10 13:00:51
字体:
来源:转载
供稿:网友


关键字: vb .net directx directsound 3d 作者:董含君

经过一下午的奋战,走了若干弯路,终于可以播放3d声音效果了

感动 ~ ~

大体按照下列的步骤来:

1 关联设备 setcooperativelevel
2 设置3d硬件效果 dsoundhelper.guid3dalgorithmhrtffull
3 格式要求 soundformat 必须是单声道,不能是立体声
4 主要缓冲区描述
5 创建listenner
6 辅助缓冲区读取wav
7 创建3d缓冲区
8 播放
9 控制空间位置,以及设置多普勒效应因子,衰减因子
10 停止播放

以下代码注释比较全,顺序比微软的例子简单不少

由于csdn的blog不能上传贴图以及rar,只能帖代码了,有两个button 一个picturebox,还有若干label


==================================================================================

imports microsoft.directx
imports microsoft.directx.directsound
imports system.drawing
imports system.drawing.graphics


public class form1
inherits system.windows.forms.form

dim dev as device '设备
dim buff as buffer '主要缓冲
dim sbuff as secondarybuffer '二级缓冲
dim buff3d as buffer3d '3d缓冲
dim descbuff as bufferdescription '缓冲区描述
dim buff3dset as buffer3dsettings '3d缓冲设置
dim listenner as listener3d '听众
dim listennerset as listener3dsettings '听众设置
dim pic as graphics
dim bmp as bitmap
const fn = "g:/media/wav/rod2m.wav" '要播放的文件,必须是单声道
#region " windows 窗体设计器生成的代码 "

public sub new()
mybase.new()

'该调用是 windows 窗体设计器所必需的。
initializecomponent()

'在 initializecomponent() 调用之后添加任何初始化

end sub

'窗体重写 dispose 以清理组件列表。
protected overloads overrides sub dispose(byval disposing as boolean)
if disposing then
if not (components is nothing) then
components.dispose()
end if
end if
mybase.dispose(disposing)
end sub

'windows 窗体设计器所必需的
private components as system.componentmodel.icontainer

'注意: 以下过程是 windows 窗体设计器所必需的
'可以使用 windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
friend withevents button1 as system.windows.forms.button
friend withevents button2 as system.windows.forms.button
friend withevents picturebox1 as system.windows.forms.picturebox
friend withevents label1 as system.windows.forms.label
friend withevents label2 as system.windows.forms.label
friend withevents label3 as system.windows.forms.label
friend withevents label4 as system.windows.forms.label
friend withevents textbox1 as system.windows.forms.textbox
friend withevents textbox2 as system.windows.forms.textbox
friend withevents textbox3 as system.windows.forms.textbox
friend withevents textbox4 as system.windows.forms.textbox
friend withevents label5 as system.windows.forms.label
friend withevents textbox5 as system.windows.forms.textbox
friend withevents label6 as system.windows.forms.label
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
me.button1 = new system.windows.forms.button
me.button2 = new system.windows.forms.button
me.picturebox1 = new system.windows.forms.picturebox
me.label1 = new system.windows.forms.label
me.label2 = new system.windows.forms.label
me.label3 = new system.windows.forms.label
me.label4 = new system.windows.forms.label
me.textbox1 = new system.windows.forms.textbox
me.textbox2 = new system.windows.forms.textbox
me.textbox3 = new system.windows.forms.textbox
me.textbox4 = new system.windows.forms.textbox
me.label5 = new system.windows.forms.label
me.textbox5 = new system.windows.forms.textbox
me.label6 = new system.windows.forms.label
me.suspendlayout()
'
'button1
'
me.button1.location = new system.drawing.point(24, 8)
me.button1.name = "button1"
me.button1.size = new system.drawing.size(88, 32)
me.button1.tabindex = 0
me.button1.text = "初始化"
'
'button2
'
me.button2.location = new system.drawing.point(24, 48)
me.button2.name = "button2"
me.button2.size = new system.drawing.size(88, 32)
me.button2.tabindex = 1
me.button2.text = "播放"
'
'picturebox1
'
me.picturebox1.borderstyle = system.windows.forms.borderstyle.fixedsingle
me.picturebox1.location = new system.drawing.point(152, 48)
me.picturebox1.name = "picturebox1"
me.picturebox1.size = new system.drawing.size(552, 432)
me.picturebox1.tabindex = 2
me.picturebox1.tabstop = false
'
'label1
'
me.label1.location = new system.drawing.point(8, 104)
me.label1.name = "label1"
me.label1.tabindex = 3
me.label1.text = "多普勒效应0~10"
'
'label2
'
me.label2.location = new system.drawing.point(8, 160)
me.label2.name = "label2"
me.label2.tabindex = 4
me.label2.text = "衰减因子 0~10"
'
'label3
'
me.label3.location = new system.drawing.point(8, 216)
me.label3.name = "label3"
me.label3.tabindex = 5
me.label3.text = "最大距离 0~100"
'
'label4
'
me.label4.location = new system.drawing.point(8, 272)
me.label4.name = "label4"
me.label4.tabindex = 6
me.label4.text = "最小距离 0~100"
'
'textbox1
'
me.textbox1.location = new system.drawing.point(24, 128)
me.textbox1.name = "textbox1"
me.textbox1.tabindex = 7
me.textbox1.text = "0.0"
'
'textbox2
'
me.textbox2.location = new system.drawing.point(24, 184)
me.textbox2.name = "textbox2"
me.textbox2.tabindex = 8
me.textbox2.text = "0.0"
'
'textbox3
'
me.textbox3.location = new system.drawing.point(24, 240)
me.textbox3.name = "textbox3"
me.textbox3.tabindex = 9
me.textbox3.text = "0.9"
'
'textbox4
'
me.textbox4.location = new system.drawing.point(24, 296)
me.textbox4.name = "textbox4"
me.textbox4.tabindex = 10
me.textbox4.text = "20.0"
'
'label5
'
me.label5.location = new system.drawing.point(8, 328)
me.label5.name = "label5"
me.label5.tabindex = 11
me.label5.text = "y轴"
'
'textbox5
'
me.textbox5.location = new system.drawing.point(24, 352)
me.textbox5.name = "textbox5"
me.textbox5.tabindex = 12
me.textbox5.text = "0.0"
'
'label6
'
me.label6.location = new system.drawing.point(152, 16)
me.label6.name = "label6"
me.label6.size = new system.drawing.size(424, 24)
me.label6.tabindex = 13
me.label6.text = "label6"
'
'form1
'
me.autoscalebasesize = new system.drawing.size(6, 14)
me.clientsize = new system.drawing.size(720, 501)
me.controls.add(me.label6)
me.controls.add(me.textbox5)
me.controls.add(me.label5)
me.controls.add(me.textbox4)
me.controls.add(me.textbox3)
me.controls.add(me.textbox2)
me.controls.add(me.textbox1)
me.controls.add(me.label4)
me.controls.add(me.label3)
me.controls.add(me.label2)
me.controls.add(me.label1)
me.controls.add(me.picturebox1)
me.controls.add(me.button2)
me.controls.add(me.button1)
me.name = "form1"
me.text = "form1"
me.resumelayout(false)

end sub

#end region

private sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
'''加载的时候初始化一下picturebox,把它变成黑色(没有颜色)
picturebox1_doubleclick(0, nothing)
end sub

sub initdirectsound()
''加载directsound设备以及读取wav文件
'设置3d模式
dim dsmode as guid
dsmode = dsoundhelper.guid3dalgorithmhrtffull '由于为了演示3d效果,所以用最高模式
'关联窗体
dev = new device
dev.setcooperativelevel(me.handle, cooperativelevel.priority)
''加载wav '''注释掉了,原来测试用的
'dim tmpdesc as new bufferdescription
'tmpdesc.guid3dalgorithm = dsmode
'tmpdesc.control3d = true
'sbuff = new secondarybuffer(fn, tmpdesc, dev)

'格式有比较严格的限制
dim fmt as new waveformat
fmt.formattag = waveformattag.pcm
fmt.channels = 2
fmt.samplespersecond = 22050
fmt.bitspersample = 16
fmt.blockalign = cshort(fmt.bitspersample / 8 * fmt.channels)
fmt.averagebytespersecond = fmt.samplespersecond * fmt.blockalign
'创建描述
descbuff = new bufferdescription
'descbuff.controlvolume = true
descbuff.control3d = true
'descbuff.globalfocus = true
'descbuff.staticbuffer = true
'descbuff.locateinhardware = true
descbuff.primarybuffer = true
descbuff.format = fmt
'descbuff.guid3dalgorithm = dsmode
'''以上可以自己去掉注释尝试一下,某些时候当primary打开的时候,其他的不能用,有冲突,尤其是后面几个
'创建主缓冲
try
'''捕获一下,防止descript的描述不正确(就是参数设置的不对)
buff = new buffer(descbuff, dev)
catch ex as exception
msgbox(ex.message)
end '不对的话,就没有必要继续了
end try
'创建听众
listenner = new listener3d(buff)
listennerset = listenner.allparameters '使用主缓冲听众的设置

''wav
dim tmpdesc2 as new bufferdescription
tmpdesc2.guid3dalgorithm = dsmode
tmpdesc2.control3d = true
''' 这里必须借助辅助缓冲给主缓冲提供实例,不知道微软怎么想的,也可能是我学艺不精,怎么尝试都要这样用
buff = new secondarybuffer(fn, tmpdesc2, dev)

'创建3d缓冲区
buff3d = new buffer3d(buff)
buff3dset = buff3d.allparameters '使用默认的设置
buff3dset.mode = mode3d.headrelative '改变一个参数
buff3d.allparameters = buff3dset '应用新的设置

msgbox("已经读取" + fn)
'ok,初始化完成

end sub

private sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click
buff.play(0, bufferplayflags.looping)
end sub

private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
initdirectsound()
end sub

private sub picturebox1_mousemove(byval sender as object, byval e as system.windows.forms.mouseeventargs) handles picturebox1.mousemove
''' 当鼠标左键按下去的时候有效,我可不希望鼠标经过就改变
if e.button <> mousebuttons.left then exit sub
dim r = new rectangle(e.x, e.y, 3, 3)

pic = picturebox1.creategraphics()
me.text = e.x.tostring + "," + e.y.tostring
pic.drawimage(bmp, 1, 1)
pic.drawellipse(new pen(color.red), r)
setsoundpos(convert.tosingle(e.x), convert.tosingle(textbox5.text), convert.tosingle(e.y))
'''这里坐标变换一下,因为y轴向上,我们需要的是远近,所以负值给z轴(从上面俯视)
end sub

private sub picturebox1_doubleclick(byval sender as object, byval e as system.eventargs) handles picturebox1.doubleclick
''' 清除画板,只需双击一下,免得太乱
bmp = new bitmap(400, 300)
picturebox1.backgroundimage = bmp
end sub
sub setsoundpos(byval x as single, byval y as single, byval z as single)
'''''''''用户改变位置以及速度之后,对buff进行调整的代码
''' 很简单,就是更改参数而已
''' 定义域在-1 到 1 之间
dim pos as vector3
dim speed as vector3

pos.x = (x - picturebox1.width / 2) / 100
pos.y = y / 100
pos.z = (z - picturebox1.height / 2) / 100

speed.x = 1
speed.y = 1
speed.z = 1

buff3d.position = pos
buff3d.velocity = speed

label6.text = "相对坐标:(x,y)=" + x.tostring + "," + z.tostring
end sub
end class

================================================================================

这里对y轴的操作不多,我无法用picturebox描述3d的效果,也许学会了d3d就可以比较形象的描述了

而且对于多普勒也没有过多的使用(我的音箱很难分辨),最好用那种汽车的单一的声音最好

还有速度的改变需要检测鼠标两次的位置判断(需要研究公式的,懒)否则多普勒不够真实

对于表现directsound3d的性能,这个例子是不够完善的,但是对于介绍使用ds3d的步骤,我想还是

说得过去的.其他的部分大家自己完善就可以了.

下次就是最后的部分了,利用directsound进行混音,实现特效.

倒是想过自己做一个封装,用于声音引擎,但是似乎牵扯到3d的地方必须了解d3d,所以ds一般都包含在游戏引擎里面

单独拿出来就没有太大的意义了(除非不使用3d)



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表