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