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

  1. VERSION 5.00
  2. Begin VB.Form DSPlayForm 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "DS Play Sound"
  5.    ClientHeight    =   4290
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   4755
  9.    Icon            =   "dstut1.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   4290
  14.    ScaleWidth      =   4755
  15.    ShowInTaskbar   =   1  'True
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin VB.PictureBox Picture1 
  18.       Height          =   1935
  19.       Index           =   1
  20.       Left            =   120
  21.       ScaleHeight     =   1875
  22.       ScaleWidth      =   4395
  23.       TabIndex        =   16
  24.       TabStop         =   0   'False
  25.       Top             =   2160
  26.       Width           =   4455
  27.       Begin VB.HScrollBar scrlPan 
  28.          Height          =   255
  29.          Index           =   1
  30.          LargeChange     =   1000
  31.          Left            =   1080
  32.          Max             =   10000
  33.          Min             =   -10000
  34.          SmallChange     =   500
  35.          TabIndex        =   12
  36.          Top             =   720
  37.          Width           =   2895
  38.       End
  39.       Begin VB.HScrollBar scrlVol 
  40.          Height          =   255
  41.          Index           =   1
  42.          LargeChange     =   20
  43.          Left            =   1080
  44.          Max             =   0
  45.          Min             =   -5000
  46.          SmallChange     =   500
  47.          TabIndex        =   11
  48.          Top             =   360
  49.          Width           =   2895
  50.       End
  51.       Begin VB.CheckBox chLoop 
  52.          Caption         =   "Loop Play"
  53.          Height          =   315
  54.          Index           =   1
  55.          Left            =   3000
  56.          TabIndex        =   10
  57.          Top             =   1200
  58.          Width           =   1455
  59.       End
  60.       Begin VB.CommandButton cmdStop 
  61.          Caption         =   "Stop"
  62.          Height          =   375
  63.          Index           =   1
  64.          Left            =   2040
  65.          TabIndex        =   9
  66.          Top             =   1200
  67.          Width           =   735
  68.       End
  69.       Begin VB.CommandButton cmdPause 
  70.          Caption         =   "Pause"
  71.          Height          =   375
  72.          Index           =   1
  73.          Left            =   1080
  74.          TabIndex        =   8
  75.          Top             =   1200
  76.          Width           =   855
  77.       End
  78.       Begin VB.CommandButton cmdPlay 
  79.          Caption         =   "Play"
  80.          Height          =   375
  81.          Index           =   1
  82.          Left            =   120
  83.          TabIndex        =   7
  84.          Top             =   1200
  85.          Width           =   855
  86.       End
  87.       Begin VB.Label Label2 
  88.          Caption         =   "Pan"
  89.          Height          =   255
  90.          Index           =   1
  91.          Left            =   120
  92.          TabIndex        =   19
  93.          Top             =   720
  94.          Width           =   975
  95.       End
  96.       Begin VB.Label Label1 
  97.          Caption         =   "Volume"
  98.          Height          =   255
  99.          Index           =   1
  100.          Left            =   120
  101.          TabIndex        =   18
  102.          Top             =   360
  103.          Width           =   1095
  104.       End
  105.       Begin VB.Label Label3 
  106.          Caption         =   "SOUND2"
  107.          BeginProperty Font 
  108.             Name            =   "MS Sans Serif"
  109.             Size            =   8.25
  110.             Charset         =   0
  111.             Weight          =   700
  112.             Underline       =   0   'False
  113.             Italic          =   0   'False
  114.             Strikethrough   =   0   'False
  115.          EndProperty
  116.          Height          =   255
  117.          Index           =   1
  118.          Left            =   120
  119.          TabIndex        =   17
  120.          Top             =   0
  121.          Width           =   1695
  122.       End
  123.    End
  124.    Begin VB.PictureBox Picture1 
  125.       Height          =   1935
  126.       Index           =   0
  127.       Left            =   120
  128.       ScaleHeight     =   1875
  129.       ScaleWidth      =   4395
  130.       TabIndex        =   0
  131.       TabStop         =   0   'False
  132.       Top             =   120
  133.       Width           =   4455
  134.       Begin VB.CommandButton cmdPlay 
  135.          Caption         =   "Play"
  136.          Height          =   375
  137.          Index           =   0
  138.          Left            =   120
  139.          TabIndex        =   1
  140.          Top             =   1200
  141.          Width           =   855
  142.       End
  143.       Begin VB.CommandButton cmdPause 
  144.          Caption         =   "Pause"
  145.          Height          =   375
  146.          Index           =   0
  147.          Left            =   1080
  148.          TabIndex        =   2
  149.          Top             =   1200
  150.          Width           =   855
  151.       End
  152.       Begin VB.CommandButton cmdStop 
  153.          Caption         =   "Stop"
  154.          Height          =   375
  155.          Index           =   0
  156.          Left            =   2040
  157.          TabIndex        =   3
  158.          Top             =   1200
  159.          Width           =   735
  160.       End
  161.       Begin VB.CheckBox chLoop 
  162.          Caption         =   "Loop Play"
  163.          Height          =   315
  164.          Index           =   0
  165.          Left            =   3000
  166.          TabIndex        =   4
  167.          Top             =   1200
  168.          Width           =   1455
  169.       End
  170.       Begin VB.HScrollBar scrlVol 
  171.          Height          =   255
  172.          Index           =   0
  173.          LargeChange     =   20
  174.          Left            =   1080
  175.          Max             =   0
  176.          Min             =   -5000
  177.          SmallChange     =   500
  178.          TabIndex        =   5
  179.          Top             =   360
  180.          Width           =   2895
  181.       End
  182.       Begin VB.HScrollBar scrlPan 
  183.          Height          =   255
  184.          Index           =   0
  185.          LargeChange     =   1000
  186.          Left            =   1080
  187.          Max             =   10000
  188.          Min             =   -10000
  189.          SmallChange     =   500
  190.          TabIndex        =   6
  191.          Top             =   720
  192.          Width           =   2895
  193.       End
  194.       Begin VB.Label Label3 
  195.          Caption         =   "SOUND1"
  196.          BeginProperty Font 
  197.             Name            =   "MS Sans Serif"
  198.             Size            =   8.25
  199.             Charset         =   0
  200.             Weight          =   700
  201.             Underline       =   0   'False
  202.             Italic          =   0   'False
  203.             Strikethrough   =   0   'False
  204.          EndProperty
  205.          Height          =   255
  206.          Index           =   0
  207.          Left            =   120
  208.          TabIndex        =   15
  209.          Top             =   0
  210.          Width           =   1695
  211.       End
  212.       Begin VB.Label Label1 
  213.          Caption         =   "Volume"
  214.          Height          =   255
  215.          Index           =   0
  216.          Left            =   120
  217.          TabIndex        =   14
  218.          Top             =   360
  219.          Width           =   1095
  220.       End
  221.       Begin VB.Label Label2 
  222.          Caption         =   "Pan"
  223.          Height          =   255
  224.          Index           =   0
  225.          Left            =   120
  226.          TabIndex        =   13
  227.          Top             =   720
  228.          Width           =   975
  229.       End
  230.    End
  231. Attribute VB_Name = "DSPlayForm"
  232. Attribute VB_GlobalNameSpace = False
  233. Attribute VB_Creatable = False
  234. Attribute VB_PredeclaredId = True
  235. Attribute VB_Exposed = False
  236. '===========================================================
  237. 'Direct Sound Tutorial 1
  238. 'This tutorial will show you how to load a wave file in to a
  239. 'buffer, and then play back the buffered file.
  240. '1/27/1999
  241. '===========================================================
  242. option explicit
  243. Dim m_dx As New DirectX7
  244. Dim m_ds As DirectSound
  245. Dim m_dsBuffer(2) As DirectSoundBuffer
  246. Dim m_bLoaded(2) As Boolean
  247. Dim MediaPath As String
  248. Private Sub Form_Load()
  249.     Me.Show
  250.     FindMediaDir "tuta.wav"
  251.     MediaPath = CurDir$
  252.     '===========================================
  253.     '- Step1 initialize the DirectX object.
  254.     '  We pass in "" to indicate we want the
  255.     '  default sound device.
  256.     '===========================================
  257.     On Local Error Resume Next
  258.     Set m_ds = m_dx.DirectSoundCreate("")
  259.     '========================================================
  260.     '- Step2
  261.     '  We can check for errors which would indicate
  262.     '  a sound card is not present or DirectX is not
  263.     '  installed. The 'On Local Error Resume Next'
  264.     '  statement allows us to check error values immediately
  265.     '  after execution. The error number 0 indicates no error.
  266.     '========================================================
  267.     If Err.Number <> 0 Then
  268.         MsgBox "Unable to start DirectSound. Check to see that your sound card is properly installed"
  269.         End
  270.     End If
  271.     '=======================================================
  272.     '- Step3 set the cooperative level by associating
  273.     '  our dsound object with a window.
  274.     '  This tells windows if the sounds created with
  275.     '  the object should be only heard when the window
  276.     '  has focus, COOPERATING  with other sounds from
  277.     '  other applications, or have EXLUSIVE ACCESS to
  278.     '  the sound card which allows us to change the
  279.     '  output wave format and not allow sounds from
  280.     '  other applications to be heard.
  281.     '
  282.     '  For this application we will use DSSCL_COOPERATIVE
  283.     '  NOTE: This has to be set before you create the
  284.     '  sound buffer.
  285.     '========================================================
  286.     m_ds.SetCooperativeLevel Me.hWnd, DSSCL_PRIORITY
  287. End Sub
  288. Sub LoadWave(i As Integer, sfile As String)
  289.     '========================================================================
  290.     '- Step4 CREATE SOUND BUFFER FROM FILE.
  291.     '  we use the DSBUFFERDESC type to indicate
  292.     '  what features we want the sound to have.
  293.     '  The lFlags member can be used to enable 3d support,
  294.     '  frequency changes, and volume changes.
  295.     '  The DSBCAPS flags indicates we will allow
  296.     '  volume changes, frequency changes, and pan changes
  297.     '  the DDSBCAPS_STATIC -(which is optional in this release
  298.     '  since all  buffers loaded by this method are static) indicates
  299.     '  that we want the entire file loaded into memory.
  300.     '
  301.     '  The function fills in the other members of bufferDesc which lets
  302.     '  us know how large the buffer is.  It also fills in the wave Format
  303.     '  type giving information about the waves quality and if it supports
  304.     '  stereo the function returns an initialized SoundBuffer
  305.     '=========================================================================
  306.     Dim bufferDesc As DSBUFFERDESC
  307.     Dim waveFormat As WAVEFORMATEX
  308.     bufferDesc.lFlags = DSBCAPS_CTRLFREQUENCY or DSBCAPS_CTRLPAN or DSBCAPS_CTRLVOLUME Or DSBCAPS_STATIC
  309.     waveFormat.nFormatTag = WAVE_FORMAT_PCM
  310.     waveFormat.nChannels = 2
  311.     waveFormat.lSamplesPerSec = 22050
  312.     waveFormat.nBitsPerSample = 16
  313.     waveFormat.nBlockAlign = waveFormat.nBitsPerSample / 8 * waveFormat.nChannels
  314.     waveFormat.lAvgBytesPerSec = waveFormat.lSamplesPerSec * waveFormat.nBlockAlign
  315.     Set m_dsBuffer(i) = m_ds.CreateSoundBufferFromFile(sfile, bufferDesc, waveFormat)
  316.     '========================================
  317.     '- Step 5 make sure we have no errors
  318.     '========================================
  319.     If Err.Number <> 0 Then
  320.         MsgBox "unable to find " + sfile
  321.         End
  322.     End If
  323.     scrlPan_Change i
  324.     scrlVol_Change i
  325. End Sub
  326. '===============================
  327. ' Step 8 - PLAYING THE SOUNDS
  328. '===============================
  329. Private Sub cmdPlay_Click(i As Integer)
  330.     '=========================================================
  331.     ' Make sure we loaded our sound
  332.     '=========================================================
  333.     If m_bLoaded(i) = False Then
  334.         m_bLoaded(i) = True
  335.         LoadWave CLng(i), MediaPath + "\tut" + Chr(97 + i) + ".wav"
  336.     End If
  337.             
  338.     '=========================================================
  339.     ' For a sound buffer you can play it two ways.
  340.     ' One is: m_dsBuffer(0).Play DSBPLAY_DEFAULT
  341.     ' this will play the buffer one time with no
  342.     ' looping.  Second is: m_dsBuffer(0).Play DSBPLAY_LOOPING
  343.     ' and this one will continue to loop the buffer.
  344.     '==========================================================
  345.         
  346.     '======================================
  347.     'See if the loop check box is checked
  348.     '======================================
  349.     Dim flag As Long
  350.     flag = 0
  351.     If chLoop(i).Value <> 0 Then flag = 1
  352.     '================================================
  353.     'Play, plays the sound from the current position
  354.     'if the sound was paused using the stop command
  355.     'then play will begin where it last left off
  356.     '================================================
  357.     m_dsBuffer(i).Play flag
  358.     '================================================
  359.     'This format (i) is used because the buffer was
  360.     'set to a array.  The syntax for normal buffer
  361.     'would be the same except for the deletion of the
  362.     'array setting.  I.E. if the buffer was set to
  363.     'DSB then the format for playing would be
  364.     'DSB.Play flag
  365.     'values are:
  366.     '   DSBPLAY_DEFAULT
  367.     '   DSBPLAY_LOOPING
  368.     '================================================
  369. End Sub
  370. '==================
  371. '- Step 9 Add Stop
  372. '==================
  373. Private Sub cmdStop_Click(i As Integer)
  374.     If m_dsBuffer(i) Is Nothing Then Exit Sub
  375.     '================================================
  376.     '- stop does not reset the position of the sound
  377.     '================================================
  378.     m_dsBuffer(i).Stop
  379.     '=======================================================
  380.     '- Here we explicitly reset the position to the beginning
  381.     '  of the sound.
  382.     '=======================================================
  383.     m_dsBuffer(i).SetCurrentPosition 0
  384. End Sub
  385. Private Sub chLoop_Click(Index As Integer)
  386.     If chLoop(Index).Value = 0 Then
  387.         cmdStop_Click Index
  388.     End If
  389. End Sub
  390. '============================
  391. '- Step 10 Add Pause
  392. '============================
  393. Private Sub cmdPause_Click(i As Integer)
  394.     '===================================================
  395.     '- stop does not reset the position of the sound
  396.     '  so play will resume in the middle of the  sound
  397.     '===================================================
  398.     If m_dsBuffer(i) Is Nothing Then Exit Sub
  399.     m_dsBuffer(i).Stop
  400. End Sub
  401. '======================================================================
  402. '- Step 11 Add Handler for setting the volume
  403. '  Changing volume is enabled because we enabled it
  404. '  when we created the buffer otherwise this call would fail.
  405. '  volume is set in db and ranges from -10000 to 0
  406. '  (direct sound doesn't amplify sounds just decreases their volume)
  407. '  because db is a log scale -6000 is almost the same as
  408. '  off and changes near zero have more effect on the volume
  409. '  than those at -6000. we use a -5000 to 0
  410. '======================================================================
  411. Private Sub scrlVol_Change(i As Integer)
  412.     If m_dsBuffer(i) Is Nothing Then Exit Sub
  413.     m_dsBuffer(i).SetVolume scrlVol(i).Value
  414. End Sub
  415. Private Sub scrlVol_Scroll(i As Integer)
  416.     If m_dsBuffer(i) Is Nothing Then Exit Sub
  417.     m_dsBuffer(i).SetVolume scrlVol(i).Value
  418. End Sub
  419. '===============================================================
  420. '- Step 9 Add Handler for Pan
  421. '  changing pan is enabled because we enabled it
  422. '  when we created the buffer otherwise this call would fail
  423. '  volume is set in db and ranges from -10000 to 10000 where
  424. '  -10000 is the left speaker and 10000 is the right speaker
  425. '  like changing volume, values at the extremes aren't as audible
  426. '  as those near 0
  427. '===============================================================
  428. Private Sub scrlPan_Change(i As Integer)
  429.     If m_dsBuffer(i) Is Nothing Then Exit Sub
  430.     m_dsBuffer(i).SetPan scrlPan(i).Value
  431. End Sub
  432. Private Sub scrlPan_Scroll(i As Integer)
  433.     If m_dsBuffer(i) Is Nothing Then Exit Sub
  434.     m_dsBuffer(i).SetPan scrlPan(i).Value
  435. End Sub
  436. '============================
  437. 'This looks for the media
  438. 'path.
  439. '============================
  440. Sub FindMediaDir(sfile As String)
  441.  On Local Error Resume Next
  442.  If Mid$(App.Path, 2, 1) = ":" Then
  443.         ChDrive Mid$(App.Path, 1, 1)
  444.  End If
  445.  ChDir App.Path
  446.  If Dir$(sfile) = "" Then
  447.      ChDir "..\media"
  448.  End If
  449.  If Dir$(sfile) = "" Then
  450.      ChDir "..\..\media"
  451.  End If
  452.  Err.Number = 0
  453. End Sub
  454.