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

  1. VERSION 5.00
  2. Begin VB.Form DS3DPositionForm 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "DS 3D Positioning"
  5.    ClientHeight    =   4365
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   8400
  9.    Icon            =   "dstut2.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   4365
  14.    ScaleWidth      =   8400
  15.    ShowInTaskbar   =   1  'True
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin VB.PictureBox Picture2 
  18.       BackColor       =   &H00FFFFFF&
  19.       FillStyle       =   7  'Diagonal Cross
  20.       Height          =   2775
  21.       Left            =   4800
  22.       ScaleHeight     =   181
  23.       ScaleMode       =   3  'Pixel
  24.       ScaleWidth      =   213
  25.       TabIndex        =   0
  26.       TabStop         =   0   'False
  27.       Top             =   1320
  28.       Width           =   3255
  29.    End
  30.    Begin VB.PictureBox Picture1 
  31.       Height          =   1935
  32.       Index           =   1
  33.       Left            =   120
  34.       ScaleHeight     =   1875
  35.       ScaleWidth      =   4395
  36.       TabIndex        =   20
  37.       TabStop         =   0   'False
  38.       Top             =   2160
  39.       Width           =   4455
  40.       Begin VB.HScrollBar scrlAngle 
  41.          Height          =   255
  42.          Index           =   1
  43.          LargeChange     =   20
  44.          Left            =   1080
  45.          Max             =   360
  46.          Min             =   -360
  47.          SmallChange     =   10
  48.          TabIndex        =   16
  49.          Top             =   720
  50.          Value           =   -90
  51.          Width           =   2895
  52.       End
  53.       Begin VB.HScrollBar scrlVol 
  54.          Height          =   255
  55.          Index           =   1
  56.          LargeChange     =   20
  57.          Left            =   1080
  58.          Max             =   0
  59.          Min             =   -3000
  60.          SmallChange     =   500
  61.          TabIndex        =   15
  62.          Top             =   360
  63.          Width           =   2895
  64.       End
  65.       Begin VB.CheckBox chLoop 
  66.          Caption         =   "Loop Play"
  67.          Height          =   315
  68.          Index           =   1
  69.          Left            =   3000
  70.          TabIndex        =   14
  71.          Top             =   1200
  72.          Width           =   1455
  73.       End
  74.       Begin VB.CommandButton cmdStop 
  75.          Caption         =   "Stop"
  76.          Height          =   375
  77.          Index           =   1
  78.          Left            =   2040
  79.          TabIndex        =   13
  80.          Top             =   1200
  81.          Width           =   735
  82.       End
  83.       Begin VB.CommandButton cmdPause 
  84.          Caption         =   "Pause"
  85.          Height          =   375
  86.          Index           =   1
  87.          Left            =   1080
  88.          TabIndex        =   12
  89.          Top             =   1200
  90.          Width           =   855
  91.       End
  92.       Begin VB.CommandButton cmdPlay 
  93.          Caption         =   "Play"
  94.          Height          =   375
  95.          Index           =   1
  96.          Left            =   120
  97.          TabIndex        =   11
  98.          Top             =   1200
  99.          Width           =   855
  100.       End
  101.       Begin VB.Label Label2 
  102.          Caption         =   "Direction"
  103.          Height          =   255
  104.          Index           =   1
  105.          Left            =   120
  106.          TabIndex        =   23
  107.          Top             =   720
  108.          Width           =   975
  109.       End
  110.       Begin VB.Label Label1 
  111.          Caption         =   "Volume"
  112.          Height          =   255
  113.          Index           =   1
  114.          Left            =   120
  115.          TabIndex        =   22
  116.          Top             =   360
  117.          Width           =   1095
  118.       End
  119.       Begin VB.Label Label3 
  120.          Caption         =   "SOUND2"
  121.          BeginProperty Font 
  122.             Name            =   "MS Sans Serif"
  123.             Size            =   8.25
  124.             Charset         =   0
  125.             Weight          =   700
  126.             Underline       =   0   'False
  127.             Italic          =   0   'False
  128.             Strikethrough   =   0   'False
  129.          EndProperty
  130.          ForeColor       =   &H00FF0000&
  131.          Height          =   255
  132.          Index           =   1
  133.          Left            =   120
  134.          TabIndex        =   21
  135.          Top             =   0
  136.          Width           =   1695
  137.       End
  138.    End
  139.    Begin VB.PictureBox Picture1 
  140.       Height          =   1935
  141.       Index           =   0
  142.       Left            =   120
  143.       ScaleHeight     =   1875
  144.       ScaleWidth      =   4395
  145.       TabIndex        =   4
  146.       TabStop         =   0   'False
  147.       Top             =   120
  148.       Width           =   4455
  149.       Begin VB.CommandButton cmdPlay 
  150.          Caption         =   "Play"
  151.          Height          =   375
  152.          Index           =   0
  153.          Left            =   120
  154.          TabIndex        =   5
  155.          Top             =   1200
  156.          Width           =   855
  157.       End
  158.       Begin VB.CommandButton cmdPause 
  159.          Caption         =   "Pause"
  160.          Height          =   375
  161.          Index           =   0
  162.          Left            =   1080
  163.          TabIndex        =   6
  164.          Top             =   1200
  165.          Width           =   855
  166.       End
  167.       Begin VB.CommandButton cmdStop 
  168.          Caption         =   "Stop"
  169.          Height          =   375
  170.          Index           =   0
  171.          Left            =   2040
  172.          TabIndex        =   7
  173.          Top             =   1200
  174.          Width           =   735
  175.       End
  176.       Begin VB.CheckBox chLoop 
  177.          Caption         =   "Loop Play"
  178.          Height          =   315
  179.          Index           =   0
  180.          Left            =   3000
  181.          TabIndex        =   8
  182.          Top             =   1200
  183.          Width           =   1455
  184.       End
  185.       Begin VB.HScrollBar scrlVol 
  186.          Height          =   255
  187.          Index           =   0
  188.          LargeChange     =   20
  189.          Left            =   1080
  190.          Max             =   0
  191.          Min             =   -3000
  192.          SmallChange     =   500
  193.          TabIndex        =   9
  194.          Top             =   360
  195.          Width           =   2895
  196.       End
  197.       Begin VB.HScrollBar scrlAngle 
  198.          Height          =   255
  199.          Index           =   0
  200.          LargeChange     =   20
  201.          Left            =   1080
  202.          Max             =   360
  203.          Min             =   -360
  204.          SmallChange     =   10
  205.          TabIndex        =   10
  206.          Top             =   720
  207.          Value           =   -90
  208.          Width           =   2895
  209.       End
  210.       Begin VB.Label Label3 
  211.          Caption         =   "SOUND1"
  212.          BeginProperty Font 
  213.             Name            =   "MS Sans Serif"
  214.             Size            =   8.25
  215.             Charset         =   0
  216.             Weight          =   700
  217.             Underline       =   0   'False
  218.             Italic          =   0   'False
  219.             Strikethrough   =   0   'False
  220.          EndProperty
  221.          ForeColor       =   &H000000FF&
  222.          Height          =   255
  223.          Index           =   0
  224.          Left            =   120
  225.          TabIndex        =   19
  226.          Top             =   0
  227.          Width           =   1695
  228.       End
  229.       Begin VB.Label Label1 
  230.          Caption         =   "Volume"
  231.          Height          =   255
  232.          Index           =   0
  233.          Left            =   120
  234.          TabIndex        =   18
  235.          Top             =   360
  236.          Width           =   1095
  237.       End
  238.       Begin VB.Label Label2 
  239.          Caption         =   "Direction"
  240.          Height          =   255
  241.          Index           =   0
  242.          Left            =   120
  243.          TabIndex        =   17
  244.          Top             =   720
  245.          Width           =   975
  246.       End
  247.    End
  248.    Begin VB.Label Label6 
  249.       Caption         =   "Right mouse click in picture box to move Sound 2"
  250.       Height          =   375
  251.       Left            =   4800
  252.       TabIndex        =   2
  253.       Top             =   840
  254.       Width           =   2535
  255.    End
  256.    Begin VB.Label Label5 
  257.       Caption         =   "Left mouse click in picture box to move Sound 1"
  258.       Height          =   495
  259.       Left            =   4800
  260.       TabIndex        =   3
  261.       Top             =   360
  262.       Width           =   2535
  263.    End
  264.    Begin VB.Label Label4 
  265.       Caption         =   "Sound Positions"
  266.       BeginProperty Font 
  267.          Name            =   "MS Sans Serif"
  268.          Size            =   8.25
  269.          Charset         =   0
  270.          Weight          =   700
  271.          Underline       =   0   'False
  272.          Italic          =   0   'False
  273.          Strikethrough   =   0   'False
  274.       EndProperty
  275.       Height          =   375
  276.       Left            =   4800
  277.       TabIndex        =   1
  278.       Top             =   120
  279.       Width           =   1575
  280.    End
  281. Attribute VB_Name = "DS3DPositionForm"
  282. Attribute VB_GlobalNameSpace = False
  283. Attribute VB_Creatable = False
  284. Attribute VB_PredeclaredId = True
  285. Attribute VB_Exposed = False
  286. Dim m_dx As New DirectX7
  287. Dim m_ds As DirectSound
  288. Dim m_dsBuffer(2) As DirectSoundBuffer
  289. Dim m_ds3dBuffer(2) As DirectSound3DBuffer
  290. Dim m_dsPrimaryBuffer As DirectSoundBuffer
  291. Dim m_dsListener As DirectSound3DListener
  292. Dim m_pos(2) As D3DVECTOR
  293. Dim m_bMouseDown As Boolean
  294. Dim mediapath As String
  295. Private Sub Form_Load()
  296.     Dim i As Integer
  297.     Me.Show
  298.     FindMediaDir "tutb.wav"
  299.     DoEvents
  300.     mediapath = CurDir$
  301.     '- Step1 initialize DirectX object
  302.     '  We pass in vbnullstring to indicate that we want the
  303.     '  default sound device.
  304.     On Local Error Resume Next
  305.     Set m_ds = m_dx.DirectSoundCreate(vbNullString)
  306.     '- Step2
  307.     '  we can check for error which would indicate
  308.     '  a sound card is not present or directX is not
  309.     '  installed - the On Local Error Resume Next
  310.     '  allows us to check error values immediately
  311.     '  after execution. 0 indicates no error
  312.     If Err.Number <> 0 Then
  313.         MsgBox "Unable to start DirectSound. Check to see that your sound card is properly installed"
  314.         End
  315.     End If
  316.     '- Step3 set the cooperative level by associating
  317.     '  our dsound object with a window.
  318.     '  this tells windows if the sounds created with
  319.     '  the object should be only heard when the window
  320.     '  has focus, COOPERATING  with other sounds from
  321.     '  other applications, or have EXLUSIVE ACCESS to
  322.     '  the sound card which allows us to change the
  323.     '  output wave format and not allow sounds from
  324.     '  other applications to be heard
  325.     '
  326.      
  327.     m_ds.SetCooperativeLevel Me.hWnd, DSSCL_PRIORITY
  328.     '- Step4
  329.     '  We create a primary sound buffer object so
  330.     '  we can get at the listener
  331.     Dim primDesc As DSBUFFERDESC, format As WAVEFORMATEX
  332.     primDesc.lFlags = DSBCAPS_CTRL3D Or DSBCAPS_PRIMARYBUFFER
  333.     Set m_dsPrimaryBuffer = m_ds.CreateSoundBuffer(primDesc, format)
  334.     Set m_dsListener = m_dsPrimaryBuffer.GetDirectSound3DListener()
  335.     m_pos(0).x = 10:  m_pos(0).z = 50
  336.     m_pos(1).x = -10:  m_pos(1).z = 50
  337.     '- Make sure we pickup the correct volume and orientation
  338.     scrlAngle_Change (i)
  339.     scrlVol_Change (i)
  340.     DrawPositions
  341. End Sub
  342. Sub Load(i As Integer, file As String)
  343.     '- Step5 create a sound buffer from a file.
  344.     '  we use the DSBUFFERDESC type to indicate
  345.     '  what features we want the sound to have
  346.     '
  347.     '  This time around we add DSBCAPS_CTRL3D
  348.     '  to obtain 3d sound capabilities - be aware that
  349.     '  their is a performance penalty for doing this
  350.     '
  351.     '  the function fills in the other members of bufferDesc which lets
  352.     '  us know how large the buffer is.  It also fills in the wave Format
  353.     '  type giving information about the waves quality and if it supports
  354.     '  stereo
  355.     '  the function returns an initialized SoundBuffer
  356.     Dim bufferDesc1 As DSBUFFERDESC
  357.     Dim waveFormat1 As WAVEFORMATEX
  358.         
  359.     bufferDesc1.lFlags = (DSBCAPS_CTRL3D Or DSBCAPS_CTRLFREQUENCY Or DSBCAPS_CTRLPAN Or DSBCAPS_CTRLVOLUME) Or DSBCAPS_STATIC
  360.     Set m_dsBuffer(i) = m_ds.CreateSoundBufferFromFile(file, bufferDesc1, waveFormat1)
  361.     '- Step 7 we now get the 3d interfaces to the buffers
  362.     '  we had to create the soundbuffers with the 3d flag
  363.     '  for this method not to fail. The original SoundBuffer
  364.     '  interface is still used to control the starting and
  365.     '  stopping of play but the 3d interface now lets us
  366.     '  position the sounds. Note by default the listener
  367.     '  is at position 0,0
  368.     Set m_ds3dBuffer(i) = m_dsBuffer(i).GetDirectSound3DBuffer
  369.     '- Step 8
  370.     ' Set initial parameters
  371.     'setup our directions
  372.     scrlAngle_Change (i)
  373.     ' Cone angle indicates how sensitive a sound is to direction
  374.     ' Sounds are omni-directional by default
  375.     ' define a narrow cone of sound
  376.     ' these calls  makes the sound sensitive to direction
  377.     m_ds3dBuffer(i).SetConeAngles DS3D_MINCONEANGLE, 100, DS3D_IMMEDIATE
  378.     m_ds3dBuffer(i).SetConeOutsideVolume -400, DS3D_IMMEDIATE
  379.     ' position our sound
  380.     m_ds3dBuffer(i).SetPosition m_pos(i).x / 50, 0, m_pos(i).z / 50, DS3D_IMMEDIATE
  381. End Sub
  382. '- Step 9 Add Play
  383. Private Sub cmdPlay_Click(i As Integer)
  384.     If m_dsBuffer(i) Is Nothing Then Call Load(i, mediapath + "\tut" + Chr(Asc("A") + i) + ".wav")
  385.     'See if the loop check box is checked
  386.     Dim flag As Long
  387.     flag = 0
  388.     If chLoop(i).Value <> 0 Then flag = 1
  389.            
  390.     'Play plays the sound from the current position
  391.     'if the sound was paused using the stop command
  392.     'then play will begin where it last left off
  393.     m_dsBuffer(i).Play flag
  394. End Sub
  395. '- Step 10 Add Stop
  396. Private Sub cmdStop_Click(i As Integer)
  397.     If m_dsBuffer(i) Is Nothing Then Exit Sub
  398.     '- stop does not reset the position of the sound
  399.     m_dsBuffer(i).Stop
  400.     '- here we explicity reset the position to the begining
  401.     '  of the sound
  402.     m_dsBuffer(i).SetCurrentPosition 0
  403. End Sub
  404. Private Sub chLoop_Click(Index As Integer)
  405.     If m_dsBuffer(Index) Is Nothing Then Exit Sub
  406.     If chLoop(Index).Value = 0 Then
  407.         cmdStop_Click (Index)
  408.     End If
  409. End Sub
  410. '- Step 11 Add Pause
  411. Private Sub cmdPause_Click(i As Integer)
  412.     If m_dsBuffer(i) Is Nothing Then Exit Sub
  413.     '- stop does not reset the position of the sound
  414.     '  so play will resume in the middle of the  sound
  415.     m_dsBuffer(i).Stop
  416. End Sub
  417. '- Step 12 Add Handlers for setting the volume
  418. '  changing volume is enabled because we enabled it
  419. '  when we created the buffer other wise this call would fail
  420. '  volume is set in dB and ranges from -10000 to 0
  421. '  (direct sound doesn't amplify sounds just attenuates them)
  422. '  because dB is a log scale -6000 is almost the same as
  423. '  off and changes near zero have more effect on the volume
  424. '  than those at -6000. we use a -5000 to 0
  425. Private Sub scrlVol_Change(i As Integer)
  426.     If m_dsBuffer(i) Is Nothing Then Exit Sub
  427.     m_dsBuffer(i).SetVolume scrlVol(i).Value
  428. End Sub
  429. Private Sub scrlVol_Scroll(Index As Integer)
  430.     scrlVol_Change (Index)
  431. End Sub
  432. '- Step 13 Add Handler for changing direction
  433. Private Sub scrlAngle_Change(i As Integer)
  434.     'fist we must calculate a vector of what direction
  435.     'the sound is traveling in.
  436.     '
  437.     Dim x As Single
  438.     Dim z As Single
  439.     'we take the current angle in degrees convert to radians
  440.     'and get the cos or sin to find the direction from an angle
  441.     x = 5 * Cos(3.141 * scrlAngle(0).Value / 180)
  442.     z = 5 * Sin(3.141 * scrlAngle(0).Value / 180)
  443.     'Update the UI
  444.     DrawPositions
  445.     If m_dsBuffer(i) Is Nothing Then Exit Sub
  446.     'the zero at the end indicates we want the postion updated immediately
  447.     m_ds3dBuffer(i).SetConeOrientation x, 0, z, DS3D_IMMEDIATE
  448. End Sub
  449. Private Sub scrlAngle_Scroll(Index As Integer)
  450.     scrlAngle_Change (Index)
  451. End Sub
  452. '- Step 14 add handler for changing position
  453. '  Mouse methods call this function
  454. '  i is 0 for sound1
  455. '  i is 1 for sound2
  456. '  x and y are the coordinates that are being clicked on
  457. Sub UpdatePosition(i As Integer, x As Single, z As Single)
  458.     m_pos(i).x = x - Picture2.ScaleWidth / 2
  459.     m_pos(i).z = z - Picture2.ScaleHeight / 2
  460.     DrawPositions
  461.     'the zero at the end indicates we want the postion updated immediately
  462.     If m_ds3dBuffer(i) Is Nothing Then Exit Sub
  463.     m_ds3dBuffer(i).SetPosition m_pos(i).x / 50, 0, m_pos(i).z / 50, DS3D_IMMEDIATE
  464. End Sub
  465. '- UI Support Code
  466. ' We use the picture box to give feedback as to the orientation
  467. ' of the sound and position
  468. Private Sub Picture2_MouseDown(Button As Integer, Shift As Integer, x As Single, z As Single)
  469.     Dim i As Integer
  470.     If Button = 2 Then i = 1
  471.     UpdatePosition i, x, z
  472.     m_bMouseDown = True
  473. End Sub
  474. Private Sub Picture2_MouseMove(Button As Integer, Shift As Integer, x As Single, z As Single)
  475.     Dim i As Integer
  476.     If m_bMouseDown = False Then Exit Sub
  477.     If Button = 2 Then i = 1
  478.     UpdatePosition i, x, z
  479. End Sub
  480. Private Sub Picture2_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
  481.     m_bMouseDown = False
  482. End Sub
  483. Private Sub Picture2_Paint()
  484.     DrawPositions
  485. End Sub
  486. Sub DrawPositions()
  487.     Dim x As Integer
  488.     Dim z As Integer
  489.     Picture2.Cls
  490.     'listener is in center and is black
  491.     DrawTriangle 0, Picture2.ScaleWidth / 2, Picture2.ScaleHeight / 2, 90
  492.     'draw sound 1 as RED
  493.     x = CInt(m_pos(0).x) + Picture2.ScaleWidth / 2
  494.     z = CInt(m_pos(0).z) + Picture2.ScaleHeight / 2
  495.     DrawTriangle RGB(256, 0, 0), x, z, scrlAngle(0).Value
  496.     'draw sound2 as BLUE
  497.     x = CInt(m_pos(1).x) + Picture2.ScaleWidth / 2
  498.     z = CInt(m_pos(1).z) + Picture2.ScaleHeight / 2
  499.     DrawTriangle RGB(0, 0, 256), x, z, scrlAngle(1).Value
  500. End Sub
  501. Sub DrawTriangle(col As Long, x As Integer, z As Integer, ByVal a As Single)
  502.     Dim x1 As Integer
  503.     Dim z1 As Integer
  504.     Dim x2 As Integer
  505.     Dim z2 As Integer
  506.     Dim x3 As Integer
  507.     Dim z3 As Integer
  508.     a = 3.141 * (a - 90) / 180
  509.     Dim q As Integer
  510.     q = 10
  511.     x1 = q * Sin(a) + x
  512.     z1 = q * Cos(a) + z
  513.     x2 = q * Sin(a + 3.141 / 1.3) + x
  514.     z2 = q * Cos(a + 3.141 / 1.3) + z
  515.     x3 = q * Sin(a - 3.141 / 1.3) + x
  516.     z3 = q * Cos(a - 3.141 / 1.3) + z
  517.     Picture2.Line (x1, z1)-(x2, z2), col
  518.     Picture2.Line (x1, z1)-(x3, z3), col
  519.     Picture2.Line (x2, z2)-(x3, z3), col
  520. End Sub
  521. Sub FindMediaDir(sFile As String)
  522.     On Local Error Resume Next
  523.     If Mid$(App.Path, 2, 1) = ":" Then
  524.         ChDrive Mid$(App.Path, 1, 1)
  525.     End If
  526.     ChDir App.Path
  527.     If Dir$(sFile) = vbNullString Then
  528.         ChDir "..\media"
  529.     End If
  530.     If Dir$(sFile) = vbNullString Then
  531.         ChDir "..\..\media"
  532.     End If
  533.     DoEvents
  534.     Err.Number = 0
  535. End Sub
  536.