home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8vbsdk.exe / samples / multimedia / vbsamples / directsound / tutorials / tut1 / frmcapture.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2000-10-02  |  7.6 KB  |  218 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form frmCapture 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "Audio Capture Tutorial"
  6.    ClientHeight    =   1395
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   4650
  10.    Icon            =   "frmCapture.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   1395
  15.    ScaleWidth      =   4650
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin MSComDlg.CommonDialog cdlSave 
  18.       Left            =   4140
  19.       Top             =   0
  20.       _ExtentX        =   847
  21.       _ExtentY        =   847
  22.       _Version        =   393216
  23.       CancelError     =   -1  'True
  24.    End
  25.    Begin VB.CommandButton cmdSave 
  26.       Caption         =   "Sa&ve"
  27.       Enabled         =   0   'False
  28.       Height          =   375
  29.       Left            =   3038
  30.       TabIndex        =   2
  31.       Top             =   840
  32.       Width           =   975
  33.    End
  34.    Begin VB.CommandButton cmdStop 
  35.       Caption         =   "&Stop"
  36.       Enabled         =   0   'False
  37.       Height          =   375
  38.       Left            =   1898
  39.       TabIndex        =   1
  40.       Top             =   840
  41.       Width           =   975
  42.    End
  43.    Begin VB.CommandButton cmdStart 
  44.       Caption         =   "&Record"
  45.       Height          =   375
  46.       Left            =   638
  47.       TabIndex        =   0
  48.       Top             =   840
  49.       Width           =   975
  50.    End
  51.    Begin VB.Label lbl 
  52.       BackStyle       =   0  'Transparent
  53.       Caption         =   "Audio Capture Tutorial"
  54.       Height          =   255
  55.       Index           =   0
  56.       Left            =   660
  57.       TabIndex        =   4
  58.       Top             =   120
  59.       Width           =   2655
  60.    End
  61.    Begin VB.Label lbl 
  62.       BackStyle       =   0  'Transparent
  63.       Caption         =   "Copyright 
  64.  2000, Microsoft Corporation, All Rights Reserved."
  65.       Height          =   435
  66.       Index           =   2
  67.       Left            =   660
  68.       TabIndex        =   3
  69.       Top             =   360
  70.       Width           =   4335
  71.    End
  72.    Begin VB.Image Image1 
  73.       Height          =   480
  74.       Left            =   120
  75.       Picture         =   "frmCapture.frx":0442
  76.       Top             =   120
  77.       Width           =   480
  78.    End
  79. Attribute VB_Name = "frmCapture"
  80. Attribute VB_GlobalNameSpace = False
  81. Attribute VB_Creatable = False
  82. Attribute VB_PredeclaredId = True
  83. Attribute VB_Exposed = False
  84. Option Explicit
  85. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  86. '  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
  87. '  File:       frmCapture.frm
  88. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  89. 'This tutorial will show basic functionality.  You will capture a buffer to memory,
  90. 'and then write it out to a file.
  91. 'Variable declarations for our app
  92. Private dx As New DirectX8
  93. Private dsc As DirectSoundCapture8
  94. Private dscb As DirectSoundCaptureBuffer8
  95. Private dscd As DSCBUFFERDESC
  96. Private capFormat As WAVEFORMATEX
  97. Private ds As DirectSound8
  98. Private Sub InitCapture()
  99.     Dim cCaps As DSCCAPS
  100.     On Local Error Resume Next
  101.     'We need to create a direct sound object before the capture object
  102.     If ds Is Nothing Then Set ds = dx.DirectSoundCreate(vbNullString)
  103.     If Err Then
  104.         MsgBox "Unable to create a DirectSound object", vbOKOnly Or vbCritical, "Cannot continue"
  105.         Cleanup
  106.         End
  107.     End If
  108.     'First we need to create our capture buffer on the default object
  109.     Set dsc = dx.DirectSoundCaptureCreate(vbNullString)
  110.     If Err Then
  111.         MsgBox "Unable to create a Capture object", vbOKOnly Or vbCritical, "Cannot continue"
  112.         Cleanup
  113.         End
  114.     End If
  115.     'Lets get the caps for our object
  116.     dsc.GetCaps cCaps
  117.     'Check for a capture format we will support in the sample
  118.     If cCaps.lFormats And WAVE_FORMAT_4M08 Then
  119.         capFormat = CreateWaveFormatEx(44100, 1, 8)
  120.     ElseIf cCaps.lFormats And WAVE_FORMAT_2M08 Then
  121.         capFormat = CreateWaveFormatEx(22050, 1, 8)
  122.     ElseIf cCaps.lFormats And WAVE_FORMAT_1M08 Then
  123.         capFormat = CreateWaveFormatEx(11025, 1, 8)
  124.     Else
  125.         MsgBox "Could not get the caps we need on this card.", vbOKOnly Or vbCritical, "Exiting."
  126.         Cleanup
  127.         End
  128.     End If
  129. End Sub
  130. Private Sub CreateCaptureBuffer()
  131.     dscd.fxFormat = capFormat
  132.     dscd.lBufferBytes = capFormat.lAvgBytesPerSec * 20
  133.     dscd.lFlags = DSCBCAPS_WAVEMAPPED
  134.     Set dscb = dsc.CreateCaptureBuffer(dscd)
  135. End Sub
  136. Private Sub Cleanup()
  137.     Set ds = Nothing
  138.     Set dscb = Nothing
  139.     Set dsc = Nothing
  140.     Set dx = Nothing
  141. End Sub
  142. Private Function CreateWaveFormatEx(Hz As Long, Channels As Integer, BITS As Integer) As WAVEFORMATEX
  143.     'Create a WaveFormatEX structure using the vars we provide
  144.     With CreateWaveFormatEx
  145.         .nFormatTag = WAVE_FORMAT_PCM
  146.         .nChannels = Channels
  147.         .lSamplesPerSec = Hz
  148.         .nBitsPerSample = BITS
  149.         .nBlockAlign = Channels * BITS / 8
  150.         .lAvgBytesPerSec = .lSamplesPerSec * .nBlockAlign
  151.         .nSize = 0
  152.     End With
  153. End Function
  154. Private Sub cmdSave_Click()
  155.     On Local Error Resume Next
  156.     With cdlSave
  157.         'Set our initial properties
  158.         .FileName = vbNullString
  159.         .flags = cdlOFNHideReadOnly
  160.         .Filter = "Wave files(*.WAV)|*.wav"
  161.         .ShowOpen
  162.         If Err Then Exit Sub 'We clicked cancel
  163.         If .FileName = vbNullString Then Exit Sub 'No file
  164.         'Save the file to disk
  165.         GetSoundBufferFromCapture(dscb).SaveToFile .FileName
  166.     End With
  167. End Sub
  168. Private Sub cmdStart_Click()
  169.     'We want to record sound now.
  170.     'First we need to get rid of any sound we may have
  171.     Set dscb = Nothing
  172.     'Now get our capture buffer once more
  173.     CreateCaptureBuffer
  174.     'Now start recording
  175.     dscb.Start DSCBSTART_DEFAULT
  176.     'Disable/Enable our buttons accordingly
  177.     cmdStop.Enabled = True
  178.     cmdStart.Enabled = False
  179.     cmdSave.Enabled = False
  180. End Sub
  181. Private Sub cmdStop_Click()
  182.     Dim lbufferStatus As Long
  183.     'Stop the buffer
  184.     dscb.Stop
  185.     'Disable/Enable our buttons accordingly
  186.     cmdStop.Enabled = False
  187.     cmdStart.Enabled = True
  188.     cmdSave.Enabled = True
  189. End Sub
  190. Private Sub Form_Load()
  191.     'Lets init our capture device
  192.     InitCapture
  193. End Sub
  194. Private Sub Form_Unload(Cancel As Integer)
  195.     Cleanup
  196. End Sub
  197. Private Function GetSoundBufferFromCapture(ByVal oCaptureBuffer As DirectSoundCaptureBuffer8) As DirectSoundSecondaryBuffer8
  198.     Dim lbufferStatus As Long
  199.     Dim capCURS As DSCURSORS
  200.     Dim dsd As DSBUFFERDESC
  201.     Dim ByteBuffer() As Integer 'Our digital data from our capture buffer
  202.     'Are we still capturing? If so, stop
  203.     oCaptureBuffer.Stop
  204.     'Get the capture info
  205.     oCaptureBuffer.GetCurrentPosition capCURS
  206.     dsd.lBufferBytes = capCURS.lWrite + 1
  207.     dsd.fxFormat = dscd.fxFormat
  208.     'If there is nothing to write, then exit
  209.     If capCURS.lWrite = 0 Then Exit Function
  210.     Set GetSoundBufferFromCapture = ds.CreateSoundBuffer(dsd)
  211.     'Set the size for our new Data
  212.     ReDim ByteBuffer(capCURS.lWrite)
  213.     'Read the data from our capture buffer
  214.     oCaptureBuffer.ReadBuffer 0, capCURS.lWrite, ByteBuffer(0), DSCBLOCK_DEFAULT
  215.     'Write the data to our sound buffer
  216.     GetSoundBufferFromCapture.WriteBuffer 0, capCURS.lWrite, ByteBuffer(0), DSBLOCK_DEFAULT
  217. End Function
  218.