home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8a_sdk.exe / samples / multimedia / vbsamples / directmusic / audioeffects / frmfx.frm (.txt) next >
Encoding:
Visual Basic Form  |  2000-10-02  |  15.6 KB  |  414 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 
  155.  2000, Microsoft Corporation, All Rights Reserved."
  156.       Height          =   435
  157.       Index           =   3
  158.       Left            =   660
  159.       TabIndex        =   0
  160.       Top             =   300
  161.       Width           =   3555
  162.    End
  163.    Begin VB.Image Image1 
  164.       Height          =   480
  165.       Left            =   120
  166.       Picture         =   "frmFX.frx":04AF
  167.       Top             =   180
  168.       Width           =   480
  169.    End
  170. Attribute VB_Name = "frmEffects"
  171. Attribute VB_GlobalNameSpace = False
  172. Attribute VB_Creatable = False
  173. Attribute VB_PredeclaredId = True
  174. Attribute VB_Exposed = False
  175. Option Explicit
  176. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  177. '  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
  178. '  File:       frmFX.frm
  179. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  180. 'API declare for windows folder
  181. Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  182. Private Const mlMaxEffects As Long = 20
  183. 'Private declares for our DirectX objects
  184. Private dx As DirectX8
  185. Private dmp As DirectMusicPerformance8
  186. Private dml As DirectMusicLoader8
  187. Private dmSeg As DirectMusicSegment8
  188. Private dmSegState As DirectMusicSegmentState8
  189. Private Sub cmdAdd_Click()
  190.     If lstAvail.ListIndex = -1 Then 'Nothing is selected
  191.         MsgBox "Please select an available effect before attempting to add it.", vbOKOnly Or vbInformation, "Nothing selected."
  192.         Exit Sub
  193.     End If
  194.     If lstUse.ListCount >= mlMaxEffects Then
  195.         MsgBox "You cannot add more than " & CStr(mlMaxEffects) & " effects in this sample.", vbOKOnly Or vbInformation, "No more effects."
  196.         Exit Sub
  197.     End If
  198.     'Add this item to our list of effects
  199.     lstUse.AddItem lstAvail.List(lstAvail.ListIndex)
  200. End Sub
  201. Private Sub cmdApply_Click()
  202.     On Local Error GoTo NoFX
  203.     Dim DSEffects() As DSEFFECTDESC
  204.     Dim lResults() As Long
  205.     Dim lCount As Long
  206.     Dim dsb As DirectSoundSecondaryBuffer8
  207.     'Do we have a sound buffer
  208.     If dmSeg Is Nothing Then
  209.         MsgBox "You must first load an audio file before you can apply effects to it.", vbOKOnly Or vbInformation, "No buffer"
  210.         Exit Sub
  211.     End If
  212.     'Yup, stop a sound already playing
  213.     If dmp.IsPlaying(dmSeg, dmSegState) = True Then
  214.         MsgBox "Stop the currently playing sound before adding effects.", vbOKOnly Or vbInformation, "Sound is playing"
  215.         Exit Sub
  216.     End If
  217.     'Yes we do, do we have effects selected?
  218.     If lstUse.ListCount = 0 Then
  219.         If MsgBox("Do you want to turn off effects for this buffer?", vbYesNo Or vbQuestion, "No effects") = vbYes Then
  220.             'We need to get a DirectSoundSecondaryBuffer from the audio path
  221.             Set dsb = dmp.GetDefaultAudioPath.GetObjectinPath(DMUS_PCHANNEL_ALL, DMUS_PATH_BUFFER, 0, GUID_ALL, 0, IID_DirectSoundSecondaryBuffer)
  222.             'Before we can call SetFX on our Audio Path, we need to deactivate it first
  223.             dmp.GetDefaultAudioPath.Activate False
  224.             'Go ahead and apply our effects
  225.             dsb.SetFX 0, DSEffects, lResults
  226.             'Now we can reactivate our audio path
  227.             dmp.GetDefaultAudioPath.Activate True
  228.             Exit Sub
  229.         Else
  230.             MsgBox "You must first select some effects to use.", vbOKOnly Or vbInformation, "No effects"
  231.             Exit Sub
  232.         End If
  233.     End If
  234.     'Ok, let's apply our effects info here
  235.     'First get an array of effects structs the right size
  236.     ReDim DSEffects(lstUse.ListCount - 1)
  237.     ReDim lResults(lstUse.ListCount - 1)
  238.     For lCount = 0 To lstUse.ListCount - 1
  239.         Select Case LCase(lstUse.List(lCount))
  240.         Case "distortion"
  241.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_DISTORTION
  242.         Case "echo"
  243.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_ECHO
  244.         Case "chorus"
  245.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_CHORUS
  246.         Case "flanger"
  247.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_FLANGER
  248.         Case "compressor"
  249.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_COMPRESSOR
  250.         Case "gargle"
  251.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_GARGLE
  252.         Case "parameq"
  253.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_PARAMEQ
  254.         Case "wavesreverb"
  255.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_WAVES_REVERB
  256.         End Select
  257.     Next
  258.     'We need to get a DirectSoundSecondaryBuffer from the audio path
  259.     Set dsb = dmp.GetDefaultAudioPath.GetObjectinPath(DMUS_PCHANNEL_ALL, DMUS_PATH_BUFFER, 0, GUID_ALL, 0, IID_DirectSoundSecondaryBuffer)
  260.     'Before we can call SetFX on our Audio Path, we need to deactivate it first
  261.     dmp.GetDefaultAudioPath.Activate False
  262.     'Go ahead and apply our effects
  263.     dsb.SetFX lstUse.ListCount, DSEffects, lResults
  264.     'Now we can reactivate our audio path
  265.     dmp.GetDefaultAudioPath.Activate True
  266.     Exit Sub
  267. NoFX:
  268.     MsgBox "This set of effects could not be set on this audio file.", vbOKOnly Or vbInformation, "Cannot set"
  269. End Sub
  270. Private Sub cmdBrowse_Click()
  271.     Static sCurDir As String
  272.     'We want to open a file now
  273.     cdlOpen.flags = cdlOFNHideReadOnly Or cdlOFNFileMustExist
  274.     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 (*.*)|*.*"
  275.     cdlOpen.FileName = vbNullString
  276.     If sCurDir = vbNullString Then
  277.         'Set the init folder to \windows\media if it exists.  If not, set it to the \windows folder
  278.         Dim sWindir As String
  279.         sWindir = Space$(255)
  280.         If GetWindowsDirectory(sWindir, 255) = 0 Then
  281.             'We couldn't get the windows folder for some reason, use the c:\
  282.             cdlOpen.InitDir = "C:\"
  283.         Else
  284.             Dim sMedia As String
  285.             sWindir = Left$(sWindir, InStr(sWindir, Chr$(0)) - 1)
  286.             If Right$(sWindir, 1) = "\" Then
  287.                 sMedia = sWindir & "Media"
  288.             Else
  289.                 sMedia = sWindir & "\Media"
  290.             End If
  291.             'We are trying to find the windows\media directory.  If it
  292.             'doesn't exist, then use the windows folder as a default
  293.             If Dir$(sMedia, vbDirectory) <> vbNullString Then
  294.                 cdlOpen.InitDir = sMedia
  295.             Else
  296.                 cdlOpen.InitDir = sWindir
  297.             End If
  298.         End If
  299.     Else
  300.         'No need to move folders.  Stay where they picked the last file
  301.         cdlOpen.InitDir = sCurDir
  302.     End If
  303.     On Local Error GoTo ClickedCancel
  304.     cdlOpen.CancelError = True
  305.     cdlOpen.ShowOpen   ' Display the Open dialog box
  306.     'Save the current information
  307.     sCurDir = GetFolder(cdlOpen.FileName)
  308.             
  309.     On Local Error GoTo NoLoadSegment
  310.     'Before we load the buffer stop one if it's playing
  311.     If Not (dmSeg Is Nothing) Then
  312.         dmp.StopEx dmSeg, 0, 0
  313.         dmSeg.Unload dmp.GetDefaultAudioPath
  314.         Set dmSeg = Nothing
  315.     End If
  316.     'Now let's load the segment
  317.     dml.SetSearchDirectory sCurDir
  318.     Set dmSeg = dml.LoadSegment(cdlOpen.FileName)
  319.     If (Right$(cdlOpen.FileName, 4) = ".mid") Or (Right$(cdlOpen.FileName, 4) = ".rmi") Or (Right$(cdlOpen.FileName, 5) = ".midi") Then
  320.         dmSeg.SetStandardMidiFile
  321.     End If
  322.     dmSeg.Download dmp.GetDefaultAudioPath
  323.     txtFile.Text = cdlOpen.FileName
  324.     Exit Sub
  325. NoLoadSegment:
  326.     If Err.Number = DSERR_BUFFERTOOSMALL Then 'This buffer isn't big enough to control effects on
  327.         MsgBox "This file isn't long enough to control effects.  Please choose a longer audio file.", vbOKOnly Or vbCritical, "Couldn't load"
  328.     Else 'Some other error
  329.         MsgBox "Couldn't load this file", vbOKOnly Or vbCritical, "Couldn't load"
  330.     End If
  331.     txtFile.Text = "No file loaded..."
  332. ClickedCancel:
  333. End Sub
  334. Private Sub cmdPlay_Click()
  335.     If dmSeg Is Nothing Then
  336.         MsgBox "You must first load a audio file before you can play it.", vbOKOnly Or vbInformation, "No buffer"
  337.         Exit Sub
  338.     End If
  339.     If chkLoop.Value = vbChecked Then
  340.         dmSeg.SetRepeats -1
  341.     Else
  342.         dmSeg.SetRepeats 0
  343.     End If
  344.     Set dmSegState = dmp.PlaySegmentEx(dmSeg, DMUS_SEGF_DEFAULT, 0, , dmp.GetDefaultAudioPath)
  345. End Sub
  346. Private Sub cmdRemove_Click()
  347.     If lstUse.ListIndex = -1 Then 'Nothing is selected
  348.         MsgBox "Please select an effect that's being used before attempting to remove it.", vbOKOnly Or vbInformation, "Nothing selected."
  349.         Exit Sub
  350.     End If
  351.     'Add this item to our list of effects
  352.     lstUse.RemoveItem lstUse.ListIndex
  353. End Sub
  354. Private Sub cmdStop_Click()
  355.     If dmSeg Is Nothing Then
  356.         MsgBox "You must first load an audio file before you can stop it.", vbOKOnly Or vbInformation, "No buffer"
  357.         Exit Sub
  358.     End If
  359.     dmp.StopEx dmSeg, 0, 0
  360. End Sub
  361. Private Sub Form_Load()
  362.     InitAudio
  363. End Sub
  364. Private Sub Form_Unload(Cancel As Integer)
  365.     Cleanup
  366. End Sub
  367. Private Sub InitAudio()
  368.     On Local Error Resume Next
  369.     Dim dma As DMUS_AUDIOPARAMS
  370.     Set dx = New DirectX8
  371.     'Create our default Performance and Loader objects
  372.     Set dmp = dx.DirectMusicPerformanceCreate
  373.     Set dml = dx.DirectMusicLoaderCreate
  374.     'We want to be able to get a buffer, and control effects.
  375.     dmp.InitAudio Me.hWnd, DMUS_AUDIOF_EAX Or DMUS_AUDIOF_BUFFERS, dma, , DMUS_APATH_DYNAMIC_3D, 128
  376.     'Make sure we did init the audio
  377.     If Err <> 0 Then 'Nope we didn't
  378.         MsgBox "Could not initialize DirectMusic." & vbCrLf & "This sample will exit.", vbOKOnly Or vbInformation, "Exiting..."
  379.         Unload Me
  380.     End If
  381. End Sub
  382. Private Sub Cleanup()
  383.     'Let's clean up now
  384.     If Not dmSeg Is Nothing Then
  385.         'If we are playing our file, stop it
  386.         dmp.StopEx dmSeg, 0, 0
  387.         dmSeg.Unload dmp.GetDefaultAudioPath
  388.     End If
  389.     'Destroy our objects
  390.     Set dmSeg = Nothing
  391.     If Not (dmp Is Nothing) Then dmp.CloseDown
  392.     Set dmp = Nothing
  393.     Set dml = Nothing
  394.     Set dx = Nothing
  395. End Sub
  396. Private Function GetFolder(ByVal sFile As String) As String
  397.     Dim lCount As Long
  398.     For lCount = Len(sFile) To 1 Step -1
  399.         If Mid$(sFile, lCount, 1) = "\" Then
  400.             GetFolder = Left$(sFile, lCount)
  401.             Exit Function
  402.         End If
  403.     Next
  404.     GetFolder = vbNullString
  405. End Function
  406. Private Sub lstAvail_DblClick()
  407.     'Double clicking should be the same as clicking the 'Add' button
  408.     cmdAdd_Click
  409. End Sub
  410. Private Sub lstUse_DblClick()
  411.     'Double clicking should be the same as clicking the 'Remove' button
  412.     cmdRemove_Click
  413. End Sub
  414.