home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / Stream_MP32029381162006.psc / AudioMemoryPlayer.cls < prev    next >
Text File  |  2006-11-06  |  6KB  |  201 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "AudioMemoryPlayer"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. ' Buffer Length < 1000 can only be even hundreds
  17. ' (200, 400, 600, 800). I can't explain why,
  18. ' but for odd hundreds smaller streams can be
  19. ' played back wrong (missing or swapped buffers).
  20. ' The bigger the buffer, the better ;)
  21. Private Const StreamBufferLength    As Long = 2000
  22.  
  23. Public Event EndOfStream()
  24. Public Event StatusChanged(ByVal stat As PlaybackStatus)
  25.  
  26. Private WithEvents m_clsAudioOut    As DirectSoundStream
  27. Attribute m_clsAudioOut.VB_VarHelpID = -1
  28. Private m_clsDirectSound            As DirectSound
  29. Private m_clsWaveStream             As WmStream
  30.  
  31. Public Property Get Volume() As Long
  32.     Volume = m_clsAudioOut.Volume
  33. End Property
  34.  
  35. Public Property Let Volume(ByVal value As Long)
  36.     m_clsAudioOut.Volume = value
  37. End Property
  38.  
  39. Public Property Get Balance() As Long
  40.     Balance = m_clsAudioOut.Balance
  41. End Property
  42.  
  43. Public Property Let Balance(ByVal value As Long)
  44.     m_clsAudioOut.Balance = value
  45. End Property
  46.  
  47. Public Property Get Duration() As Long
  48.     Duration = m_clsWaveStream.Duration
  49. End Property
  50.  
  51. Public Property Get Position() As Long
  52.     Position = m_clsAudioOut.Elapsed
  53. End Property
  54.  
  55. Public Property Let Position(ByVal value As Long)
  56.     Dim i                   As Long
  57.     Dim intData()           As Integer
  58.     Dim lngDataSize         As Long
  59.     Dim lngRead             As Long
  60.     Dim lngStreamPosition   As Long
  61.     
  62.     m_clsWaveStream.StreamSeek value \ 1000, SND_SEEK_SECONDS
  63.     lngStreamPosition = m_clsWaveStream.Position
  64.     
  65.     If m_clsAudioOut.PlaybackStatus = PlaybackPlaying Then
  66.         ' clear buffer queue
  67.         m_clsAudioOut.PlaybackStop
  68.     
  69.         ' buffer 2 seconds of audio data
  70.         lngDataSize = m_clsAudioOut.BytesFromMs(200)
  71.         ReDim intData(lngDataSize \ 2 - 1) As Integer
  72.     
  73.         For i = 1 To 10
  74.             m_clsWaveStream.StreamRead VarPtr(intData(0)), lngDataSize, lngRead
  75.             If lngRead > 0 Then
  76.                 m_clsAudioOut.AudioBufferAdd VarPtr(intData(0)), lngRead
  77.             Else
  78.                 Exit For
  79.             End If
  80.         Next
  81.     
  82.         m_clsAudioOut.PlaybackStart
  83.     End If
  84.     
  85.     m_clsAudioOut.Elapsed = lngStreamPosition
  86. End Property
  87.  
  88. Public Function StopPlayback() As Boolean
  89.     If m_clsAudioOut.PlaybackStop() Then
  90.         m_clsWaveStream.StreamSeek 0, SND_SEEK_PERCENT
  91.         RaiseEvent StatusChanged(PlaybackStopped)
  92.         StopPlayback = True
  93.     End If
  94. End Function
  95.  
  96. Public Function Pause() As Boolean
  97.     If m_clsAudioOut.PlaybackPause() Then
  98.         RaiseEvent StatusChanged(PlaybackPausing)
  99.         Pause = True
  100.     End If
  101. End Function
  102.  
  103. Public Function Play() As Boolean
  104.     Dim i           As Long
  105.     Dim intData()   As Integer
  106.     Dim lngDataSize As Long
  107.     Dim lngRead     As Long
  108.     
  109.     If m_clsAudioOut.PlaybackStatus = PlaybackStopped Then
  110.         ' buffer 2 seconds of audio data
  111.         lngDataSize = m_clsAudioOut.BytesFromMs(200)
  112.         ReDim intData(lngDataSize \ 2 - 1) As Integer
  113.         
  114.         For i = 1 To 10
  115.             m_clsWaveStream.StreamRead VarPtr(intData(0)), lngDataSize, lngRead
  116.             If lngRead > 0 Then
  117.                 m_clsAudioOut.AudioBufferAdd VarPtr(intData(0)), lngRead
  118.             Else
  119.                 Exit For
  120.             End If
  121.         Next
  122.     End If
  123.     
  124.     If m_clsAudioOut.PlaybackStart() Then
  125.         RaiseEvent StatusChanged(PlaybackPlaying)
  126.         Play = True
  127.     End If
  128. End Function
  129.  
  130. Public Function OpenStream(ByVal pMem As Long, ByVal DataSize As Long) As Boolean
  131.     Dim clsStream   As DirectSoundStream
  132.     
  133.     CloseStream
  134.     
  135.     If m_clsWaveStream.StreamOpenByPointer(pMem, DataSize) = SND_ERR_SUCCESS Then
  136.         With m_clsWaveStream
  137.             If m_clsDirectSound.CreateStream(.Sameplerate, .Channels, .BitsPerSample, StreamBufferLength, clsStream) Then
  138.                 Set m_clsAudioOut = clsStream
  139.                 OpenStream = True
  140.             Else
  141.                 m_clsWaveStream.StreamClose
  142.             End If
  143.         End With
  144.     End If
  145. End Function
  146.  
  147. Public Function CloseStream() As Boolean
  148.     CloseStream = m_clsWaveStream.StreamClose() = SND_ERR_SUCCESS
  149.     Set m_clsAudioOut = Nothing
  150. End Function
  151.  
  152. Private Sub Class_Initialize()
  153.     Set m_clsDirectSound = New DirectSound
  154.     Set m_clsWaveStream = New WmStream
  155.     
  156.     m_clsDirectSound.Initialize 1, 44100, 2, 16
  157. End Sub
  158.  
  159. Private Sub m_clsAudioOut_BufferDone()
  160.     Dim intData()   As Integer
  161.     Dim lngDataSize As Long
  162.     Dim lngRead     As Long
  163.     
  164.     If Not m_clsWaveStream.EndOfStream Then
  165.         lngDataSize = m_clsAudioOut.BytesFromMs(200)
  166.         ReDim intData(lngDataSize \ 2 - 1) As Integer
  167.     
  168.         m_clsWaveStream.StreamRead VarPtr(intData(0)), lngDataSize, lngRead
  169.         
  170.         If lngRead > 0 Then
  171.             m_clsAudioOut.AudioBufferAdd VarPtr(intData(0)), lngRead
  172.         End If
  173.     End If
  174. End Sub
  175.  
  176. Private Sub m_clsAudioOut_NoDataLeft()
  177.     Dim intData()   As Integer
  178.     Dim lngDataSize As Long
  179.     Dim lngRead     As Long
  180.     Dim i           As Long
  181.     
  182.     If m_clsWaveStream.EndOfStream Then
  183.         m_clsAudioOut.PlaybackStop
  184.         m_clsWaveStream.StreamSeek 0, SND_SEEK_PERCENT
  185.         RaiseEvent EndOfStream
  186.     Else
  187.         ' buffer underrun, buffer 2 seconds of audio data
  188.         lngDataSize = m_clsAudioOut.BytesFromMs(200)
  189.         ReDim intData(lngDataSize \ 2 - 1) As Integer
  190.         
  191.         For i = 1 To 10
  192.             m_clsWaveStream.StreamRead VarPtr(intData(0)), lngDataSize, lngRead
  193.             If lngRead > 0 Then
  194.                 m_clsAudioOut.AudioBufferAdd VarPtr(intData(0)), lngRead
  195.             Else
  196.                 Exit For
  197.             End If
  198.         Next
  199.     End If
  200. End Sub
  201.