vb.net realization DIRECTSOUND9 (5) ds3d2

Source: Internet
Author: User
Tags tostring
Keywords: VB. NET DirectX DirectSound 3D Author: Dong Gangjun

After an afternoon of fighting, took a number of detours, finally can play 3D sound effect

Moved ~ ~

The following steps are generally followed:

1 Associated Equipment SetCooperativeLevel
2 set 3D hardware effect Dsoundhelper.guid3dalgorithmhrtffull
3 format requires Soundformat must be mono, not stereo
4 Main Buffer Description
5 Creating Listenner
6 Secondary buffer Read wav
7 Creating 3D Buffers
8 Play
9 control space position, and set Doppler effect factor, attenuation factor
10 Stop Playing

The following code comments are more complete, and the order is much simpler than the Microsoft example

Because CSDN blog can not upload maps and RAR, can only post code, there are two button a PictureBox, there are several 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 ' device
Dim Buff as buffer ' primary buffer
Dim Sbuff as SecondaryBuffer ' two level buffer
Dim buff3d as Buffer3D ' 3D buffer
Dim Descbuff as bufferdescription ' buffer description
Dim Buff3dset as buffer3dsettings ' 3D buffer setting
Dim Listenner as Listener3D ' listeners
Dim Listennerset as listener3dsettings ' listener settings
Dim Pic as Graphics
Dim BMP as Bitmap
Const FN = "G:\media\wav\rod2m.wav" "The file to play, must be mono
#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 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 = "Initialize"
'
' 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 = "Play"
'
' 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 = "Doppler effect 0~10"
'
' Label2
'
Me.Label2.Location = New System.Drawing.Point (8, 160)
Me.Label2.Name = "Label2"
Me.Label2.TabIndex = 4
Me.Label2.Text = "attenuation factor 0~10"
'
' Label3
'
Me.Label3.Location = New System.Drawing.Point (8, 216)
Me.Label3.Name = "Label3"
Me.Label3.TabIndex = 5
Me.Label3.Text = "Max distance 0~100"
'
' Label4
'
Me.Label4.Location = New System.Drawing.Point (8, 272)
Me.Label4.Name = "Label4"
Me.Label4.TabIndex = 6
Me.Label4.Text = "min Distance 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 axis"
'
' 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
Initialize the PictureBox when loading, turn it Black (no color)
Picturebox1_doubleclick (0, nothing)
End Sub

Sub Initdirectsound ()
' Load DirectSound device and read WAV file
' Set 3D mode
Dim Dsmode as Guid
Dsmode = Dsoundhelper.guid3dalgorithmhrtffull ' because in order to demonstrate 3D effect, use the highest mode
' Association form
Dev = New Device
Dev.setcooperativelevel (Me.handle, cooperativelevel.priority)
' Load wav ' annotation dropped, the original test trial
' Dim Tmpdesc as New bufferdescription
' Tmpdesc.guid3dalgorithm = Dsmode
' Tmpdesc.control3d = True
' Sbuff = New secondarybuffer (FN, Tmpdesc, Dev)

' Format has more stringent restrictions
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
' Create a description
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
The above can take off the annotation oneself to try, some time when primary open, other cannot use, has the conflict, especially after several
' Create primary buffer
Try
"Capture to prevent descript from being incorrectly described (that is, incorrect parameter settings)
Buff = New Buffer (descbuff, Dev)
Catch ex as Exception
MsgBox (ex. Message)
End ' If not, there is no need to continue
End Try
' Create audience
Listenner = New Listener3D (Buff)
Listennerset = Listenner.allparameters ' Use primary buffered audience settings

' WAV
Dim TMPDESC2 as New bufferdescription
Tmpdesc2.guid3dalgorithm = Dsmode
Tmpdesc2.control3d = True
"" Here must use auxiliary buffer to provide an example of the main buffer, do not know what Microsoft think, or maybe I learned not fine, how to try to use this
Buff = New SecondaryBuffer (FN, TMPDESC2, Dev)

' Create a 3D buffer
BUFF3D = New Buffer3D (Buff)
Buff3dset = Buff3d.allparameters ' Use default settings
Buff3dset.mode = mode3d.headrelative ' Change a parameter
Buff3d.allparameters = Buff3dset ' Apply the new settings

MsgBox ("read already" + FN)
' OK, initialization complete

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 picture Box1.mousemove
"When the left mouse button is down, I don't want the mouse to change.
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))
"Here the coordinates change, because the Y axis is up, we need to be near and far, so negative values are given to the z axis (from above)
End Sub

Private Sub Picturebox1_doubleclick (ByVal sender as Object, ByVal e as System.EventArgs) Handles Picturebox1.doubleclick
"Clear the artboard, just double-click it, lest it be too messy
BMP = New Bitmap (400, 300)
Picturebox1.backgroundimage = BMP
End Sub
Sub Setsoundpos (ByVal x as single, ByVal y as single, ByVal Z as single)
"'" "The code that adjusts the buff after the user changes position and speed
"It's simple, it's changing the parameters.
"" Defines the field between 1 and 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 = "Relative coordinates: (x,y) =" + X.tostring + "," + z.tostring
End Sub
End Class

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

Here to the Y axis operation is not much, I can not use PictureBox description 3D effect, perhaps learned to D3D can be compared to describe the image

And there's not much use for Doppler (my speakers are hard to tell), and it's best to use the single sound of that car.

There is also a change in speed need to detect the mouse two times the position of the judge (need to study the formula, lazy) or Doppler not true

This example is not perfect for performance directsound3d, but for the introduction of using Ds3d, I would like to

It makes sense. The rest of us are perfect on our own.

The next time is the final part, using DirectSound for mixing, to achieve special effects.

I've been thinking about making a package for the sound engine, but it seems like you have to understand the D3D in the 3D, so the DS is usually included in the game engine.

It doesn't make much sense to take it out alone (unless you don't use 3D)





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.