Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


waxychicken

Member Since 26 Aug 2011
Offline Last Active Feb 26 2012 08:29 PM

Topics I've Started

Proper .Volume usage?

22 February 2012 - 08:45 PM

What is the proper usage, purpose, or ranges of the .Volume property in SlimDX.DirectSound.SoundBuffer?
(for example, i'm learning that i can't set the .volume to 500)
Does it eleminate attenuation or does it just increase the general volume and thus increase the range the sound can be herd from?

Links welcome.


	Public Sub PlaySound(ByVal description As String, ByVal X As Double, ByVal Y As Double, ByVal Z As Double, Optional ByVal MyVolume As Integer = 500)
		Dim Temp As Integer
		description = UCase(description)
		Temp = SoundList.FindIndex(Function(value As SoundListDefinition)
									   Return value.Description = description
								   End Function)
		Dim SoundBufferEntryCopy As New SoundListDefinition
		Device.DuplicateSoundBuffer(SoundList(Temp).Buffer, SoundBufferEntryCopy.Buffer)
		SoundBufferEntryCopy.Buffer.Volume = MyVolume

		Dim t As New Thread(DirectCast(Sub() PlayStoredSoundBuffer(SoundBufferEntryCopy, X, Y, Z), ThreadStart))
		t.Start()
	End Sub

[Resolved] mystery crash when playing sound 2nd time. [VB.NET2010]

22 February 2012 - 01:23 AM

Ok, New problem:

I created a sound library class.

first: the principal:
Upon initialization ...

Dim Audio as New SoundLibrary

it opens up a data file, reads the information listed in the file, and stores the sounds in buffers accordingly so that they can be called upon later to play at any time.

(Sample sound index file: )
/Sound Index File

[Description]
GunShot
[Frequency]
44100
[BitRate]
16
[FileName]
Pistol1.wav
[End Of Sound]

[end]


It reads the file fine. It will create the 3D listener fine.
It creates the device, primary, and secondary buffers fine.
It even goes so far as to play a sound:

Audio.PlaySound("GUNSHOT", MenuCamera.Position.X, MenuCamera.Position.Y, MenuCamera.Position.Z)
when the program first loads.

but when i ask it to play the same sound a second time it crashes
For testing reasons, i have it so that clicking on the screen anywhere calls the Audio.PlaySound again.
If MouseStateInfo.LeftClick = True Then
			MouseStateInfo.LeftClick = False
			If ShowMenu1 = True Or PopUp <> eMessageBox.NoBoxDisplayed Then
				MenuClick(MouseStateInfo.Position.X, MouseStateInfo.Position.Y)
				Audio.PlaySound("GUNSHOT", 0.1R, 0.1R, 0.1R)

				Application.DoEvents()
				'exit Function
			End If
		End If

but it's crashing.
and it's not giving me an error response in Debug or in the Try..Catches i have set up.

If anyone can tell me what the hell i'm doing wrong then i'll be totally grateful because i am utterly lost in why sounds will play once but not a second time.

Imports System
Imports System.Text
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports Microsoft.VisualBasic
Imports SlimDX
Imports SlimDX.Multimedia
Imports SlimDX.DirectSound
Imports System.Threading
Imports System.IO

'Some items are not actually used yet even though they are loaded. such as Frequency.

		'a ListOf by the definition of SoundListDefinition under the name of SoundList.
		'soundbuffers and discriptions are added to the list using Sub AddSoundToIndex(...)
		'When a call is made to play a sound then the following steps are taken: (see sub PlayStoredSoundBuffer(...) )
		'	1 - match the "Discription" given to a SoundList(IndexOf)
		'	2 - clone SoundList(IndexOf) to a new object.
		'	3 - start a new thread for the playing (copy of) buffer
		'	4 - the thread loops and when the Buffer.Status = DonePlaying then
		'		  it disposes of the temporary buffer.

		' Current problem: Sounds play only once then crash on the second attempt to play.


Public Class SoundLibrary

	Public Shared AmUpdating As Boolean = False
	Public Shared AmReady As Boolean = False
	Public Structure ThreeDAlgos
		Public Const Defalt As String = "Default 3D Algorithm"
		Public Const Low As String = "Low 3D Algorithm"
		Public Const Full As String = "Full 3D Algorithm"
		Public Const NoVirt As String = "No Virtualization Algorithm"
	End Structure

	Private Structure SoundListDefinition
		Dim Description As String
		Dim Buffer As SecondarySoundBuffer

	End Structure
	'TODO: note to self
	'http://health-7.com/Introduction%20to%203D%20Game%20Engine%20Design%20Using%20DirectX%209%20and%20CSharp/Hearing%20in%20Three%20Dimensions
	'SoundBuffer3D listing 9.16


	Private Device As New DirectSound
	Private SoundList As New List(Of SoundListDefinition)
	Private Buffer As PrimarySoundBuffer
	Private Desc As SoundBufferDescription
	Friend applicationBuffer As SecondarySoundBuffer
	Friend MyListener As SoundListener3D
	Friend SoundPath As String

	Friend Sub New()
		PushIntoChat("INITILIZING SOUND...")
		SetUpHardware()
		SoundPath = Application.StartupPath
		AddToDebug("Sub SoundLibrary New()")

		Dim CurrentLine As String
		Dim WavSoundsList As System.IO.TextReader = New System.IO.StreamReader(SoundPath + "\bin\Sounds\SoundList.txt")
		Dim Doit As Boolean = True
		Dim Channels As Short = 1
		Dim Frequency As Integer
		Dim BitRate As Short
		Dim FileName As String = "FILENAME NOT GIVEN"
		Dim Description As String = "NO DESCRIPTION SET"

		Try
			While Doit = True
ReReadLine:
				CurrentLine = WavSoundsList.ReadLine()
				AddToDebug("JUST READ:" & CurrentLine)

				CurrentLine = UCase(Trim(CurrentLine))
				If Left(CurrentLine, 1) = "/" Or Left(CurrentLine, 1) = "\" Or CurrentLine = "" Then
					GoTo ReReadLine
				End If

				Select Case CurrentLine
					Case "[DESCRIPTION]"
						Description = UCase(WavSoundsList.ReadLine())
					Case "[FREQUENCY]"
						Frequency = Val(WavSoundsList.ReadLine())
					Case "[BITRATE]"
						BitRate = (Val(WavSoundsList.ReadLine()))
					Case "[FILENAME]"
						CurrentLine = Trim(UCase(WavSoundsList.ReadLine()))
						FileName = SoundPath + "\bin\sounds\" + CurrentLine
					Case "[END OF SOUND]"
						'have all info. load the sound and store it in the list.
						AddSoundToIndex(FileName, Channels, Frequency, BitRate, Description)
						Description = "Undiscribed"
						Frequency = 0
						BitRate = 0
						FileName = "NO FILE NAME PROVIDED IN SOUND LIST FILE"


					Case "[END]"
						Doit = False
				End Select

			End While
			WavSoundsList.Close()
			WavSoundsList = Nothing
		Catch ex As Exception
			MsgBox("ERROR LOADING SOUNDS:" & ex.ToString)
			MsgBox(debugstring)
		End Try
		Application.DoEvents()
		Application.DoEvents()
		Application.DoEvents()

		AmReady = True

	End Sub

	Private Sub AddSoundToIndex(ByVal File As String, ByVal Channels As Short, ByVal Frequency As Integer, ByVal Bitrate As Short, ByVal Description As String)



		If File = "FILENAME NOT GIVEN" Then
			PushIntoChat("ERROR: NO FILE NAME GIVEN FOR SOUND FILE!")
			Exit Sub
		End If

		PushIntoChat("Loading sound...: " & File & "  " & Description)
		Dim DefaultBufferDescription As New SoundBufferDescription
		Dim CurrentSoundToAdd As New SoundListDefinition
		Dim FileName As New WaveStream(File)


		DefaultBufferDescription = New SoundBufferDescription
		DefaultBufferDescription.AlgorithmFor3D = DirectSound3DAlgorithmGuid.NoVirtualization3DAlgorithm
		DefaultBufferDescription.Format = FileName.Format
		DefaultBufferDescription.Flags = BufferFlags.Control3D + BufferFlags.Mute3DAtMaxDistance + BufferFlags.GlobalFocus

		If DefaultBufferDescription.Format.Channels <> 1 Then
			PushIntoChat("WARNING! NOT A MONO SOUND FILE! AVOID STEREO! " & File)
			Exit Sub
		End If

		DefaultBufferDescription.Flags = BufferFlags.Control3D + BufferFlags.GlobalFocus + BufferFlags.ControlVolume
	
		DefaultBufferDescription.SizeInBytes = FileName.Length
		CurrentSoundToAdd.Buffer = New SecondarySoundBuffer(Device, DefaultBufferDescription)
		CurrentSoundToAdd.Buffer.Write(ReadWaveFile(FileName), 0, LockFlags.None)
		CurrentSoundToAdd.Description = Description

		CurrentSoundToAdd.Description = UCase(Description)
		SoundList.Add(CurrentSoundToAdd)
		PushIntoChat("Sound Successfully loaded into memory")


	End Sub

	Private Sub SetPrimaryBuffer(ByVal channels As Short, ByVal frequency As Integer, ByVal bitRate As Short)
		Desc = New SoundBufferDescription


		Dim format As New WaveFormatExtensible()
		Try
			With format
				.FormatTag = WaveFormatTag.Pcm
				.Channels = 1
				.SamplesPerSecond = frequency
				.BitsPerSample = bitRate
				.BlockAlignment = CShort(Fix(bitRate \ 8 * channels))
				.AverageBytesPerSecond = frequency * format.BlockAlignment

			End With

			Desc.Flags = BufferFlags.PrimaryBuffer + BufferFlags.Control3D ' + BufferFlags.GlobalFocus
			Device.SetCooperativeLevel(Form1.Handle, CooperativeLevel.Priority)
			Buffer = New PrimarySoundBuffer(Device, Desc)
			Buffer.Format = format
		Catch ex As Exception
			Debug.Print(ex.ToString)
			Debug.Print(ex.Message)
			Debug.Print("  ")
		End Try

	End Sub


	
	Private Function ReadWaveFile(ByVal file As WaveStream) As Byte()

		Dim data(file.Length - 1) As Byte
		file.Read(data, 0, CInt(file.Length))

		Return data
	End Function

	Private Sub SetUpHardware()
		Device = New DirectSound()
		Device.SetCooperativeLevel(Form1.Handle, CooperativeLevel.Priority)
		SetPrimaryBuffer(1, 44100, 8)
		MyListener = Create3DListener(1, 1, 1)

	End Sub
	Private Function Create3DListener(ByVal x As Single, ByVal y As Single, ByVal z As Single) As SoundListener3D
		Dim GivenVector As Vector3
		Dim listener As SoundListener3D
		Dim description As New SoundBufferDescription

		listener = New SoundListener3D(Buffer)
		GivenVector = New Vector3(x, y, z)
		listener.Position = GivenVector
		Return (listener)

	End Function

	Public Sub MoveListener(ByVal x As Single, ByVal y As Single, ByVal z As Single)
		MyListener.Position = New Vector3(x, y, z)

	End Sub
	Public Sub PlaySound(ByVal MyIndex As Integer, ByVal x As Double, ByVal y As Double, ByVal z As Double)
		PlaySound(SoundList(MyIndex).Description, x, y, z)

	End Sub

	Public Sub StartUpdatingAudio()
		Dim T As Thread
		T = New Thread(AddressOf UpdateAudioThread)
		AmUpdating = True
		T.Start()

	End Sub

	Private Sub UpdateAudioThread()

		While SoundLibrary.AmUpdating = True
			If MyListener IsNot Nothing And InGameCamera IsNot Nothing Then
				Dim front As New Vector3(InGameCamera.Target.X, InGameCamera.Target.Y, InGameCamera.Target.Z)
				Dim top As New Vector3(0.0F, 1.0F, 0.0F)
				Dim position As New Vector3(InGameCamera.Position.X, InGameCamera.Position.Y, InGameCamera.Position.Z)
				MyListener.FrontOrientation = front
				MyListener.TopOrientation = top
				MyListener.Position = position
				MyListener.CommitDeferredSettings()
			End If
		End While


	End Sub
	Public Sub StopUpdatingAudio()
		AmUpdating = False
	End Sub

	Public Sub PlaySound(ByVal description As String, ByVal X As Double, ByVal y As Double, ByVal z As Double, Optional ByVal MyVolume As Integer = 100)
		Dim Temp As Integer
		description = UCase(description)
		Temp = SoundList.FindIndex(Function(value As SoundListDefinition)
									   Return value.Description = description
								   End Function)

		Dim SoundBufferEntryCopy As SoundListDefinition
		SoundBufferEntryCopy = Me.CopyOf(SoundList(Temp))

		'   Audio.PlaySound("GUNSHOT", 0, 0, 0) SoundBufferEntryCopy.Buffer.Volume = MyVolume
		'If SoundBufferEntryCopy.Buffer.Status = BufferStatus.Playing Then
		'	SoundBufferEntryCopy.Buffer.Stop()
		'End If

		Dim t As New Thread(DirectCast(Sub() PlayStoredSoundBuffer(SoundBufferEntryCopy, X, y, z), ThreadStart))

		t.Start()

	End Sub
	Private Function CopyOf(ByVal MyObject As SoundListDefinition) As SoundListDefinition
		'Duplicates the object so i may play it multiple times without worrying about interrupting a currently playing sound.
		Dim NewObject As SoundListDefinition
		NewObject.Buffer = MyObject.Buffer
		NewObject.Description = MyObject.Description
		NewObject.Buffer = DirectCast(MyObject.Buffer, SecondarySoundBuffer)
		Return NewObject

	End Function
	Private Sub PlayStoredSoundBuffer(ByVal BufferIndexClone As SoundListDefinition, ByVal x As Double, ByVal y As Double, ByVal z As Double)

		Dim The3DBuffer As SoundBuffer3D = New SoundBuffer3D(BufferIndexClone.Buffer)
		The3DBuffer.Position = New Vector3(Convert.ToSingle(x), Convert.ToSingle(y), Convert.ToSingle(z))
		BufferIndexClone.Buffer.Play(0, PlayFlags.TerminateByDistance And PlayFlags.TerminateByPriority And PlayFlags.TerminateByTime)

		While BufferIndexClone.Buffer.Status = BufferStatus.Playing
			Application.DoEvents()

		End While

		'I thought that disposing things was perhaps the problem - maybe i was
		'cross-disposing items still in use. but that hasn't helped.

		'The3DBuffer.Dispose()
		'BufferIndexClone.Buffer.Dispose()
		'BufferIndexClone = Nothing

	End Sub

End Class

Looking for tutorial links

15 February 2012 - 02:18 AM

I was banging my head for 3days trying to figure out why my D3D References weren't showing up in VS 2010 and found out that it's not compatible with .NET 4.0 so i'm having a go at SlimDX.

I've just downloaded the newest relase but their website is seriusly lacking on tutorials.

I'm looking for a SlimDX 3D audio tutorial that can do this:
http://content.gpwik...ET:DX9:3D_Sound
(3d listener location and sound emitter location)

I've been GooGle-ing but i'm only finding Graphic tutorials with SlimDX (but i know it has audio because i checked the docs)

Anybody have any SlimDX Audio tutoiral links available?

will take any .NET tutorials
(I use VB.NET and can read a good bit of VC.NET even though i can't write it yet. so any .NET tutorials for Slim)

Confused about NAT UDP Hole Punching

12 February 2012 - 09:59 PM

I've asked about this in a couple of places but havn't found anyone that knows how to do it.
I've also been reading up at http://www.brynosaur...pub/net/p2pnat/ but some of it is a bit fuzzy to me so maybe you guys can help.

First, the problem:
  • Game Server is UPnP enabled. works perfectly.
  • Client has no UPnP protocols on their router so it requires manual port settings (which kills any modern game) or NAT Punching.
  • "Master Server" is just an HTTP PHP scripted web server that receives Form Submissions to add, remove, and output current Game Server IP:Port infos for clients to download and browse through. (Server Browser data for clients)

currently i have:

Server starts, contacts master server via HTTP request and informs it "This is my port/ip/name/etc..."

Client loads and makes an HTTP request to the master server and gets the serverlist, including our target server.

Server Listens on: <Server Customized>
Server Sends To: 45,000

Client Listens On: 45,000
Client Sends To: <Server customized>

Client does HTTP request to a master server to get the target server's IP and <ServerCustomized>port (working)

The client sends UDP request to the server join the server targeting <ServerCustomized> port. (working)

Server hears the client request (working)
and replies to client listening port 45000 "yes you may join" (NOT working - NON-UPnP routers are blocking)

as far as i can tell, the server is sending it, but the client doesn't hear it due to it's NAT router.

Q: OMG! Why are you doing hole punching?
A:Because i've already found 2/3 testers that DONT have upnp routers (eg: WANPPPConnection:1 and WANIPConnection:1).


From what i understand from http://www.brynosaur...pub/net/p2pnat/ , it appears that the game server won't need a 3rd negotiating machine so long as the game server is properly configured to receive connection requests from game clients - no problem. got that.

According to the document and what I've been able to scrounge up on the web it seems the game server will need to reply to the client on the end points:
  • LAN.IP.xxx.xxx:MachinePort "Token #1"
  • WAN.IP.xxx.xxx:RouterPort "Token #2"
  • WAN.IP.xxx.xxx:MachinePort "Token #3"
(as far as i understand, endpoint means IP:PORT )

The game client will reply back with a "I herd "Token #n " and the server locks in on that information to run the game connection.

Now my questions:
  • Do i understand correctly?
  • How do i find the external WAN port of the client?
LAN IP, PORT no problem.
WAN IP, no problem
WAN Port - don't know how to get it.

On a side question of the same topic:
on section 2.2 of the document http://www.brynosaur...pub/net/p2pnat/,
how do either of the machines receive the Hole Punch UDP Data Packets from the Server S if the client routers are not yet holepunched to get through the NAT?



VB.NET 2010 Ultimate
Win 7 Ultimate

UDP Hole Punch?

06 February 2012 - 12:51 AM

Game Server is UPnP enabled. works perfectly.
Client has no UPnP protocols on their router so it requires manual port settings.
But these days most games find a way around that problem and i'm guessing they are using Hole Punching.

I've researched it a bit but it's all fuzzy to me. So maybe someone can clarify the steps needed to do a HolePunch on most networks.


currently i have: (all UDP except for the HTTP ofcourse)

Server starts, contacts master server via HTTP request and informs it "This is my port/ip/name/etc..."

Client loads and makes an HTTP request to the master server and gets the serverlist, including our target server.

Server Listens on: <Server Customized>
Server Sends To: 45,000

Client Listens On: 45,000
Client Sends To: <Server customized>

Client does HTTP request to a master server to get the target server's IP and <ServerCustomized>port (working)

The client sends UDP request to the server join the server targeting <ServerCustomized> port. (working)

Server hears the client request (working)
and replies to client listening port 45000 "yes you may join" (NOT working)

as far as i can tell, the server is sending it, but the client doesn't hear it. I say this because it works on LAN and i haven't found a UPnP enabled tester to try it with over the internet.

to hole punch, do i just need to have the client send the initial join request on both
<ServerCustomized> port and the 45000 port to punch a hole?

Q: OMG! Why are you doing hole punching?
A:Because i've already found 2/3 testers that DONT have upnp routers (eg: WANPPPConnection:1 and WANIPConnection:1).

PARTNERS