home *** CD-ROM | disk | FTP | other *** search
/ Microsoft DirectX SDK 7.0 / Dx7.bin / DXF / samples / multimedia / vbsamples / dsound / src / capture / form1.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-08-05  |  10.5 KB  |  326 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmMain 
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Capture and Save to File Sample"
  6.    ClientHeight    =   2925
  7.    ClientLeft      =   150
  8.    ClientTop       =   720
  9.    ClientWidth     =   4110
  10.    Icon            =   "Form1.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   2925
  15.    ScaleWidth      =   4110
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin MSComDlg.CommonDialog svFile 
  18.       Left            =   240
  19.       Top             =   840
  20.       _ExtentX        =   847
  21.       _ExtentY        =   847
  22.       _Version        =   393216
  23.       Flags           =   4
  24.    End
  25.    Begin VB.CommandButton cmdStopPlaying 
  26.       Caption         =   "Stop Playing"
  27.       Height          =   375
  28.       Left            =   2520
  29.       TabIndex        =   4
  30.       Top             =   1800
  31.       Width           =   1455
  32.    End
  33.    Begin VB.CommandButton cmdSaveToFile 
  34.       Caption         =   "Save to File"
  35.       Height          =   375
  36.       Left            =   2520
  37.       TabIndex        =   3
  38.       Top             =   2280
  39.       Width           =   1455
  40.    End
  41.    Begin VB.CommandButton cmdPlayRec 
  42.       Caption         =   "Play"
  43.       Height          =   375
  44.       Left            =   2520
  45.       TabIndex        =   2
  46.       Top             =   1320
  47.       Width           =   1455
  48.    End
  49.    Begin VB.CommandButton cmdStopRec 
  50.       Caption         =   "Stop Recording"
  51.       Height          =   375
  52.       Left            =   2520
  53.       TabIndex        =   1
  54.       Top             =   840
  55.       Width           =   1455
  56.    End
  57.    Begin VB.Timer tmrCount 
  58.       Left            =   840
  59.       Top             =   840
  60.    End
  61.    Begin VB.CommandButton cmdStartRec 
  62.       Caption         =   "Start Recording"
  63.       Height          =   375
  64.       Left            =   2520
  65.       TabIndex        =   0
  66.       Top             =   360
  67.       Width           =   1455
  68.    End
  69.    Begin VB.Label lblLTime 
  70.       Alignment       =   1  'Right Justify
  71.       Caption         =   "Time:"
  72.       Height          =   255
  73.       Left            =   240
  74.       TabIndex        =   6
  75.       Top             =   360
  76.       Width           =   795
  77.    End
  78.    Begin VB.Label lblTIME 
  79.       Caption         =   "Label1"
  80.       Height          =   255
  81.       Left            =   1140
  82.       TabIndex        =   5
  83.       Top             =   360
  84.       Width           =   795
  85.    End
  86.    Begin VB.Menu mnuFile 
  87.       Caption         =   "File"
  88.       Begin VB.Menu mnuExit 
  89.          Caption         =   "E&xit"
  90.       End
  91.    End
  92. Attribute VB_Name = "frmMain"
  93. Attribute VB_GlobalNameSpace = False
  94. Attribute VB_Creatable = False
  95. Attribute VB_PredeclaredId = True
  96. Attribute VB_Exposed = False
  97. ''--------------------------------------------------------
  98. ''This sample will show how to use the "SaveToFile"
  99. ''--------------------------------------------------------
  100. Dim dx As New DirectX7
  101. Dim ds As DirectSound
  102. Dim dsb As DirectSoundBuffer
  103. Dim dsd As DSBUFFERDESC
  104. Dim dsc As DirectSoundCapture
  105. Dim dscb As DirectSoundCaptureBuffer
  106. Dim dscd As DSCBUFFERDESC
  107. Dim CaptureWave As WAVEFORMATEX
  108. Dim capCURS As DSCURSORS
  109. Dim ByteBuffer() As Integer
  110. Dim CNT As Integer
  111. Dim cCaps As DSCCAPS
  112. Dim gfPlay As Boolean
  113. Private Sub cmdPlayRec_Click()
  114.     ''----------------------------------------
  115.     '' convert the data from a capture buffer
  116.     '' to a sound buffer
  117.     ''----------------------------------------
  118.     ConvertToSBuffer
  119.     '' did the sound buffer get created?
  120.     If dsb Is Nothing Then
  121.         Exit Sub
  122.     Else
  123.         dsb.Play DSBPLAY_DEFAULT
  124.         tmrCount.Enabled = True
  125.         CNT = 0
  126.         lblTIME.Caption = vbNullString
  127.         If gfPlay Then cmdStopPlaying.Enabled = True
  128.     End If
  129. End Sub
  130. Private Sub cmdSaveToFile_Click()
  131. On Error Resume Next
  132.     Dim FileLocal As String
  133.     ConvertToSBuffer
  134.     If dsb Is Nothing Then Exit Sub
  135.     cmdStopPlaying.Enabled = False
  136.     tmrCount.Enabled = False
  137.     lblTIME.Caption = vbNullString
  138.     CNT = 0
  139.     If dsb Is Nothing Then
  140.         MsgBox "Please record a sound first"
  141.         Exit Sub
  142.     End If
  143.     'common dialog control
  144.     svFile.Filter = "*.wav"
  145.     svFile.DialogTitle = "Save Wave File"
  146.     svFile.ShowSave
  147.     If Right(svFile.filename, 4) <> ".wav" And svFile.filename <> vbNullString Then
  148.         FileLocal = svFile.filename
  149.         FileLocal = FileLocal & ".wav"
  150.     Else
  151.         FileLocal = svFile.filename
  152.     End If
  153.     'FileLocal = InputBox("Please enter the location, and file name you want the file saved as.", "SAVE", "c:\windows\temp\test.wav")
  154.     If FileLocal = vbNullString Then Exit Sub
  155.     If Mid(FileLocal, 2, 1) <> ":" Then Exit Sub
  156.     If Right(FileLocal, 3) <> "wav" Then
  157.         MsgBox "Please enter a correct name ie something.wav", vbApplicationModal
  158.         Exit Sub
  159.     End If
  160.         
  161.     dsb.SaveToFile FileLocal
  162. End Sub
  163. Private Sub cmdStartRec_Click()
  164.     Set dscb = Nothing
  165.     Call InitCapture
  166.     dscb.start DSCBSTART_DEFAULT
  167.     tmrCount.Interval = 1000
  168.     tmrCount.Enabled = True
  169.     cmdStopRec.Enabled = True
  170.     cmdStartRec.Enabled = False
  171. End Sub
  172. Private Sub cmdStopPlaying_Click()
  173.     If dsb Is Nothing Then Exit Sub
  174.     Dim l_st As Long
  175.     Dim l_soundStatus As Long
  176.     ''--- see if the capture buffer is running
  177.     l_st = dscb.GetStatus()
  178.     If (l_st And DSCBSTATUS_CAPTURING) Then
  179.         dscb.Stop
  180.     End If
  181.     ''-- see if the sound buffer is playing
  182.     l_soundStatus = dsb.GetStatus()
  183.     If (l_soundStatus And DSBSTATUS_PLAYING) Then
  184.         dsb.Stop
  185.         dsb.SetCurrentPosition 0
  186.     End If
  187.     tmrCount.Enabled = False
  188.     CNT = 0
  189.     lblTIME.Caption = vbNullString
  190.     cmdStopPlaying.Enabled = False
  191. End Sub
  192. Private Sub cmdStopRec_Click()
  193.     Dim l_bufferS As Long
  194.     If dscb Is Nothing Then Exit Sub
  195.     cmdSaveToFile.Enabled = True
  196.     If gfPlay Then cmdPlayRec.Enabled = True
  197.     ''cmdStopPlaying.Enabled = True
  198.     '' is the buffer going?
  199.     l_bufferS = dscb.GetStatus()
  200.     If (l_bufferS And DSCBSTATUS_CAPTURING) Then
  201.         dscb.Stop
  202.     End If
  203.     tmrCount.Enabled = False
  204.     CNT = 0
  205.     lblTIME.Caption = vbNullString
  206.     cmdStartRec.Enabled = True
  207.     cmdStopRec.Enabled = False
  208. End Sub
  209. Private Sub Form_Load()
  210.     On Local Error GoTo errOut
  211.     Set dsc = dx.DirectSoundCaptureCreate(vbNullString)
  212.     On Error Resume Next
  213.     Set ds = dx.DirectSoundCreate(vbNullString)
  214.     If Err.Number = DSERR_ALLOCATED Then 'The card isn't supporting full duplex
  215.         gfPlay = False
  216.         MsgBox "This card does not support full duplex.  You may still record sound.", vbOKOnly Or vbInformation, "No full duplex"
  217.     Else
  218.         gfPlay = True
  219.         ds.SetCooperativeLevel Me.hWnd, DSSCL_NORMAL
  220.     End If
  221.     On Local Error GoTo errOut
  222.     InitCapture
  223.     cmdSaveToFile.Enabled = False
  224.     cmdPlayRec.Enabled = False
  225.     cmdStopPlaying.Enabled = False
  226.     cmdStopRec.Enabled = False
  227.     lblTIME.Caption = vbNullString
  228.     Exit Sub
  229. errOut:
  230.     MsgBox "Unable to initialize sound card for capture.  Exiting this application", vbOKOnly Or vbCritical
  231.     End
  232. End Sub
  233. Private Sub ConvertToSBuffer()
  234.     Dim l_captureS As Long
  235.     '' are we running?
  236.     l_captureS = dscb.GetStatus()
  237.     If (l_captureS And DSCBSTATUS_CAPTURING) Then
  238.         dscb.Stop
  239.     End If
  240.     '' get the capture info
  241.     dscb.GetCurrentPosition capCURS
  242.     dsd.lBufferBytes = capCURS.lWrite * dscd.fxFormat.nBlockAlign
  243.     dsd.lFlags = DSBCAPS_CTRLVOLUME Or DSBCAPS_STATIC
  244.     If capCURS.lWrite = 0 Then
  245.         Exit Sub
  246.     End If
  247.     Set dsb = ds.CreateSoundBuffer(dsd, dscd.fxFormat)
  248.     ReDim ByteBuffer(capCURS.lWrite * dscd.fxFormat.nBlockAlign + 1)
  249.     dscb.ReadBuffer 0, capCURS.lWrite * dscd.fxFormat.nBlockAlign, ByteBuffer(0), DSCBLOCK_DEFAULT
  250.     dsb.WriteBuffer 0, capCURS.lWrite * dscd.fxFormat.nBlockAlign, ByteBuffer(0), DSBLOCK_DEFAULT
  251.     ''Set dscb = Nothing
  252. End Sub
  253. Private Function WaveEx(Hz As Long, Channels As Integer, BITS As Integer) As WAVEFORMATEX
  254.     WaveEx.nFormatTag = WAVE_FORMAT_PCM
  255.     WaveEx.nChannels = Channels
  256.     WaveEx.lSamplesPerSec = Hz
  257.     WaveEx.nBitsPerSample = BITS
  258.     WaveEx.nBlockAlign = Channels * BITS / 8
  259.     WaveEx.lAvgBytesPerSec = WaveEx.lSamplesPerSec * WaveEx.nBlockAlign
  260.     WaveEx.nSize = 0
  261. End Function
  262. Private Sub InitCapture()
  263.     'set the capture buffer
  264.     dsc.GetCaps cCaps
  265.     If cCaps.lFormats And WAVE_FORMAT_2M08 Then
  266.         CaptureWave = WaveEx(22050, 1, 8)
  267.     ElseIf cCaps.lFormats And WAVE_FORMAT_1M08 Then
  268.         CaptureWave = WaveEx(11025, 1, 8)
  269.     Else
  270.         MsgBox "Capture is not supported with your sound card!", vbApplicationModal
  271.         End
  272.     End If
  273.     dscd.fxFormat = CaptureWave
  274.     dscd.lBufferBytes = CaptureWave.lAvgBytesPerSec * 20
  275.     dscd.lFlags = DSCBCAPS_WAVEMAPPED
  276.     Set dscb = dsc.CreateCaptureBuffer(dscd)
  277. End Sub
  278. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  279.     Call CleanUp
  280.     End
  281. End Sub
  282. Private Sub CleanUp()
  283.     ''Clean up all the stuff
  284.     Set dx = Nothing
  285.     Set ds = Nothing
  286.     Set dsb = Nothing
  287.     Set dsc = Nothing
  288.     Set dscb = Nothing
  289.     Erase ByteBuffer
  290. End Sub
  291. Private Sub mnuExit_Click()
  292.     Unload Me
  293. End Sub
  294. Private Sub tmrCount_Timer()
  295. On Error Resume Next
  296.     CNT = CNT + 1
  297.     If CNT = 19 Then
  298.         dscb.Stop
  299.         lblTIME.Caption = "Full"
  300.         frmMain.Refresh
  301.         tmrCount.Enabled = False
  302.                 
  303.         cmdSaveToFile.Enabled = True
  304.         If gfPlay Then cmdPlayRec.Enabled = True
  305.         If gfPlay Then cmdStopPlaying.Enabled = True
  306.         
  307.         Exit Sub
  308.     End If
  309.     lblTIME.Caption = CNT
  310.     ''check the status of the sound buffer
  311.     Dim l_sBs As Long
  312.     If Not (dsb Is Nothing) Then
  313.         l_sBs = dsb.GetStatus()
  314.         If (l_sBs And DSBSTATUS_PLAYING) Then
  315.         Else
  316.             If cmdStartRec.Enabled = True Then
  317.                 tmrCount.Enabled = False
  318.                 CNT = 1
  319.                 lblTIME.Caption = vbNullString
  320.                 cmdStopPlaying.Enabled = False
  321.             End If
  322.         End If
  323.     End If
  324.         
  325. End Sub
  326.