• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
waxychicken

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

2 posts in this topic

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: )
[CODE]
/Sound Index File

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

[end][/CODE]


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.
[CODE] 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[/CODE]

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.

[CODE] 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
[/CODE]
0

Share this post


Link to post
Share on other sites
I finaly got it to a point where i can get an error.

[quote]An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: An item with the same key has already been added.[/quote]

the error is at the "DIM The3DBuffer" line in this sub (complete code above.)

[pre] Private Sub PlayStoredSoundBuffer(ByVal BufferIndexClone As SoundListDefinition, ByVal x As Double, ByVal y As Double, ByVal z As Double)
Dim The3DBuffer As 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[/pre]

i don't get it. it's in a new thread. why is there a conflict?
just because it's the same sound?
0

Share this post


Link to post
Share on other sites
Found the problem.
i didn't know you couldn't directcast a copy/clone.
you have to use the function
Device.DuplicateSoundBuffer(Source SoundBuffer, Destination SoundBuffer)
and had to go from SecondarySoundBuffer to just SoundBuffer as the type definitions.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0