VB.NET实现DirectSound9 (6) 声音特效

关键字: VB.NET DirectSound 9 声音特效 回声Echo 作者:董含君
转载请注明来自:http://blog.csdn.net/a11s

实际上这个就是传说的混音,微软把各种效果添加起来变成了混音程序
严格的讲,这个算作声音的特效.

微软的DirectX的例子还有一个特点,就是爱把简单的问题复杂化,我用了大量的时间搞清出中间的各种对象怎样协作的,明白之后再来考虑各自的用途.虽然相信微软这么做也许是有道理的,但是不适合初学者.经过尝试整理出了以下结果.

首先大体介绍一下步骤

加载
1 需要设备以及一个缓冲用于播放声音(具体请参见前文的介绍)

2 描述这个缓冲区的时候,记得加上ControlEffects=true的属性(跟ControlVolume是一个道理)

读取
3 new SecondaryBuffer读取文件,这个文件如果太小,则会出现错误.无法体现效果,需要try catch一下

设置效果
4 设置效果需要介绍一些对象,稍后再说

5 播放,同时可以进行相关属性的操作

6 特效的移除
7 停止播放

由此可见,大体步骤跟3D音效类似,只不过中间多了效果设置而已

以下是对象的介绍.

1 首先需要了解,一个声音(Buffer)可以同时应用多种效果
这样就产生了一个问题,如何才能设置我们的SecondaryBuffer有这些效果

DirectSound是用一个数组来表示的,添加一个效果,这个数组就有一个元素.两个效果,这个数组就有两个元素.
依次类推.

我们的SecondaryBuffer应用的时候,是直接将这个数组一同传递给SecondaryBuffer的.
SB.SetEffects(effd)

2 不要以为什么特效都要自己做(天!千万别这么想,否则需要专门研究计算机声学的)
微软DirectSound内置了几种特效,可以参见微软SDK最后的那个例子,也可以用
DSoundHelper.StandardEchoGuid 类似的这种形式列出

3 每种特效的属性都是一个结构体(也可以理解为对象).我们有很多特效的对象
比如 Dim echo As EchoEffect

4 每种特效对象的属性是一个结构体(需要跟上面区分开来)
比如RchoEffect对象不能直接用这种方法修改 echo.WetDryMix 属性
需要利用一个结构体来完成echo属性的存储,这个结构体叫做
EffectsEcho (微软真是作孽啊,名字取得都这么像,很难区分的)
Dim eff As New EffectsEcho '''结构
eff = echo.AllParameters
eff.WetDryMix = 0
eff.Feedback = 0
eff.LeftDelay = 1
eff.RightDelay = 1
eff.PanDelay = 0
设置完成之后,一次性传给特效对象echo
echo.AllParameters = eff

5 Buffer播放的时候可以可以调整特效属性的,只要修改echo.allparameters就可以了

知道中间怎么做的了,然后看代码

代码分为几个部分,主要还是为了容易理解
第一步,初始化设备(实在太经典了,就不用看了)
第二步,加载wav(就是多了一个controlEffect,别的都一样)
第三步,设置音效(单独看)
第四步,播放(简单一个play)
第五步,停止播放

Imports Microsoft.DirectX.DirectSound

Public Class Form1

Inherits System.Windows.Forms.Form

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 TextBox1 As System.Windows.Forms.TextBox

Friend WithEvents Button1 As System.Windows.Forms.Button

Friend WithEvents Button2 As System.Windows.Forms.Button

Friend WithEvents Button3 As System.Windows.Forms.Button

Friend WithEvents Button4 As System.Windows.Forms.Button

Friend WithEvents Button5 As System.Windows.Forms.Button

  1<system.diagnostics.debuggerstepthrough()> Private  Sub  InitializeComponent() 
  2
  3Me  .TextBox1 =  New  System.Windows.Forms.TextBox 
  4
  5Me  .Button1 =  New  System.Windows.Forms.Button 
  6
  7Me  .Button2 =  New  System.Windows.Forms.Button 
  8
  9Me  .Button3 =  New  System.Windows.Forms.Button 
 10
 11Me  .Button4 =  New  System.Windows.Forms.Button 
 12
 13Me  .Button5 =  New  System.Windows.Forms.Button 
 14
 15Me  .SuspendLayout() 
 16
 17' 
 18
 19'TextBox1 
 20
 21' 
 22
 23Me  .TextBox1.Location =  New  System.Drawing.Point(104, 40) 
 24
 25Me  .TextBox1.Name = "TextBox1" 
 26
 27Me  .TextBox1.Size =  New  System.Drawing.Size(160, 21) 
 28
 29Me  .TextBox1.TabIndex = 2 
 30
 31Me  .TextBox1.Text = "g:\media\wav\rod2.wav" 
 32
 33' 
 34
 35'Button1 
 36
 37' 
 38
 39Me  .Button1.Location =  New  System.Drawing.Point(16, 8) 
 40
 41Me  .Button1.Name = "Button1" 
 42
 43Me  .Button1.Size =  New  System.Drawing.Size(80, 24) 
 44
 45Me  .Button1.TabIndex = 3 
 46
 47Me  .Button1.Text = "init" 
 48
 49' 
 50
 51'Button2 
 52
 53' 
 54
 55Me  .Button2.Location =  New  System.Drawing.Point(16, 40) 
 56
 57Me  .Button2.Name = "Button2" 
 58
 59Me  .Button2.Size =  New  System.Drawing.Size(80, 24) 
 60
 61Me  .Button2.TabIndex = 4 
 62
 63Me  .Button2.Text = "load" 
 64
 65' 
 66
 67'Button3 
 68
 69' 
 70
 71Me  .Button3.Location =  New  System.Drawing.Point(16, 72) 
 72
 73Me  .Button3.Name = "Button3" 
 74
 75Me  .Button3.Size =  New  System.Drawing.Size(80, 24) 
 76
 77Me  .Button3.TabIndex = 5 
 78
 79Me  .Button3.Text = "play" 
 80
 81' 
 82
 83'Button4 
 84
 85' 
 86
 87Me  .Button4.Location =  New  System.Drawing.Point(16, 104) 
 88
 89Me  .Button4.Name = "Button4" 
 90
 91Me  .Button4.Size =  New  System.Drawing.Size(80, 24) 
 92
 93Me  .Button4.TabIndex = 6 
 94
 95Me  .Button4.Text = "stop" 
 96
 97' 
 98
 99'Button5 
100
101' 
102
103Me  .Button5.Location =  New  System.Drawing.Point(280, 40) 
104
105Me  .Button5.Name = "Button5" 
106
107Me  .Button5.Size =  New  System.Drawing.Size(88, 24) 
108
109Me  .Button5.TabIndex = 7 
110
111Me  .Button5.Text = "Echo" 
112
113' 
114
115'Form1 
116
117' 
118
119Me  .AutoScaleBaseSize =  New  System.Drawing.Size(6, 14) 
120
121Me  .ClientSize =  New  System.Drawing.Size(400, 269) 
122
123Me  .Controls.Add(  Me  .Button5) 
124
125Me  .Controls.Add(  Me  .Button4) 
126
127Me  .Controls.Add(  Me  .Button3) 
128
129Me  .Controls.Add(  Me  .Button2) 
130
131Me  .Controls.Add(  Me  .Button1) 
132
133Me  .Controls.Add(  Me  .TextBox1) 
134
135Me  .Name = "Form1" 
136
137Me  .Text = "Form1" 
138
139Me  .ResumeLayout(  False  ) 
140
141End  Sub 
142
143#  End  Region 
144
145Dim  Dev  As  Device 
146
147Dim  PB  As  Buffer 
148
149Dim  SB  As  SecondaryBuffer 
150
151Dim  Desc1  As  BufferDescription 
152
153Dim  desc2  As  BufferDescription 
154
155Dim  eff  As  EffectDescription 
156
157Private  Structure  EffectInfo 
158
159Public  description  As  EffectDescription 
160
161Public  EffectSettings  As  Object 
162
163Public  Effect  As  Object 
164
165End  Structure 
166
167Private  Sub  Button1_Click(  ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click 
168
169'''加载DirectSound 
170
171Dev =  New  Device 
172
173Dev.SetCooperativeLevel(  Me  , CooperativeLevel.Priority) 
174
175Me  .Text = "init ok" 
176
177End  Sub 
178
179Private  Sub  Button2_Click(  ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button2.Click 
180
181'''读取wav 
182
183Desc1 =  New  BufferDescription 
184
185Desc1.ControlEffects =  True 
186
187SB =  New  SecondaryBuffer(TextBox1.Text, Desc1, Dev) 
188
189Me  .Text = "load ok" 
190
191End  Sub 
192
193Private  Sub  Button3_Click(  ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button3.Click 
194
195'''播放啊播放 
196
197If  SB  Is  Nothing  Then  Exit  Sub 
198
199SB.Play(0, BufferPlayFlags.Default) 
200
201Me  .Text = "playing" 
202
203End  Sub 
204
205Private  Sub  Button4_Click(  ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button4.Click 
206
207'''停止播放,释放当前的wav 
208
209SB.Stop() 
210
211SB.Dispose() 
212
213Me  .Text = "disposed" 
214
215End  &lt;SPAN</system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus