Vb. NET implementation DIRECTSOUND9 (6) Sound effects

Source: Internet
Author: User
Tags try catch
Keyword: vb.net DirectSound 9 sound effects echo echo Author: Dong Gangjun
Reprint please indicate from: http://blog.csdn.net/a11s

In fact, this is the legendary mix, Microsoft has added a variety of effects into the mix program
Strictly speaking, this is a special effect that counts as sound.

Microsoft's example of DirectX also has one feature, it is love that complicates simple problems, and I spend a lot of time figuring out how the various objects in the middle work together, and then thinking about their uses. Although it may be reasonable to believe that Microsoft does so, it is not suitable for beginners. After trying to sort out the following results.

First, a general introduction to the steps
============================================================
Load
1 requires a device and a buffer to play the sound (see the previous article for details)

2 when describing this buffer, remember to add the Controleffects=true attribute (with ControlVolume is a truth)

Read
3 new SecondaryBuffer Read the file, if the file is too small, you will receive an error. Cannot reflect effect, need try catch up

Setting effects
4 setting effect you need to introduce some objects.

5 playback, while the related properties can be manipulated

6 Removal of effects
7 Stop Playing
=====================================

This shows that the general step is similar to the 3D sound, but the middle of a lot of effect settings just

The following is an introduction to the object.

1 First you need to understand that a sound (Buffer) can apply multiple effects at the same time
This creates the problem of how to set our SecondaryBuffer to have these effects

DirectSound is represented by an array, adding an effect, and the array has an element. Two effects, this array has two elements.
by analogy.

When our secondarybuffer is applied, we pass this array directly to SecondaryBuffer.
SB. Seteffects (EFFD)

2 Do not think that any special effects have to do their own (days! Do not think so, otherwise need to specialize in computer acoustics)
Microsoft DirectSound has several special effects built into it, you can see the last example of the Microsoft SDK, and you can use
Dsoundhelper.standardechoguid similar to this form listed

3 The properties of each effect are a struct (also understood as an object). We have a lot of special effects.
Like Dim Echo as Echoeffect

4 the properties of each effect object are a struct (need to be distinguished from the above)
For example, the Rchoeffect object cannot modify the Echo.wetdrymix property directly in this way
You need to use a struct to complete the memory of the Echo property, which is called
Effectsecho (Microsoft is a wicked man, the name is made so alike, it is difficult to distinguish)
Dim eff as New effectsecho ' ' Structure
EFF = echo. Allparameters
Eff. Wetdrymix = 0
Eff. Feedback = 0
Eff. Leftdelay = 1
Eff. Rightdelay = 1
Eff. Pandelay = 0
Once the settings are complete, pass to the special effects object echo
Echo. Allparameters = eff

5 buffer playback can adjust the properties of special effects, as long as the modification of the echo.allparameters can be

Know how to do it in the middle, and then look at the code

The code is divided into several parts, mainly for easy understanding
The first step is to initialize the device (it's really classic, so don't look at it)
The second step is to load the WAV (just one more controleffect, the other is the same)
Step three, set the sound effects (see alone)
Fourth step, play (simple one plays)
Step fifth, stop playing



Imports Microsoft.DirectX.DirectSound



Public Class Form1

Inherits System.Windows.Forms.Form



#Region "code generated by the Windows forms Designer"



Public Sub New ()

MyBase.New ()



' This call is required by the Windows Forms Designer.

InitializeComponent ()



' Add any initialization after the InitializeComponent () call



End Sub



' Form overrides dispose to clean up the list of components.

Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)

If disposing Then

If not (components are nothing) Then

Components. Dispose ()

End If

End If

Mybase.dispose (disposing)

End Sub



' Required by the Windows Forms Designer

Private Components as System.ComponentModel.IContainer



' NOTE: The following procedure is required by the Windows Forms Designer

' You can use the Windows Forms Designer to modify this procedure.

' Do not modify it using the Code Editor.

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

<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()

Me.textBox1 = New System.Windows.Forms.TextBox

Me.button1 = New System.Windows.Forms.Button

Me.button2 = New System.Windows.Forms.Button

Me.button3 = New System.Windows.Forms.Button

Me.button4 = New System.Windows.Forms.Button

Me.button5 = New System.Windows.Forms.Button

Me.suspendlayout ()

'

' TextBox1

'

Me.TextBox1.Location = New System.Drawing.Point (104, 40)

Me.TextBox1.Name = "TextBox1"

Me.TextBox1.Size = New System.Drawing.Size (160, 21)

Me.TextBox1.TabIndex = 2

Me.TextBox1.Text = "G:\media\wav\rod2.wav"

'

' Button1

'

Me.Button1.Location = New System.Drawing.Point (16, 8)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size (80, 24)

Me.Button1.TabIndex = 3

Me.Button1.Text = "Init"

'

' Button2

'

Me.Button2.Location = New System.Drawing.Point (16, 40)

Me.Button2.Name = "Button2"

Me.Button2.Size = New System.Drawing.Size (80, 24)

Me.Button2.TabIndex = 4

Me.Button2.Text = "Load"

'

' Button3

'

Me.Button3.Location = New System.Drawing.Point (16, 72)

Me.Button3.Name = "Button3"

Me.Button3.Size = New System.Drawing.Size (80, 24)

Me.Button3.TabIndex = 5

Me.Button3.Text = "Play"

'

' Button4

'

Me.Button4.Location = New System.Drawing.Point (16, 104)

Me.Button4.Name = "Button4"

Me.Button4.Size = New System.Drawing.Size (80, 24)

Me.Button4.TabIndex = 6

Me.Button4.Text = "Stop"

'

' Button5

'

Me.Button5.Location = New System.Drawing.Point (280, 40)

Me.Button5.Name = "Button5"

Me.Button5.Size = New System.Drawing.Size (88, 24)

Me.Button5.TabIndex = 7

Me.Button5.Text = "Echo"

'

' Form1

'

Me.autoscalebasesize = New System.Drawing.Size (6, 14)

Me.clientsize = New System.Drawing.Size (400, 269)

ME.CONTROLS.ADD (ME.BUTTON5)

ME.CONTROLS.ADD (Me.button4)

ME.CONTROLS.ADD (Me.button3)

ME.CONTROLS.ADD (Me.button2)

ME.CONTROLS.ADD (Me.button1)

ME.CONTROLS.ADD (Me.textBox1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.resumelayout (False)



End Sub



#End Region

Dim Dev as Device

Dim PB as Buffer

Dim SB as SecondaryBuffer

Dim Desc1 as BufferDescription

Dim DESC2 as BufferDescription

Dim eff as Effectdescription



Private Structure Effectinfo



Public description as Effectdescription

Public Effectsettings as Object

Public Effect as Object

End Structure



Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click

"" Load DirectSound

Dev = New Device

Dev.setcooperativelevel (Me, cooperativelevel.priority)

Me.Text = "Init OK"

End Sub



Private Sub button2_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button2.click

"" Read wav

DESC1 = New bufferdescription

Desc1.controleffects = True

SB = New SecondaryBuffer (TextBox1.Text, DESC1, Dev)

Me.Text = "Load OK"

End Sub



Private Sub button3_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button3.click

"Play it, play it."

If SB is nothing Then Exit Sub

SB. Play (0, Bufferplayflags.default)

Me.Text = "playing"

End Sub



Private Sub button4_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button4.click

"" Stop playback, release the current WAV

SB. Stop ()

SB. Dispose ()

Me.Text = "disposed"

End Sub



Private Sub button5_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button5.click

"Mainly look at this

"My array declares an element that does not seem necessary, but when set, the requirement must be an array, even if the element has only one

"' If more effects are needed, set the corresponding number of elements

Dim effd (0) as Effectdescription ' description collection

Dim eff as New Effectsecho ' structure, because the object cannot set properties individually, it can only be used to indirectly modify

"" Key steps to specify the type of effect

EFFD (0). Guideffectclass = Dsoundhelper.standardechoguid ' ' shows that the first effect is an echo

"" Software, if you use hardware, I do not object to

EFFD (0). Locateinsoftware = True

"A one-time submission to buffer, submitted by an array of

SB. Seteffects (EFFD)

"Declare a special effect object, and don't forget that we need to modify the parameter drop

Dim Echo as Echoeffect

"" To get the current buffer effect

"The 0 that follows, is the index that you pass the effect of the past at once, forget?" That's the number group.

echo = SB. Geteffects (0)

Object can not be directly modified, so use it to indirectly change

EFF = echo. Allparameters

Eff. Wetdrymix = 50

Eff. Feedback = 50

Eff. Leftdelay = 500

Eff. Rightdelay = 500

Eff. Pandelay = 0

"OK, so you can try to replace the values with other data, where Leftdelay and rightdelay are defined in 1~2000,

"You can get the maximum minimum value with the Echo attribute (much more convenient)

Echo. Allparameters = eff



Me.Text = "echo OK"

End Sub

End Class



Okay, copy the past and run it directly.

I used echo as an example to illustrate the problem (estimate this is the most, the effect is most obvious) there are many, you refer to Microsoft's example, and then change the above code can be easily implemented, I will not nonsense.

Next. Recording




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.