home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8vbsdk.exe / samples / multimedia / vbsamples / directmusic / tutorials / tut2 / audtut2.frm (.txt) next >
Encoding:
Visual Basic Form  |  2000-10-02  |  10.5 KB  |  294 lines

  1. VERSION 5.00
  2. Begin VB.Form AudTut2 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "Audio Tutorial 2"
  5.    ClientHeight    =   3405
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   3390
  9.    Icon            =   "audtut2.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   3405
  14.    ScaleWidth      =   3390
  15.    StartUpPosition =   3  'Windows Default
  16.    Begin VB.CommandButton cmdStop 
  17.       Caption         =   "Stop"
  18.       Height          =   375
  19.       Left            =   1718
  20.       TabIndex        =   6
  21.       Top             =   2940
  22.       Width           =   855
  23.    End
  24.    Begin VB.CommandButton cmdPlay 
  25.       Caption         =   "Play"
  26.       Height          =   375
  27.       Left            =   818
  28.       TabIndex        =   5
  29.       Top             =   2940
  30.       Width           =   855
  31.    End
  32.    Begin VB.Frame fraSound 
  33.       Caption         =   "Sound Type"
  34.       Height          =   1275
  35.       Left            =   60
  36.       TabIndex        =   4
  37.       Top             =   1560
  38.       Width           =   3255
  39.       Begin VB.OptionButton optSeg 
  40.          Caption         =   "DirectMusic Segment"
  41.          Height          =   255
  42.          Left            =   180
  43.          TabIndex        =   9
  44.          Top             =   900
  45.          Width           =   2655
  46.       End
  47.       Begin VB.OptionButton optMid 
  48.          Caption         =   "Midi File"
  49.          Height          =   255
  50.          Left            =   180
  51.          TabIndex        =   8
  52.          Top             =   600
  53.          Width           =   1815
  54.       End
  55.       Begin VB.OptionButton optWave 
  56.          Caption         =   "Wave File"
  57.          Height          =   255
  58.          Left            =   180
  59.          TabIndex        =   7
  60.          Top             =   300
  61.          Value           =   -1  'True
  62.          Width           =   1815
  63.       End
  64.    End
  65.    Begin VB.HScrollBar scrlPan 
  66.       Height          =   255
  67.       LargeChange     =   2
  68.       Left            =   1080
  69.       Max             =   10
  70.       Min             =   -10
  71.       TabIndex        =   1
  72.       Top             =   1200
  73.       Width           =   2235
  74.    End
  75.    Begin VB.HScrollBar scrlVol 
  76.       Height          =   255
  77.       LargeChange     =   20
  78.       Left            =   1080
  79.       Max             =   0
  80.       Min             =   -5000
  81.       SmallChange     =   500
  82.       TabIndex        =   0
  83.       Top             =   840
  84.       Width           =   2235
  85.    End
  86.    Begin VB.Image Image1 
  87.       Height          =   480
  88.       Left            =   120
  89.       Picture         =   "audtut2.frx":0442
  90.       Top             =   180
  91.       Width           =   480
  92.    End
  93.    Begin VB.Label lbl 
  94.       BackStyle       =   0  'Transparent
  95.       Caption         =   "Copyright 
  96.  2000, Microsoft Corporation, All Rights Reserved."
  97.       Height          =   435
  98.       Index           =   2
  99.       Left            =   660
  100.       TabIndex        =   11
  101.       Top             =   300
  102.       Width           =   2655
  103.    End
  104.    Begin VB.Label lbl 
  105.       BackStyle       =   0  'Transparent
  106.       Caption         =   "Audio Tutorial 2"
  107.       Height          =   255
  108.       Index           =   0
  109.       Left            =   660
  110.       TabIndex        =   10
  111.       Top             =   60
  112.       Width           =   2655
  113.    End
  114.    Begin VB.Label Label2 
  115.       Caption         =   "Pan"
  116.       Height          =   255
  117.       Left            =   120
  118.       TabIndex        =   3
  119.       Top             =   1200
  120.       Width           =   975
  121.    End
  122.    Begin VB.Label Label1 
  123.       Caption         =   "Volume"
  124.       Height          =   255
  125.       Left            =   120
  126.       TabIndex        =   2
  127.       Top             =   840
  128.       Width           =   1095
  129.    End
  130. Attribute VB_Name = "AudTut2"
  131. Attribute VB_GlobalNameSpace = False
  132. Attribute VB_Creatable = False
  133. Attribute VB_PredeclaredId = True
  134. Attribute VB_Exposed = False
  135. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  136. '  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
  137. '  File:       audTut2.frm
  138. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  139. '===========================================================
  140. 'DirectX Audio Tutorial 2
  141. 'This tutorial will show you how to load a wave, midi, or
  142. 'segment file in to an audio path, and then play back the
  143. 'file.  It will also show how to get an object from an audio
  144. 'path and manipulate it
  145. '===========================================================
  146. Option Explicit
  147. Private dx As DirectX8
  148. Private dml As DirectMusicLoader8
  149. Private dmp As DirectMusicPerformance8
  150. Private dmSeg As DirectMusicSegment8
  151. Private dmSegState As DirectMusicSegmentState8
  152. Private dmPath As DirectMusicAudioPath8
  153. Private mlOffset As Long
  154. Private MediaPath As String
  155. Private Sub Form_Load()
  156.     Dim dmA As DMUS_AUDIOPARAMS
  157.     MediaPath = FindMediaDir("tuta.wav")
  158.     '===========================================
  159.     '- Step 1 initialize the DirectX objects.
  160.     '===========================================
  161.     On Local Error Resume Next
  162.     Set dx = New DirectX8
  163.     Set dml = dx.DirectMusicLoaderCreate
  164.     Set dmp = dx.DirectMusicPerformanceCreate
  165.     dml.SetSearchDirectory MediaPath
  166.     '========================================================
  167.     '- Step 2
  168.     '  Now we can init our audio environment, and check for any errors
  169.     '  if a sound card is not present or DirectX is not
  170.     '  installed. The 'On Local Error Resume Next'
  171.     '  statement allows us to check error values immediately
  172.     '  after execution. The error number 0 indicates no error.
  173.     '========================================================
  174.     dmp.InitAudio Me.hWnd, DMUS_AUDIOF_ALL, dmA
  175.     If Err.Number <> 0 Then
  176.         MsgBox "Could not initialize DirectMusic." & vbCrLf & "This sample will exit.", vbOKOnly Or vbInformation, "Exiting..."
  177.         Unload Me
  178.         Exit Sub
  179.     End If
  180.     '===========================================================
  181.     '- Step 3 Now we can set up our Audio path which will play
  182.     ' the sounds.
  183.     '============================================================
  184.     Set dmPath = dmp.CreateStandardAudioPath(DMUS_APATH_DYNAMIC_3D, 64, True)
  185. End Sub
  186. Sub LoadSound(sFile As String)
  187.     '========================================================================
  188.     '- Step 4 We can now load our audio file.
  189.     ' But first we'll make sure the file exists
  190.     '=========================================================================
  191.     On Error Resume Next 'We'll check our error later in the app
  192.     If Dir$(sFile) = vbNullString Then
  193.         MsgBox "Unable to find " & sFile, vbOKOnly Or vbCritical, "Not found."
  194.         Unload Me
  195.     End If
  196.     'Cleanup if there is already a segment loaded
  197.     If Not (dmSeg Is Nothing) Then
  198.         dmSeg.Unload dmPath
  199.         Set dmSeg = Nothing
  200.     End If
  201.     Set dmSeg = dml.LoadSegment(sFile)
  202.     If Right$(sFile, 4) = ".mid" Then dmSeg.SetStandardMidiFile
  203.     '========================================
  204.     '- Step 5 Download the segment, and make sure we have no errors
  205.     '========================================
  206.     dmSeg.Download dmPath
  207.     If Err.Number <> 0 Then
  208.         MsgBox "Unable to download segment.", vbOKOnly Or vbCritical, "No download."
  209.         Unload Me
  210.     End If
  211.     scrlPan_Change
  212.     scrlVol_Change
  213. End Sub
  214. '===============================
  215. ' Step 6 - PLAYING THE SOUNDS
  216. '===============================
  217. Private Sub cmdPlay_Click()
  218.     '=========================================================
  219.     ' Make sure we've loaded our sound
  220.     '=========================================================
  221.     If dmSeg Is Nothing Then
  222.         If optWave.Value Then LoadSound MediaPath & "\tuta.wav"
  223.         If optMid.Value Then LoadSound MediaPath & "\tut.mid"
  224.         If optSeg.Value Then LoadSound MediaPath & "\sample.sgt"
  225.     End If
  226.             
  227.     '================================================
  228.     'Plays the sound
  229.     '================================================
  230.     Set dmSegState = dmp.PlaySegmentEx(dmSeg, 0, 0, Nothing, dmPath)
  231. End Sub
  232. '==================
  233. '- Step 7 Add Stop
  234. '==================
  235. Private Sub cmdStop_Click()
  236.     If dmSeg Is Nothing Then Exit Sub
  237.     dmp.StopEx dmSeg, 0, 0
  238.     mlOffset = 0
  239. End Sub
  240. '======================================================================
  241. '- Step 8 Add Handler for setting the volume
  242. '  volume is set in db and ranges from -10000 to 0
  243. '  (direct sound doesn't amplify sounds just decreases their volume)
  244. '  because db is a log scale -6000 is almost the same as
  245. '  off and changes near zero have more effect on the volume
  246. '  than those at -6000. we use a -5000 to 0
  247. '======================================================================
  248. Private Sub scrlVol_Change()
  249.     'We can just set our volume
  250.     dmPath.SetVolume scrlVol.Value, 0
  251. End Sub
  252. Private Sub scrlVol_Scroll()
  253.     scrlVol_Change
  254. End Sub
  255. '===============================================================
  256. '- Step 9 Add Handler for Pan
  257. '===============================================================
  258. Private Sub scrlPan_Change()
  259.     If dmSeg Is Nothing Then Exit Sub
  260.     'Now we need to get the corresponding Sound buffer, and make the call
  261.     Dim dsBuf As DirectSound3DBuffer8
  262.     Set dsBuf = dmPath.GetObjectinPath(DMUS_PCHANNEL_ALL, DMUS_PATH_BUFFER, 0, vbNullString, 0, "IID_IDirectSound3DBuffer")
  263.     dsBuf.SetPosition scrlPan.Value / 5, 0, 0, DS3D_IMMEDIATE
  264.     Set dsBuf = Nothing
  265. End Sub
  266. Private Sub scrlPan_Scroll()
  267.     scrlPan_Change
  268. End Sub
  269. Private Sub Form_Unload(Cancel As Integer)
  270.     'Here we will cleanup any of our code
  271.     'First we should stop any currently playing sounds
  272.     If Not (dmSeg Is Nothing) Then dmp.StopEx dmSeg, 0, 0
  273.     Set dml = Nothing
  274.     Set dmSeg = Nothing
  275.     dmp.CloseDown
  276.     Set dmPath = Nothing
  277.     Set dmp = Nothing
  278.     Set dx = Nothing
  279. End Sub
  280. 'If we click on any of the option buttons
  281. 'we should load the new a new segment
  282. Private Sub optMid_Click()
  283.     cmdStop_Click
  284.     LoadSound MediaPath & "\tut.mid"
  285. End Sub
  286. Private Sub optSeg_Click()
  287.     cmdStop_Click
  288.     LoadSound MediaPath & "\sample.sgt"
  289. End Sub
  290. Private Sub optWave_Click()
  291.     cmdStop_Click
  292.     LoadSound MediaPath & "\tuta.wav"
  293. End Sub
  294.