home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectMusic / AudioEffects / frmFX.frm (.txt) next >
Encoding:
Visual Basic Form  |  2001-10-08  |  15.7 KB  |  413 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form frmEffects 
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Audio Effects using DirectMusic AudioPaths"
  6.    ClientHeight    =   4845
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   4800
  10.    Icon            =   "frmFX.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    ScaleHeight     =   4845
  14.    ScaleWidth      =   4800
  15.    StartUpPosition =   3  'Windows Default
  16.    Begin VB.CheckBox chkLoop 
  17.       Caption         =   "Loop Sound"
  18.       Height          =   315
  19.       Left            =   780
  20.       TabIndex        =   15
  21.       Top             =   4380
  22.       Width           =   1455
  23.    End
  24.    Begin VB.CommandButton cmdStop 
  25.       Caption         =   "&Stop"
  26.       Height          =   375
  27.       Left            =   3540
  28.       TabIndex        =   14
  29.       Top             =   4380
  30.       Width           =   1095
  31.    End
  32.    Begin VB.CommandButton cmdPlay 
  33.       Caption         =   "&Play"
  34.       Height          =   375
  35.       Left            =   2340
  36.       TabIndex        =   13
  37.       Top             =   4380
  38.       Width           =   1095
  39.    End
  40.    Begin VB.Frame fraEffects 
  41.       Caption         =   "Effects Information"
  42.       Height          =   3495
  43.       Left            =   120
  44.       TabIndex        =   2
  45.       Top             =   780
  46.       Width           =   4515
  47.       Begin VB.TextBox txtFile 
  48.          Height          =   285
  49.          Left            =   120
  50.          Locked          =   -1  'True
  51.          TabIndex        =   9
  52.          Text            =   "No file loaded..."
  53.          Top             =   480
  54.          Width           =   3855
  55.       End
  56.       Begin VB.CommandButton cmdBrowse 
  57.          Caption         =   "..."
  58.          Height          =   285
  59.          Left            =   3960
  60.          TabIndex        =   8
  61.          Top             =   480
  62.          Width           =   315
  63.       End
  64.       Begin VB.ListBox lstAvail 
  65.          Height          =   1815
  66.          ItemData        =   "frmFX.frx":0442
  67.          Left            =   120
  68.          List            =   "frmFX.frx":045E
  69.          TabIndex        =   7
  70.          Top             =   1080
  71.          Width           =   1875
  72.       End
  73.       Begin VB.ListBox lstUse 
  74.          Height          =   1815
  75.          Left            =   2400
  76.          TabIndex        =   6
  77.          Top             =   1080
  78.          Width           =   1875
  79.       End
  80.       Begin VB.CommandButton cmdAdd 
  81.          Caption         =   ">"
  82.          Height          =   285
  83.          Left            =   2040
  84.          TabIndex        =   5
  85.          Top             =   1500
  86.          Width           =   315
  87.       End
  88.       Begin VB.CommandButton cmdRemove 
  89.          Caption         =   "<"
  90.          Height          =   285
  91.          Left            =   2040
  92.          TabIndex        =   4
  93.          Top             =   2220
  94.          Width           =   315
  95.       End
  96.       Begin VB.CommandButton cmdApply 
  97.          Caption         =   "Apply Effects"
  98.          Height          =   315
  99.          Left            =   3120
  100.          TabIndex        =   3
  101.          Top             =   3000
  102.          Width           =   1215
  103.       End
  104.       Begin VB.Label lbl 
  105.          BackStyle       =   0  'Transparent
  106.          Caption         =   "Currently loaded sound file:"
  107.          Height          =   195
  108.          Index           =   0
  109.          Left            =   120
  110.          TabIndex        =   12
  111.          Top             =   240
  112.          Width           =   4515
  113.       End
  114.       Begin VB.Label lbl 
  115.          BackStyle       =   0  'Transparent
  116.          Caption         =   "Available Effects"
  117.          Height          =   195
  118.          Index           =   1
  119.          Left            =   120
  120.          TabIndex        =   11
  121.          Top             =   840
  122.          Width           =   1215
  123.       End
  124.       Begin VB.Label lbl 
  125.          BackStyle       =   0  'Transparent
  126.          Caption         =   "Effects in use"
  127.          Height          =   195
  128.          Index           =   2
  129.          Left            =   2700
  130.          TabIndex        =   10
  131.          Top             =   840
  132.          Width           =   1215
  133.       End
  134.    End
  135.    Begin MSComDlg.CommonDialog cdlOpen 
  136.       Left            =   300
  137.       Top             =   3720
  138.       _ExtentX        =   847
  139.       _ExtentY        =   847
  140.       _Version        =   393216
  141.    End
  142.    Begin VB.Label lbl 
  143.       BackStyle       =   0  'Transparent
  144.       Caption         =   "Audio Effects using DirectMusic Audiopaths"
  145.       Height          =   255
  146.       Index           =   4
  147.       Left            =   660
  148.       TabIndex        =   1
  149.       Top             =   60
  150.       Width           =   3195
  151.    End
  152.    Begin VB.Label lbl 
  153.       BackStyle       =   0  'Transparent
  154.       Caption         =   "Copyright (C) 1999-2001 Microsoft Corporation, All Rights Reserved."
  155.       Height          =   435
  156.       Index           =   3
  157.       Left            =   660
  158.       TabIndex        =   0
  159.       Top             =   300
  160.       Width           =   3555
  161.    End
  162.    Begin VB.Image Image1 
  163.       Height          =   480
  164.       Left            =   120
  165.       Picture         =   "frmFX.frx":04AF
  166.       Top             =   180
  167.       Width           =   480
  168.    End
  169. Attribute VB_Name = "frmEffects"
  170. Attribute VB_GlobalNameSpace = False
  171. Attribute VB_Creatable = False
  172. Attribute VB_PredeclaredId = True
  173. Attribute VB_Exposed = False
  174. Option Explicit
  175. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  176. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  177. '  File:       frmFX.frm
  178. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  179. 'API declare for windows folder
  180. Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  181. Private Const mlMaxEffects As Long = 20
  182. 'Private declares for our DirectX objects
  183. Private dx As DirectX8
  184. Private dmp As DirectMusicPerformance8
  185. Private dml As DirectMusicLoader8
  186. Private dmSeg As DirectMusicSegment8
  187. Private dmSegState As DirectMusicSegmentState8
  188. Private Sub cmdAdd_Click()
  189.     If lstAvail.ListIndex = -1 Then 'Nothing is selected
  190.         MsgBox "Please select an available effect before attempting to add it.", vbOKOnly Or vbInformation, "Nothing selected."
  191.         Exit Sub
  192.     End If
  193.     If lstUse.ListCount >= mlMaxEffects Then
  194.         MsgBox "You cannot add more than " & CStr(mlMaxEffects) & " effects in this sample.", vbOKOnly Or vbInformation, "No more effects."
  195.         Exit Sub
  196.     End If
  197.     'Add this item to our list of effects
  198.     lstUse.AddItem lstAvail.List(lstAvail.ListIndex)
  199. End Sub
  200. Private Sub cmdApply_Click()
  201.     On Local Error GoTo NoFX
  202.     Dim DSEffects() As DSEFFECTDESC
  203.     Dim lResults() As Long
  204.     Dim lCount As Long
  205.     Dim dsb As DirectSoundSecondaryBuffer8
  206.     'Do we have a sound buffer
  207.     If dmSeg Is Nothing Then
  208.         MsgBox "You must first load an audio file before you can apply effects to it.", vbOKOnly Or vbInformation, "No buffer"
  209.         Exit Sub
  210.     End If
  211.     'Yup, stop a sound already playing
  212.     If dmp.IsPlaying(dmSeg, dmSegState) = True Then
  213.         MsgBox "Stop the currently playing sound before adding effects.", vbOKOnly Or vbInformation, "Sound is playing"
  214.         Exit Sub
  215.     End If
  216.     'Yes we do, do we have effects selected?
  217.     If lstUse.ListCount = 0 Then
  218.         If MsgBox("Do you want to turn off effects for this buffer?", vbYesNo Or vbQuestion, "No effects") = vbYes Then
  219.             'We need to get a DirectSoundSecondaryBuffer from the audio path
  220.             Set dsb = dmp.GetDefaultAudioPath.GetObjectinPath(DMUS_PCHANNEL_ALL, DMUS_PATH_BUFFER, 0, GUID_ALL, 0, IID_DirectSoundSecondaryBuffer)
  221.             'Before we can call SetFX on our Audio Path, we need to deactivate it first
  222.             dmp.GetDefaultAudioPath.Activate False
  223.             'Go ahead and apply our effects
  224.             dsb.SetFX 0, DSEffects, lResults
  225.             'Now we can reactivate our audio path
  226.             dmp.GetDefaultAudioPath.Activate True
  227.             Exit Sub
  228.         Else
  229.             MsgBox "You must first select some effects to use.", vbOKOnly Or vbInformation, "No effects"
  230.             Exit Sub
  231.         End If
  232.     End If
  233.     'Ok, let's apply our effects info here
  234.     'First get an array of effects structs the right size
  235.     ReDim DSEffects(lstUse.ListCount - 1)
  236.     ReDim lResults(lstUse.ListCount - 1)
  237.     For lCount = 0 To lstUse.ListCount - 1
  238.         Select Case LCase(lstUse.List(lCount))
  239.         Case "distortion"
  240.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_DISTORTION
  241.         Case "echo"
  242.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_ECHO
  243.         Case "chorus"
  244.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_CHORUS
  245.         Case "flanger"
  246.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_FLANGER
  247.         Case "compressor"
  248.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_COMPRESSOR
  249.         Case "gargle"
  250.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_GARGLE
  251.         Case "parameq"
  252.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_PARAMEQ
  253.         Case "wavesreverb"
  254.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_WAVES_REVERB
  255.         End Select
  256.     Next
  257.     'We need to get a DirectSoundSecondaryBuffer from the audio path
  258.     Set dsb = dmp.GetDefaultAudioPath.GetObjectinPath(DMUS_PCHANNEL_ALL, DMUS_PATH_BUFFER, 0, GUID_ALL, 0, IID_DirectSoundSecondaryBuffer)
  259.     'Before we can call SetFX on our Audio Path, we need to deactivate it first
  260.     dmp.GetDefaultAudioPath.Activate False
  261.     'Go ahead and apply our effects
  262.     dsb.SetFX lstUse.ListCount, DSEffects, lResults
  263.     'Now we can reactivate our audio path
  264.     dmp.GetDefaultAudioPath.Activate True
  265.     Exit Sub
  266. NoFX:
  267.     MsgBox "This set of effects could not be set on this audio file.", vbOKOnly Or vbInformation, "Cannot set"
  268. End Sub
  269. Private Sub cmdBrowse_Click()
  270.     Static sCurDir As String
  271.     'We want to open a file now
  272.     cdlOpen.flags = cdlOFNHideReadOnly Or cdlOFNFileMustExist
  273.     cdlOpen.Filter = "Wave Files (*.wav)|*.wav|Music Files (*.mid;*.rmi)|*.mid;*.rmi|Segment Files (*.sgt)|*.sgt|All Audio Files|*.wav;*.mid;*.rmi;*.sgt|All Files (*.*)|*.*"
  274.     cdlOpen.FileName = vbNullString
  275.     If sCurDir = vbNullString Then
  276.         'Set the init folder to \windows\media if it exists.  If not, set it to the \windows folder
  277.         Dim sWindir As String
  278.         sWindir = Space$(255)
  279.         If GetWindowsDirectory(sWindir, 255) = 0 Then
  280.             'We couldn't get the windows folder for some reason, use the c:\
  281.             cdlOpen.InitDir = "C:\"
  282.         Else
  283.             Dim sMedia As String
  284.             sWindir = Left$(sWindir, InStr(sWindir, Chr$(0)) - 1)
  285.             If Right$(sWindir, 1) = "\" Then
  286.                 sMedia = sWindir & "Media"
  287.             Else
  288.                 sMedia = sWindir & "\Media"
  289.             End If
  290.             'We are trying to find the windows\media directory.  If it
  291.             'doesn't exist, then use the windows folder as a default
  292.             If Dir$(sMedia, vbDirectory) <> vbNullString Then
  293.                 cdlOpen.InitDir = sMedia
  294.             Else
  295.                 cdlOpen.InitDir = sWindir
  296.             End If
  297.         End If
  298.     Else
  299.         'No need to move folders.  Stay where they picked the last file
  300.         cdlOpen.InitDir = sCurDir
  301.     End If
  302.     On Local Error GoTo ClickedCancel
  303.     cdlOpen.CancelError = True
  304.     cdlOpen.ShowOpen   ' Display the Open dialog box
  305.     'Save the current information
  306.     sCurDir = GetFolder(cdlOpen.FileName)
  307.             
  308.     On Local Error GoTo NoLoadSegment
  309.     'Before we load the buffer stop one if it's playing
  310.     If Not (dmSeg Is Nothing) Then
  311.         dmp.StopEx dmSeg, 0, 0
  312.         dmSeg.Unload dmp.GetDefaultAudioPath
  313.         Set dmSeg = Nothing
  314.     End If
  315.     'Now let's load the segment
  316.     dml.SetSearchDirectory sCurDir
  317.     Set dmSeg = dml.LoadSegment(cdlOpen.FileName)
  318.     If (Right$(cdlOpen.FileName, 4) = ".mid") Or (Right$(cdlOpen.FileName, 4) = ".rmi") Or (Right$(cdlOpen.FileName, 5) = ".midi") Then
  319.         dmSeg.SetStandardMidiFile
  320.     End If
  321.     dmSeg.Download dmp.GetDefaultAudioPath
  322.     txtFile.Text = cdlOpen.FileName
  323.     Exit Sub
  324. NoLoadSegment:
  325.     If Err.Number = DSERR_BUFFERTOOSMALL Then 'This buffer isn't big enough to control effects on
  326.         MsgBox "This file isn't long enough to control effects.  Please choose a longer audio file.", vbOKOnly Or vbCritical, "Couldn't load"
  327.     Else 'Some other error
  328.         MsgBox "Couldn't load this file", vbOKOnly Or vbCritical, "Couldn't load"
  329.     End If
  330.     txtFile.Text = "No file loaded..."
  331. ClickedCancel:
  332. End Sub
  333. Private Sub cmdPlay_Click()
  334.     If dmSeg Is Nothing Then
  335.         MsgBox "You must first load a audio file before you can play it.", vbOKOnly Or vbInformation, "No buffer"
  336.         Exit Sub
  337.     End If
  338.     If chkLoop.Value = vbChecked Then
  339.         dmSeg.SetRepeats -1
  340.     Else
  341.         dmSeg.SetRepeats 0
  342.     End If
  343.     Set dmSegState = dmp.PlaySegmentEx(dmSeg, DMUS_SEGF_DEFAULT, 0, , dmp.GetDefaultAudioPath)
  344. End Sub
  345. Private Sub cmdRemove_Click()
  346.     If lstUse.ListIndex = -1 Then 'Nothing is selected
  347.         MsgBox "Please select an effect that's being used before attempting to remove it.", vbOKOnly Or vbInformation, "Nothing selected."
  348.         Exit Sub
  349.     End If
  350.     'Add this item to our list of effects
  351.     lstUse.RemoveItem lstUse.ListIndex
  352. End Sub
  353. Private Sub cmdStop_Click()
  354.     If dmSeg Is Nothing Then
  355.         MsgBox "You must first load an audio file before you can stop it.", vbOKOnly Or vbInformation, "No buffer"
  356.         Exit Sub
  357.     End If
  358.     dmp.StopEx dmSeg, 0, 0
  359. End Sub
  360. Private Sub Form_Load()
  361.     InitAudio
  362. End Sub
  363. Private Sub Form_Unload(Cancel As Integer)
  364.     Cleanup
  365. End Sub
  366. Private Sub InitAudio()
  367.     On Local Error Resume Next
  368.     Dim dma As DMUS_AUDIOPARAMS
  369.     Set dx = New DirectX8
  370.     'Create our default Performance and Loader objects
  371.     Set dmp = dx.DirectMusicPerformanceCreate
  372.     Set dml = dx.DirectMusicLoaderCreate
  373.     'We want to be able to get a buffer, and control effects.
  374.     dmp.InitAudio Me.hWnd, DMUS_AUDIOF_EAX Or DMUS_AUDIOF_BUFFERS, dma, , DMUS_APATH_DYNAMIC_3D, 128
  375.     'Make sure we did init the audio
  376.     If Err <> 0 Then 'Nope we didn't
  377.         MsgBox "Could not initialize DirectMusic." & vbCrLf & "This sample will exit.", vbOKOnly Or vbInformation, "Exiting..."
  378.         Unload Me
  379.     End If
  380. End Sub
  381. Private Sub Cleanup()
  382.     'Let's clean up now
  383.     If Not dmSeg Is Nothing Then
  384.         'If we are playing our file, stop it
  385.         dmp.StopEx dmSeg, 0, 0
  386.         dmSeg.Unload dmp.GetDefaultAudioPath
  387.     End If
  388.     'Destroy our objects
  389.     Set dmSeg = Nothing
  390.     If Not (dmp Is Nothing) Then dmp.CloseDown
  391.     Set dmp = Nothing
  392.     Set dml = Nothing
  393.     Set dx = Nothing
  394. End Sub
  395. Private Function GetFolder(ByVal sFile As String) As String
  396.     Dim lCount As Long
  397.     For lCount = Len(sFile) To 1 Step -1
  398.         If Mid$(sFile, lCount, 1) = "\" Then
  399.             GetFolder = Left$(sFile, lCount)
  400.             Exit Function
  401.         End If
  402.     Next
  403.     GetFolder = vbNullString
  404. End Function
  405. Private Sub lstAvail_DblClick()
  406.     'Double clicking should be the same as clicking the 'Add' button
  407.     cmdAdd_Click
  408. End Sub
  409. Private Sub lstUse_DblClick()
  410.     'Double clicking should be the same as clicking the 'Remove' button
  411.     cmdRemove_Click
  412. End Sub
  413.