日期:2008-05-20  浏览次数:20417 次

关键字: 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