home *** CD-ROM | disk | FTP | other *** search
/ Microsoft DirectX SDK 7.0 / Dx7.bin / DXF / samples / multimedia / vbsamples / dsound / src / streamto / basstream.bas next >
Encoding:
BASIC Source File  |  1999-07-29  |  4.7 KB  |  187 lines

  1. Attribute VB_Name = "basStream"
  2. ''----------------------------------------
  3. ''Variables
  4. ''----------------------------------------
  5.  
  6. Global gDX As New DirectX7
  7. Global gDSC As DirectSoundCapture
  8. Global gDSCB As DirectSoundCaptureBuffer
  9. Global gDSCBD As DSCBUFFERDESC
  10. Public EventID(1) As Long
  11. Public EVNT(1) As DSBPOSITIONNOTIFY
  12. Public MEM() As Byte
  13. Global f%
  14. Public WaveF1 As WAVEFORMATEX
  15. Public Buffer() As Byte
  16. Public cur As DSCURSORS
  17.  
  18.  
  19. ''----------------------------------------
  20. ''Wave header info
  21. ''----------------------------------------
  22. Private Type FileHeader
  23.     dwRiff As Long
  24.     dwFileSize As Long
  25.     dwWave As Long
  26.     dwFormat As Long
  27.     dwFormatLength As Long
  28.     wFormatTag As Integer
  29.     nChannels As Integer
  30.     nSamplesPerSec As Long
  31.     nAvgBytesPerSec As Long
  32.     nBlockAlign As Integer
  33.     wBitsPerSample As Integer
  34.     dwData As Long
  35.     dwDataLength As Long
  36. End Type
  37.  
  38.  
  39. ''-------------------------------------
  40. ''init the capture buffer
  41. ''-------------------------------------
  42. Public Sub Init(hWnd As Long)
  43.     
  44.     On Local Error GoTo ErrOut
  45.     With WaveF1
  46.         .nChannels = 1
  47.         .nFormatTag = WAVE_FORMAT_PCM
  48.         .lSamplesPerSec = 22050
  49.         .lAvgBytesPerSec = 22050
  50.         .nBlockAlign = 1
  51.         .nBitsPerSample = 8
  52.     End With
  53.     
  54.     'capture buffer
  55.     Set gDSC = gDX.DirectSoundCaptureCreate(vbNullString)
  56.     
  57.     With gDSCBD
  58.         .lFlags = DSCBCAPS_WAVEMAPPED
  59.         .lBufferBytes = WaveF1.lAvgBytesPerSec * 2
  60.         .fxFormat = WaveF1
  61.     End With
  62.     
  63.     Set gDSCB = gDSC.CreateCaptureBuffer(gDSCBD)
  64.     
  65.     ''---------------------------------
  66.     ''Send the info to the file
  67.     ''---------------------------------
  68.     Call StreamToTempFile
  69.     Exit Sub
  70.  
  71. ErrOut:
  72.     MsgBox "Could not create the default sound device", vbOKOnly Or vbCritical, "Cannot create"
  73.     End
  74.         
  75. End Sub
  76.  
  77. ''--------------------------------------
  78. ''Close the open files
  79. ''--------------------------------------
  80. Public Sub CloseFiles()
  81.     On Local Error Resume Next
  82.     Close #fFile_1
  83.     Close #fFile_2
  84.     Close #f
  85. End Sub
  86.  
  87. ''--------------------------------------
  88. ''Delete the temp file
  89. ''--------------------------------------
  90. Public Sub KillTempFile()
  91.     On Local Error Resume Next
  92.     Close #f
  93.     Kill App.Path + "\tmp.tmp"
  94. End Sub
  95.  
  96.  
  97. ''--------------------------------------
  98. ''Open a temp file for output
  99. ''--------------------------------------
  100. Public Sub StreamToTempFile()
  101.     'open a temp file for streaming input
  102.     f% = FreeFile
  103.     Open App.Path + "\tmp.tmp" For Binary Access Write As #f
  104. End Sub
  105.  
  106.  
  107. ''--------------------------------------
  108. ''send the buffer data to a file
  109. ''--------------------------------------
  110. Public Sub CopyBuffer(part As Integer)
  111.     Static sz As Double
  112.     
  113.     gDSCB.GetCurrentPosition cur
  114.     
  115.     ReDim Buffer(cur.lWrite - 1)
  116.     
  117.     gDSCB.ReadBuffer 0, cur.lWrite, Buffer(0), DSCBLOCK_DEFAULT
  118.     
  119.     
  120.     Put #f, , Buffer
  121.     
  122.     Erase Buffer
  123.     
  124.     sz = sz + cur.lWrite
  125.     frmMain.lblSize = Str$(sz) ' send size info to the form label
  126.  
  127. End Sub
  128.  
  129.  
  130.  
  131. ''--------------------------------------------------
  132. ''Save the whole thing to a file
  133. ''--------------------------------------------------
  134.  
  135. Public Sub SaveToFileAsStream(sName As String)
  136.     
  137.     Dim fh As FileHeader, Status As Long
  138.     Dim WF As WAVEFORMATEX
  139.     Dim fFile_1%, fFile_2%, File_1Holder() As Byte
  140.     
  141.     Status = gDSCB.GetStatus
  142.  
  143.     If (Status And DSCBSTATUS_CAPTURING) Then gDSCB.Stop
  144.     
  145.     'get the wave data from the tmp file
  146.     fFile_1% = FreeFile
  147.     
  148.     Open App.Path + "\tmp.tmp" For Binary Access Read As #fFile_1
  149.     ReDim File_1Holder(LOF(fFile_1%) + 1)
  150.     
  151.     Get #fFile_1, , File_1Holder
  152.     
  153.     Close #fFile_1
  154.     
  155.     
  156.     WF = WaveF1
  157.         
  158.     
  159.     fFile_2% = FreeFile
  160.     
  161.     Open App.Path + "\" + sName For Binary Access Write As #fFile_2
  162.     
  163.     fh.dwRiff = &H46464952            '                // RIFF
  164.     fh.dwWave = &H45564157            '                // WAVE
  165.     fh.dwFormat = &H20746D66          '                // fmt_chnk
  166.     fh.dwFormatLength = 16
  167.     fh.wFormatTag = WAVE_FORMAT_PCM
  168.     fh.nChannels = WF.nChannels
  169.     fh.nSamplesPerSec = WF.lSamplesPerSec
  170.     fh.wBitsPerSample = WF.nBitsPerSample
  171.     fh.nBlockAlign = fh.wBitsPerSample / 8 * fh.nChannels
  172.     fh.nAvgBytesPerSec = fh.nSamplesPerSec * fh.nBlockAlign
  173.     fh.dwData = &H61746164            '                 // data_chnk
  174.     fh.dwDataLength = UBound(File_1Holder)
  175.     fh.dwFileSize = UBound(File_1Holder) + Len(fh)
  176.     
  177.     'add the info to the second file
  178.     Put #fFile_2, , fh
  179.     Put #fFile_2, , File_1Holder
  180.     
  181.     Close #fFile_2
  182.     
  183.     Call KillTempFile
  184.     
  185. End Sub
  186.  
  187.