TizzyT

Engine_Audio

Jul 31st, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.97 KB | None | 0 0
  1. Imports System.Windows.Media
  2. Module Misc_Audio
  3.     'Stores all Audio Resources
  4.     Private GameAudio As New Dictionary(Of String, AudioBit)
  5.  
  6.     'Load an audio stream into the list for later playback
  7.     Public Sub LoadAudio(ByVal Name As String, ByVal Path As String, Optional ByVal Hash As Byte() = Nothing)
  8.         If Not GameAudio.ContainsKey(Name) Then GameAudio.Add(Name, New AudioBit(Path))
  9.     End Sub
  10.  
  11.     'Removes the specified audio from the list
  12.     Public Sub UnloadAudio(ByVal Name As String)
  13.         If GameAudio.ContainsKey(Name) Then
  14.             GameAudio(Name).StopAll()
  15.             GameAudio(Name).Close()
  16.             GameAudio.Remove(Name)
  17.         End If
  18.     End Sub
  19.  
  20.     'Removes all audio from the list
  21.     Public Sub ClearAudio()
  22.         For Each k As KeyValuePair(Of String, AudioBit) In GameAudio
  23.             k.Value.StopAll()
  24.             k.Value.Close()
  25.         Next
  26.         GameAudio.Clear()
  27.     End Sub
  28.  
  29.     'Plays the main audio stream
  30.     Public Sub PlayAudio(ByVal Name As String)
  31.         If GameAudio.ContainsKey(Name) Then GameAudio(Name).Play()
  32.     End Sub
  33.  
  34.     'Stops all instances for specified audio
  35.     Public Sub StopAllAudio(ByVal Name As String)
  36.         If GameAudio.ContainsKey(Name) Then GameAudio(Name).StopAll()
  37.     End Sub
  38.  
  39.     'Pause all instances for specified audio
  40.     Public Sub PauseAllAudio(ByVal Name As String)
  41.         If GameAudio.ContainsKey(Name) Then GameAudio(Name).PauseAll()
  42.     End Sub
  43.  
  44.     'Plays all instances for specified audio
  45.     Public Sub PlayAllAudio(ByVal Name As String)
  46.         If GameAudio.ContainsKey(Name) Then GameAudio(Name).PlayAll()
  47.     End Sub
  48.  
  49.     'Pauses all sub instances for specified audio
  50.     Public Sub PauseAllSubAudio(ByVal Name As String)
  51.         If GameAudio.ContainsKey(Name) Then GameAudio(Name).PauseAllSubInstances()
  52.     End Sub
  53.  
  54.     'Plays all sub instances for specified audio
  55.     Public Sub PlayAllSubAudio(ByVal Name As String)
  56.         If GameAudio.ContainsKey(Name) Then GameAudio(Name).PlayAllSubInstances()
  57.     End Sub
  58.  
  59.     'Stops a sub instance of specified audio
  60.     Public Sub StopSubAudio(ByVal Name As String, ByVal Instance As UInteger)
  61.         If GameAudio.ContainsKey(Name) Then GameAudio(Name).StopSubInstance(Instance)
  62.     End Sub
  63.  
  64.     'Stops all sub instances for specified audio
  65.     Public Sub StopAllSubAudio(ByVal Name As String)
  66.         If GameAudio.ContainsKey(Name) Then GameAudio(Name).StopAllSubInstances()
  67.     End Sub
  68.  
  69.     'Replays the main audio stream
  70.     Public Sub ReplayAudio(ByVal Name As String)
  71.         If GameAudio.ContainsKey(Name) Then GameAudio(Name).Replay()
  72.     End Sub
  73.  
  74.     'Stops the main audio stream
  75.     Public Sub StopAudio(ByVal Name As String)
  76.         If GameAudio.ContainsKey(Name) Then GameAudio(Name).Stop()
  77.     End Sub
  78.  
  79.     'Pauses the main audio stream
  80.     Public Sub PauseAudio(ByVal Name As String)
  81.         If GameAudio.ContainsKey(Name) Then GameAudio(Name).Pause()
  82.     End Sub
  83.  
  84.     'Plays audio as a separate instance
  85.     Public Function PlayAudioInstance(ByVal Name As String) As UInteger
  86.         If GameAudio.ContainsKey(Name) Then Return GameAudio(Name).PlayNewInstance() Else Return 0
  87.     End Function
  88.  
  89.     'Main audio object for main audio
  90.     Private Class AudioBit
  91.         Inherits MediaPlayer
  92.         Private Shared Beginning As New TimeSpan(0, 0, 0, 0, 1)
  93.         Private Instances As UInteger = 1
  94.         Private TempStore As New Dictionary(Of Integer, TempAudioBit)
  95.  
  96.         Public Sub New(ByVal Path As String)
  97.             Open(New Uri(Path))
  98.             Position = Beginning
  99.         End Sub
  100.  
  101.         'Replays the main audio instance at position 1 millisecond
  102.         Public Sub Replay()
  103.             If Position <> Beginning Then Position = Beginning
  104.             Play()
  105.         End Sub
  106.  
  107.         'Stops all audio instances in this object
  108.         Public Sub StopAll()
  109.             If TempStore.Count > 0 Then
  110.                 For Each k As KeyValuePair(Of Integer, TempAudioBit) In TempStore
  111.                     k.Value.Stop()
  112.                     k.Value.Close()
  113.                 Next
  114.             End If
  115.             TempStore.Clear()
  116.             [Stop]()
  117.         End Sub
  118.  
  119.         'Pause all audio instances in this object
  120.         Public Sub PauseAll()
  121.             If TempStore.Count > 0 Then
  122.                 For Each k As KeyValuePair(Of Integer, TempAudioBit) In TempStore
  123.                     k.Value.Pause()
  124.                 Next
  125.             End If
  126.             Pause()
  127.         End Sub
  128.  
  129.         'Plays all audio instance in this object
  130.         Public Sub PlayAll()
  131.             If TempStore.Count > 0 Then
  132.                 For Each k As KeyValuePair(Of Integer, TempAudioBit) In TempStore
  133.                     k.Value.Play()
  134.                 Next
  135.             End If
  136.             Play()
  137.         End Sub
  138.  
  139.         'Pause all sub instances of audio in this object
  140.         Public Sub PauseAllSubInstances()
  141.             If TempStore.Count > 0 Then
  142.                 For Each k As KeyValuePair(Of Integer, TempAudioBit) In TempStore
  143.                     k.Value.Pause()
  144.                 Next
  145.             End If
  146.         End Sub
  147.  
  148.         'Plays all sub instances of audio in this object
  149.         Public Sub PlayAllSubInstances()
  150.             If TempStore.Count > 0 Then
  151.                 For Each k As KeyValuePair(Of Integer, TempAudioBit) In TempStore
  152.                     k.Value.Play()
  153.                 Next
  154.             End If
  155.         End Sub
  156.  
  157.         'Stops a specified Instance of audio
  158.         Public Sub StopSubInstance(ByVal Instance As UInteger)
  159.             If TempStore.ContainsKey(Instance) Then
  160.                 Dim AudioInstance As TempAudioBit = TempStore(Instance)
  161.                 AudioInstance.Stop()
  162.                 AudioInstance.Close()
  163.             End If
  164.         End Sub
  165.  
  166.         'Stops all sub instances of audio to this object
  167.         Public Sub StopAllSubInstances()
  168.             If TempStore.Count > 0 Then
  169.                 For Each k As KeyValuePair(Of Integer, TempAudioBit) In TempStore
  170.                     k.Value.Stop()
  171.                     k.Value.Close()
  172.                 Next
  173.             End If
  174.             TempStore.Clear()
  175.         End Sub
  176.  
  177.         'Creates/Adds/Plays a new instance of the audio enclosed in this object
  178.         Public Function PlayNewInstance() As UInteger
  179.             If Instances = UInteger.MaxValue Then Instances = 1
  180.             Dim CrntInstance As UInteger = Instances
  181.             Instances += 1
  182.             Dim tempAudio As New TempAudioBit(CrntInstance, Me)
  183.             AddHandler tempAudio.Finished, AddressOf SubInstanceFinished
  184.             TempStore.Add(CrntInstance, tempAudio)
  185.             Return CrntInstance
  186.         End Function
  187.  
  188.         'When main audio is finished playback set to stop state and set position to 1 millisecond
  189.         Private Sub AudioBit_MediaEnded(sender As Object, e As EventArgs) Handles Me.MediaEnded
  190.             [Stop]()
  191.             Position = Beginning
  192.         End Sub
  193.  
  194.         'If sub instance of audio is completed prepare it for closing and removal
  195.         Private Sub SubInstanceFinished(ByVal Instance As Integer)
  196.             If TempStore.ContainsKey(Instance) Then TempStore.Remove(Instance)
  197.         End Sub
  198.  
  199.         'Sub instance of audio for multiple playbacks simultaneously
  200.         Private Class TempAudioBit
  201.             Inherits MediaPlayer
  202.             Public Event Finished(ByVal Instance As UInteger)
  203.             Private Instance As UInteger
  204.  
  205.             Public Sub New(ByVal Instance As UInteger, ByVal Clone As AudioBit)
  206.                 Me.Instance = Instance
  207.                 Open(Clone.Source)
  208.                 Position = Beginning
  209.                 Play()
  210.             End Sub
  211.  
  212.             'When sub instance finishes playback, prepares itself for removal and closes itself
  213.             Private Sub TempAudioBit_MediaEnded(sender As Object, e As EventArgs) Handles Me.MediaEnded
  214.                 RaiseEvent Finished(Instance)
  215.                 Close()
  216.             End Sub
  217.         End Class
  218.     End Class
  219. End Module
Advertisement
Add Comment
Please, Sign In to add comment