home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / DirectSoun2029361162006.psc / DirectSound.cls < prev    next >
Text File  |  2006-11-04  |  5KB  |  183 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 = "DirectSound"
  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. Private Declare Function CreateWindowEx Lib "user32" _
  17. Alias "CreateWindowExA" ( _
  18.     ByVal dwExStyle As Long, _
  19.     ByVal lpClassName As String, _
  20.     ByVal lpWindowName As String, _
  21.     ByVal dwStyle As Long, _
  22.     ByVal x As Long, _
  23.     ByVal y As Long, _
  24.     ByVal nWidth As Long, _
  25.     ByVal nHeight As Long, _
  26.     ByVal hWndParent As Long, _
  27.     ByVal hMenu As Long, _
  28.     ByVal hInstance As Long, _
  29.     ByVal lpParam As Long _
  30. ) As Long
  31.  
  32. Private Declare Function DestroyWindow Lib "user32" ( _
  33.     ByVal hWnd As Long _
  34. ) As Long
  35.  
  36. Private m_clsPrimBuf    As DirectSoundPrimaryBuffer8
  37. Private m_clsDSound     As DirectSound8
  38. Private m_blnReady      As Boolean
  39. Private m_hDSWnd        As Long
  40.  
  41. Public Property Get DirectXAvailable() As Boolean
  42.     DirectXAvailable = m_blnReady
  43. End Property
  44.  
  45. Public Property Get DeviceCount() As Long
  46.     DeviceCount = DirectX.GetDSEnum.GetCount
  47. End Property
  48.  
  49. Public Property Get DeviceName(ByVal index As Long) As String
  50.     DeviceName = DirectX.GetDSEnum.GetName(index)
  51. End Property
  52.  
  53. Public Property Get DeviceDescription(ByVal index As Long) As String
  54.     DeviceDescription = DirectX.GetDSEnum.GetDescription(index)
  55. End Property
  56.  
  57. Public Property Get DeviceGuid(ByVal index As Long) As String
  58.     DeviceGuid = DirectX.GetDSEnum.GetGuid(index)
  59. End Property
  60.  
  61. Public Function CreateStream( _
  62.     ByVal Samplerate As Long, _
  63.     ByVal Channels As Integer, _
  64.     ByVal BitsPerSample As Integer, _
  65.     ByVal BufferLengthMs As Long, _
  66.     ByRef stream As DirectSoundStream _
  67. ) As Boolean
  68.  
  69.     Dim clsStream As DirectSoundStream
  70.     Dim clsSecBuf As DirectSoundSecondaryBuffer8
  71.     Dim udtBufDsc As DSBUFFERDESC
  72.     
  73.     If DirectXAvailable Then
  74.         If BufferLengthMs < 200 Then BufferLengthMs = 200
  75.         If BufferLengthMs < 1000 Then
  76.             If (BufferLengthMs \ 100) Mod 2 = 1 Then
  77.                 BufferLengthMs = (BufferLengthMs \ 100 + 1) * 100
  78.             End If
  79.         End If
  80.     
  81.         With udtBufDsc
  82.             With .fxFormat
  83.                 .lSamplesPerSec = Samplerate
  84.                 .nChannels = Channels
  85.                 .nBitsPerSample = BitsPerSample
  86.                 .nBlockAlign = .nChannels * (.nBitsPerSample \ 8)
  87.                 .lAvgBytesPerSec = .nBlockAlign * .lSamplesPerSec
  88.                 .nFormatTag = WAVE_FORMAT_PCM
  89.             End With
  90.             
  91.             .lFlags = DSBCAPS_CTRLPAN Or _
  92.                       DSBCAPS_CTRLVOLUME Or _
  93.                       DSBCAPS_GETCURRENTPOSITION2 Or _
  94.                       DSBCAPS_GLOBALFOCUS Or _
  95.                       DSBCAPS_STATIC
  96.                       
  97.             .lBufferBytes = (BufferLengthMs / 1000) * .fxFormat.lAvgBytesPerSec
  98.         End With
  99.         
  100.         On Error GoTo ErrorHandler
  101.             Set clsSecBuf = m_clsDSound.CreateSoundBuffer(udtBufDsc)
  102.         On Error GoTo 0
  103.  
  104.         Set clsStream = New DirectSoundStream
  105.  
  106.         ' initialize the new stream with the created secondary buffer
  107.         CallCustomConstructor clsStream, udtBufDsc.lBufferBytes, VarPtr(clsSecBuf)
  108.         
  109.         Set stream = clsStream
  110.         
  111.         CreateStream = True
  112.     Else
  113.         CreateStream = False
  114.     End If
  115.     
  116.     Exit Function
  117.     
  118. ErrorHandler:
  119.     CreateStream = False
  120. End Function
  121.  
  122. Public Function Deinitialize() As Boolean
  123.     Set m_clsPrimBuf = Nothing
  124.     Set m_clsDSound = Nothing
  125.     
  126.     Deinitialize = True
  127. End Function
  128.  
  129. Public Function Initialize( _
  130.     ByVal DeviceIndex As Long, _
  131.     ByVal Samplerate As Long, _
  132.     ByVal Channels As Integer, _
  133.     ByVal BitsPerSample As Integer _
  134. ) As Boolean
  135.  
  136.     Dim udtBufDesc  As DSBUFFERDESC
  137.  
  138.     On Error GoTo ErrorHandler
  139.         Set m_clsDSound = DirectX.DirectSoundCreate(DeviceGuid(DeviceIndex))
  140.         m_clsDSound.SetCooperativeLevel m_hDSWnd, DSSCL_PRIORITY
  141.     On Error GoTo 0
  142.     
  143.     With udtBufDesc
  144.         With .fxFormat
  145.             .lSamplesPerSec = Samplerate
  146.             .nChannels = Channels
  147.             .nBitsPerSample = BitsPerSample
  148.             .nBlockAlign = .nChannels * (.nBitsPerSample \ 8)
  149.             .lAvgBytesPerSec = .nBlockAlign * .lSamplesPerSec
  150.             .nFormatTag = WAVE_FORMAT_PCM
  151.         End With
  152.         
  153.         .lFlags = DSBCAPS_PRIMARYBUFFER
  154.     End With
  155.     
  156.     On Error GoTo ErrorHandler
  157.         Set m_clsPrimBuf = m_clsDSound.CreatePrimarySoundBuffer(udtBufDesc)
  158.     On Error GoTo 0
  159.     
  160.     Initialize = True
  161.     Exit Function
  162.     
  163. ErrorHandler:
  164.     Set m_clsDSound = Nothing
  165.     Initialize = False
  166. End Function
  167.  
  168. Private Sub Class_Initialize()
  169.     m_blnReady = InitializeDirectX()
  170.     m_hDSWnd = CreateWindowEx(0, "static", "VB DS Stream", 0, 0, 0, 0, 0, 0, 0, 0, 0)
  171. End Sub
  172.  
  173. Private Sub Class_Terminate()
  174.     Set m_clsPrimBuf = Nothing
  175.     Set m_clsDSound = Nothing
  176.     
  177.     If m_hDSWnd <> 0 Then
  178.         DestroyWindow m_hDSWnd
  179.     End If
  180.  
  181.     DeinitializeDirectX
  182. End Sub
  183.