VB.NET implementation DirectSound9 (5) DS3D2

xiaoxiao2021-03-06  41

Keywords: VB .NET DirectX DirectSound 3D Author: Dong Jun containing

After an afternoon, I walked a few detours, I finally played 3D sound effect.

Move ~ ~

Based on the following steps:

1 Related Device SetCoopeRATIVELEVEL2 Setting 3D Hardware Effects DSOUNDHELPER.GUID3DalgorithMHRTffull3 format requires SoundFormat must be mono, cannot be stereo 4 primary buffer description 5 Creating a Listenner6 Auxiliary Buffer Read WAV7 Create 3D Buffer 8 Play 9 Control Space Location, and Settings Doppler effect factor, attenuation factor 10 stops playing

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

Since the 9CBS blog can't get the map and RAR, only the code is code, there are two Button a PictureBox, there are several Label

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

Imports Microsoft.directXimports Microsoft.directx.directSoundImports System.drawingImports System.drawing.graphics

Public class form1 inherits system.windows.Forms.form

Dim Dev As Device 'device Dim Buff As Buffer' main buffer Dim SBuff As SecondaryBuffer 'two buffer Dim Buff3D As Buffer3D' 3D buffer Dim descBuff As BufferDescription 'buffer descriptor Dim Buff3DSet As Buffer3DSettings' 3D buffer setting Dim Listenner As Listener3D' listener DIM Listennerset As Listener3dSettings 'listener Sets DIM PIC AS Graphics Dim Bmp As Bitmap const fn = "g: /media/wav/rod2m.wav"' To play files, must be a mono #REGONSFIWS Form Designer Generator Code "

Public Sub new () mybase.new ()

'This call is required for the Windows Form Designer. InitializeComponent ()

'Add any initialization after INITIALIZECOMPONENT ()

End Sub

'Form rewriting Dispose to clean up the list of components. 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 Form Designer Private components required As system.componentmodel.icontainer

'Note: The following procedure is necessary to use the Windows Form Designer to modify this process using the Windows Form Designer. 'Don't 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 . Form.label private subinitializeComponent () me.button1 = new system.windows.Forms.Button Me.Button2 = New S ystem.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 = "Initialization" '' 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 =

"Maximum distance 0 ~ 100" '' label4 'me.label4.location = new system.drawing.point (8, 272) me.label4.name = "label4" me.label4.TabINDEX = 6 me.label4.text = "Minimum distance 0 ~ 100" '' TextBox1 'me.textbox1.location = new system.drawing.point (248) 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 '' 'when loaded at initialization PictureBox, it becomes black (no color) PictureBox1_DoubleClick (0, Nothing) End Sub

Sub initDirectSound () '' and a reading device loaded DirectSound wav files' provided a 3D mode Dim DSMode As Guid DSMode = DSoundHelper.Guid3DAlgorithmHrtfFull 'due to demonstrate the effect of 3D, so a maximum mode' associated form Dev = New Device Dev.SetCooperativeLevel ( Me.Handle, CooperativeLevel.Priority) '' loading wav '' 'commented, the original test of' 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 described 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' '' above can remove the comment attempt, when primary is opened, other can't use, conflict, especially behind, a few 'creation main buffers Try '' Capture, prevent Descript's description (not the parameter setting) BUFF = New Buffer (Descbuff, DEV) Catch exception msgbox (ex. measureage) end 'is wrong, there is no need to continue End Try 'creates audience Listenner = New Listener3D (BUFF) ListennerSet = Listenner.AllParameters 'audience using the main buffer settings' 'WAV Dim tmpDesc2 As New BufferDescription tmpDesc2.Guid3DAlgorithm = DSMode tmpDesc2.Control3D = True' '' here must use the auxiliary buffer to provide examples to the main buffer, do not know how to think Microsoft It may also be that I am studying, how to try it to use buff = New SecondaryBuffer (FN, TMPDESC2, DEV)

'Creating a 3D buffer buff3d = new buff3d (buff) buff3dset = buff3d.allparameters' Using the default setting buff3dset.mode = mode3d.HeadRelanch 'change a parameter buff3d.allparameters = buff3dset' Apply new settings

MsgBox ("" ​​ fn) 'OK, initialize completed End Sub

Private sub button2_click (byval e as system.EventArgs) Handles Button2.Click Buff.Play (0, BufferplayFlags.LoPing) End Sub

Private sub button1_click (byvale) Handles button1.click initdirectsound () end sub

Private Sub PictureBox1_MouseMove (ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove '' 'When press down the left mouse button when effective, I do not want to change a rollover If e.Button <> MouseButtons.Left the exit sub Dim r = new Rectangle (EX, EY, 3, 3)

Pic = PictureBox1.creategraphics () me.text = ext7tring "," " EytoString Pic.drawImage (BMP, 1, 1) Pic.drawellipse (New Pen (Color.red), R) Setsoundpos (Convert.Tosingle EX), convert.tosingle (TextBox5.text), convert.tosingle (EY)) '' 'Here the coordinate transformation, because the Y axial, we need far close, so negative value to the z-axis (top view from above) End Sub

Private Sub PictureBox1_DoubleClick (ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick '' 'clear the drawing board, simply double-click, so too messy BMP = New Bitmap (400, 300) PictureBox1.BackgroundImage = BMP End Sub Sub setsoundpos (byval x askLE, BYVAL Y, BYVAL Z AS SINGLE) '' '' '' '' changes the location and speed, the code '' '' '' '' '' is very simple, that is, change the parameters. '' Defining the Domain between -1 to 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 speted.y = 1 speted.z = 1buff3d.position = POS buff3d.velocity = speed

Label6.text = "Relative coordinates: (x, y) =" x.toString "," Z.TOString End Subend Class

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

There are not many operations on the Y axis, I can't use the Picturebox to describe the 3D effect, maybe I can learn D3D to describe the image.

And for Doppler, there is no too much use (my speaker is difficult to distinguish), it is best to use the single voice of the car.

There is also a speed change that needs to detect the position of the mouse twice (need to study the formula, lazy) otherwise Doppler is not true enough.

This example is not perfect for performance of DirectSound3D, but for the steps to use DS3D, I think or

Speaking of it. Other some of everyone can perfect.

The next time is the final part, use DirectSound to make a special effect.

I want to make a package, for the sound engine, but it seems to be involved in the 3D place must understand D3D, so DS is generally included in the game engine.

There is not much sense to take it alone (unless 3D is not used)

转载请注明原文地址:https://www.9cbs.com/read-53692.html

New Post(0)