home *** CD-ROM | disk | FTP | other *** search
/ Microsoft DirectX SDK 7.0 / Dx7.bin / DXF / samples / multimedia / vbsamples / dsound / src / fullduplex / module1.bas < prev    next >
Encoding:
BASIC Source File  |  1999-06-16  |  3.7 KB  |  145 lines

  1. Attribute VB_Name = "basUtils"
  2.  
  3. Private Sub SetTheSoundBuffer(CDesc As DSCBUFFERDESC)
  4. Dim tempDSD As DSBUFFERDESC, tmpWF As WAVEFORMATEX
  5.  
  6.     tmpWF = WaveEx(Full.SoundFMT.lSamplesPerSec, Full.SoundFMT.nChannels, Full.SoundFMT.nBitsPerSample)
  7.  
  8.     tempDSD.lBufferBytes = CDesc.lBufferBytes
  9.     tempDSD.lFlags = DSBCAPS_CTRLPOSITIONNOTIFY Or DSBCAPS_GLOBALFOCUS Or DSBCAPS_GETCURRENTPOSITION2
  10.  
  11.     Set gDSB = gDS.CreateSoundBuffer(tempDSD, tmpWF)
  12.  
  13.     gDSB.Play DSBPLAY_LOOPING
  14.     
  15. End Sub
  16.  
  17. Public Sub InitDirectSound(FormHwnd As Long, GUID As String)
  18.  
  19.  
  20. 'set up the direct sound for playback
  21. Set gDS = gDX.DirectSoundCreate(GUID)
  22.  
  23. 'set the cooperative level
  24. gDS.SetCooperativeLevel FormHwnd, DSSCL_PRIORITY
  25.  
  26.  
  27.  
  28. End Sub
  29.  
  30. Public Sub InitDirectSoundCapture()
  31.  
  32.  
  33. Dim tempformat As WAVEFORMATEX
  34. Dim Notifications(1) As DSBPOSITIONNOTIFY
  35. Dim CaptureDesc As DSCBUFFERDESC
  36.  
  37. ' set up the direct sound capture for recording
  38. Set gDSC = gDX.DirectSoundCaptureCreate(Full.CaptureGUID)
  39.  
  40. '''''''''''''''''''''
  41. 'Grab the capture
  42. 'format
  43. '''''''''''''''''''''
  44. tempformat = WaveEx(Full.CaptureFMT.lSamplesPerSec, Full.CaptureFMT.nChannels, Full.CaptureFMT.nBitsPerSample)
  45. gDSCFW = tempformat
  46. 'tempformat = WaveEx(22050, 2, HBITS)
  47.  
  48. ' Set up the capture description for the capture buffer
  49. CaptureDesc.fxFormat = tempformat
  50. CaptureDesc.lReserved = 0
  51. CaptureDesc.lBufferBytes = tempformat.lAvgBytesPerSec
  52. CaptureDesc.lFlags = DSCBCAPS_WAVEMAPPED
  53.  
  54.  
  55. 'create the capture buffer
  56. Set gDSCB = gDSC.CreateCaptureBuffer(CaptureDesc)
  57.  
  58. Call SetTheSoundBuffer(CaptureDesc)
  59.  
  60. End Sub
  61.  
  62. Public Sub record(DSCB As DirectSoundCaptureBuffer)
  63. On Error GoTo err_out
  64.  
  65.     If DSCB Is Nothing Then
  66.         Call InitDirectSoundCapture
  67.     End If
  68.     
  69.     'start the recording loop
  70.     DSCB.start DSCBSTART_LOOPING
  71. Exit Sub
  72.  
  73. err_out:
  74.     MsgBox Err.Description, vbApplicationModal
  75.     End
  76. End Sub
  77.  
  78.  
  79. Private Function WaveEx(Hz As Long, Channels As Integer, BITS As Integer) As WAVEFORMATEX
  80.  
  81.     WaveEx.nFormatTag = WAVE_FORMAT_PCM
  82.     WaveEx.nChannels = Channels
  83.     WaveEx.lSamplesPerSec = Hz
  84.     WaveEx.nBitsPerSample = BITS
  85.     WaveEx.nBlockAlign = Channels * BITS / 8
  86.     WaveEx.lAvgBytesPerSec = WaveEx.lSamplesPerSec * WaveEx.nBlockAlign
  87.     WaveEx.nSize = 0
  88.  
  89. End Function
  90. Public Sub CopyBuffers(DSOUNDB As DirectSoundBuffer, DSCB As DirectSoundCaptureBuffer, Effect As Integer)
  91.     Dim CURS As DSCURSORS, cnt As Double, tmpB() As Integer
  92.     Dim tCnt As Integer
  93.     
  94.     DSCB.GetCurrentPosition CURS
  95.     
  96.     ReDim gBuffer(CURS.lWrite + 1)
  97.     
  98.     Select Case Effect
  99.     Case 0
  100.         DSCB.ReadBuffer 0, CURS.lWrite + 1, gBuffer(0), DSCBLOCK_DEFAULT
  101.         
  102.         DSOUNDB.WriteBuffer 0, CURS.lWrite + 1, gBuffer(0), DSBLOCK_DEFAULT
  103.         Erase gBuffer
  104.     Case 1
  105.         ReDim tmpB(CURS.lWrite + 1)
  106.         DSCB.ReadBuffer 0, CURS.lWrite + 1, gBuffer(0), DSCBLOCK_DEFAULT
  107.         
  108.  
  109. '        For cnt = 0 To UBound(gBuffer)
  110. '        '''''''''''''''''''''''
  111. '        'Do the effect
  112. '        '''''''''''''''''''''''
  113. '            i = i + 1: If i > 5 Then i = 1
  114. '            If gBuffer(cnt) <> 0 Then tmpB(cnt) = Sin(cnt / i) * i / (gBuffer(cnt))
  115. '            gBuffer(cnt) = tmpB(cnt)
  116. '
  117. '        Next
  118. '
  119. '        For tCnt = CURS.lWrite + 2 To UBound(gBuffer)
  120. '            gBuffer(tCnt) = 0
  121. '        Next
  122.         
  123.         
  124.         DSOUNDB.WriteBuffer 0, CURS.lWrite + 1, tmpB(0), DSBLOCK_DEFAULT
  125.         Erase gBuffer: Erase tmpB
  126.         
  127.     End Select
  128.     
  129. End Sub
  130.  
  131.  
  132. Public Sub StopAll(DSB As DirectSoundBuffer, DSCB As DirectSoundCaptureBuffer)
  133.     On Error Resume Next
  134.     DSB.Stop
  135.     DSCB.Stop
  136.     running = False
  137. End Sub
  138.     
  139. Public Sub PlayBack(DSB As DirectSoundBuffer)
  140.     DSB.Play DSBPLAY_LOOPING
  141. End Sub
  142.  
  143.  
  144.  
  145.