home *** CD-ROM | disk | FTP | other *** search
/ Beginning Direct3D Game Programming / Direct3D.iso / directx / dxf / samples / multimedia / vbsamples / directmusic / playaudio / frmaudio.frm (.txt) next >
Encoding:
Visual Basic Form  |  2000-10-02  |  13.5 KB  |  406 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
  4. Begin VB.Form frmAudio 
  5.    BorderStyle     =   1  'Fixed Single
  6.    Caption         =   "Play Audio"
  7.    ClientHeight    =   2520
  8.    ClientLeft      =   150
  9.    ClientTop       =   435
  10.    ClientWidth     =   4890
  11.    Icon            =   "frmAudio.frx":0000
  12.    LinkTopic       =   "Form1"
  13.    MaxButton       =   0   'False
  14.    ScaleHeight     =   2520
  15.    ScaleWidth      =   4890
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin VB.Frame Frame1 
  18.       Caption         =   "Master Volume"
  19.       Height          =   675
  20.       Index           =   1
  21.       Left            =   2520
  22.       TabIndex        =   9
  23.       Top             =   1680
  24.       Width           =   2295
  25.       Begin MSComctlLib.Slider sldVolume 
  26.          Height          =   195
  27.          Left            =   180
  28.          TabIndex        =   10
  29.          Top             =   420
  30.          Width           =   1995
  31.          _ExtentX        =   3519
  32.          _ExtentY        =   344
  33.          _Version        =   393216
  34.          LargeChange     =   1000
  35.          SmallChange     =   100
  36.          Min             =   -2500
  37.          Max             =   200
  38.          SelStart        =   200
  39.          TickFrequency   =   500
  40.          Value           =   200
  41.       End
  42.       Begin VB.Label lbl 
  43.          BackStyle       =   0  'Transparent
  44.          Caption         =   "Max"
  45.          Height          =   255
  46.          Index           =   3
  47.          Left            =   1860
  48.          TabIndex        =   12
  49.          Top             =   180
  50.          Width           =   315
  51.       End
  52.       Begin VB.Label lbl 
  53.          BackStyle       =   0  'Transparent
  54.          Caption         =   "Min"
  55.          Height          =   255
  56.          Index           =   1
  57.          Left            =   180
  58.          TabIndex        =   11
  59.          Top             =   180
  60.          Width           =   315
  61.       End
  62.    End
  63.    Begin VB.Frame fraTempo 
  64.       Caption         =   "Tempo"
  65.       Height          =   675
  66.       Left            =   60
  67.       TabIndex        =   8
  68.       Top             =   1680
  69.       Width           =   2295
  70.       Begin MSComctlLib.Slider sldTempo 
  71.          Height          =   195
  72.          Left            =   120
  73.          TabIndex        =   13
  74.          Top             =   420
  75.          Width           =   1995
  76.          _ExtentX        =   3519
  77.          _ExtentY        =   344
  78.          _Version        =   393216
  79.          Max             =   30
  80.          SelStart        =   10
  81.          TickFrequency   =   5
  82.          Value           =   10
  83.       End
  84.       Begin VB.Label lbl 
  85.          BackStyle       =   0  'Transparent
  86.          Caption         =   "Fast"
  87.          Height          =   255
  88.          Index           =   6
  89.          Left            =   1680
  90.          TabIndex        =   16
  91.          Top             =   180
  92.          Width           =   375
  93.       End
  94.       Begin VB.Label lbl 
  95.          BackStyle       =   0  'Transparent
  96.          Caption         =   "Normal"
  97.          Height          =   255
  98.          Index           =   5
  99.          Left            =   540
  100.          TabIndex        =   15
  101.          Top             =   180
  102.          Width           =   615
  103.       End
  104.       Begin VB.Label lbl 
  105.          BackStyle       =   0  'Transparent
  106.          Caption         =   "Slow"
  107.          Height          =   255
  108.          Index           =   4
  109.          Left            =   120
  110.          TabIndex        =   14
  111.          Top             =   180
  112.          Width           =   375
  113.       End
  114.    End
  115.    Begin VB.CommandButton cmdExit 
  116.       Caption         =   "E&xit"
  117.       Height          =   315
  118.       Left            =   3840
  119.       TabIndex        =   7
  120.       Top             =   1260
  121.       Width           =   975
  122.    End
  123.    Begin VB.CheckBox chkLoop 
  124.       Caption         =   "Loop Audio"
  125.       Height          =   255
  126.       Left            =   60
  127.       TabIndex        =   6
  128.       Top             =   1320
  129.       Width           =   1155
  130.    End
  131.    Begin VB.TextBox txtFile 
  132.       BackColor       =   &H8000000F&
  133.       Height          =   285
  134.       Left            =   1140
  135.       Locked          =   -1  'True
  136.       TabIndex        =   5
  137.       Top             =   900
  138.       Width           =   3675
  139.    End
  140.    Begin VB.CommandButton cmdOpen 
  141.       Caption         =   "&Audio File"
  142.       Height          =   315
  143.       Left            =   120
  144.       TabIndex        =   0
  145.       Top             =   900
  146.       Width           =   975
  147.    End
  148.    Begin VB.CommandButton cmdPlay 
  149.       Caption         =   "&Play"
  150.       Enabled         =   0   'False
  151.       Height          =   315
  152.       Left            =   1320
  153.       TabIndex        =   1
  154.       Top             =   1260
  155.       Width           =   975
  156.    End
  157.    Begin VB.CommandButton cmdStop 
  158.       Caption         =   "&Stop"
  159.       Enabled         =   0   'False
  160.       Height          =   315
  161.       Left            =   2340
  162.       TabIndex        =   2
  163.       Top             =   1260
  164.       Width           =   975
  165.    End
  166.    Begin MSComDlg.CommonDialog cdlOpen 
  167.       Left            =   3000
  168.       Top             =   0
  169.       _ExtentX        =   847
  170.       _ExtentY        =   847
  171.       _Version        =   393216
  172.       Flags           =   4
  173.    End
  174.    Begin VB.Image Image1 
  175.       Height          =   480
  176.       Left            =   60
  177.       Picture         =   "frmAudio.frx":0442
  178.       Top             =   60
  179.       Width           =   480
  180.    End
  181.    Begin VB.Label lbl 
  182.       BackStyle       =   0  'Transparent
  183.       Caption         =   "Copyright 
  184.  2000, Microsoft Corporation All Rights Reserved."
  185.       Height          =   495
  186.       Index           =   2
  187.       Left            =   600
  188.       TabIndex        =   4
  189.       Top             =   300
  190.       Width           =   3015
  191.    End
  192.    Begin VB.Label lbl 
  193.       BackStyle       =   0  'Transparent
  194.       Caption         =   "Play Audio Sample"
  195.       Height          =   255
  196.       Index           =   0
  197.       Left            =   600
  198.       TabIndex        =   3
  199.       Top             =   60
  200.       Width           =   2655
  201.    End
  202. Attribute VB_Name = "frmAudio"
  203. Attribute VB_GlobalNameSpace = False
  204. Attribute VB_Creatable = False
  205. Attribute VB_PredeclaredId = True
  206. Attribute VB_Exposed = False
  207. Option Explicit
  208. Option Compare Text
  209. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  210. '  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
  211. '  File:       frmAudio.frm
  212. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  213. Implements DirectXEvent8
  214. Private dx As New DirectX8
  215. 'We need a loader object and a performance object
  216. 'We will play everything on our default audio path, so we do not need an audiopath object
  217. Private dmp As DirectMusicPerformance8
  218. Private dml As DirectMusicLoader8
  219. Private dmSeg As DirectMusicSegment8
  220. 'Our event handle
  221. Private dmEvent As Long
  222. 'API declare for windows folder
  223. Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  224. Private Sub cmdExit_Click()
  225.     Unload Me 'Cleanup happens in form unload
  226. End Sub
  227. Private Sub cmdOpen_Click()
  228.     Static sCurDir As String
  229.     Static lFilter As Long
  230.     'We want to open a file now
  231.     cdlOpen.flags = cdlOFNHideReadOnly Or cdlOFNFileMustExist
  232.     cdlOpen.FilterIndex = lFilter
  233.     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 (*.*)|*.*"
  234.     cdlOpen.FileName = vbNullString
  235.     If sCurDir = vbNullString Then
  236.         'Set the init folder to \windows\media if it exists.  If not, set it to the \windows folder
  237.         Dim sWindir As String
  238.         sWindir = Space$(255)
  239.         If GetWindowsDirectory(sWindir, 255) = 0 Then
  240.             'We couldn't get the windows folder for some reason, use the c:\
  241.             cdlOpen.InitDir = "C:\"
  242.         Else
  243.             Dim sMedia As String
  244.             sWindir = Left$(sWindir, InStr(sWindir, Chr$(0)) - 1)
  245.             If Right$(sWindir, 1) = "\" Then
  246.                 sMedia = sWindir & "Media"
  247.             Else
  248.                 sMedia = sWindir & "\Media"
  249.             End If
  250.             If Dir$(sMedia, vbDirectory) <> vbNullString Then
  251.                 cdlOpen.InitDir = sMedia
  252.             Else
  253.                 cdlOpen.InitDir = sWindir
  254.             End If
  255.         End If
  256.     Else
  257.         cdlOpen.InitDir = sCurDir
  258.     End If
  259.     On Local Error GoTo ClickedCancel
  260.     cdlOpen.CancelError = True
  261.     cdlOpen.ShowOpen   ' Display the Open dialog box
  262.     'Save the current information
  263.     sCurDir = GetFolder(cdlOpen.FileName)
  264.     'Set the search folder to this one so we can auto download anything we need
  265.     dml.SetSearchDirectory sCurDir
  266.     lFilter = cdlOpen.FilterIndex
  267.             
  268.     On Local Error GoTo NoLoadSegment
  269.     'Before we load the segment stop one if it's playing
  270.     cmdStop_Click
  271.     'Now let's load the segment
  272.     If FileLen(cdlOpen.FileName) = 0 Then Err.Raise 5
  273.     EnableTempoControl (Right$(cdlOpen.FileName, 4) <> ".wav")
  274.     Set dmSeg = dml.LoadSegment(cdlOpen.FileName)
  275.     If (Right$(cdlOpen.FileName, 4) = ".mid") Or (Right$(cdlOpen.FileName, 4) = ".rmi") Or (Right$(cdlOpen.FileName, 5) = ".midi") Then
  276.         dmSeg.SetStandardMidiFile
  277.     End If
  278.     txtFile.Text = cdlOpen.FileName
  279.     EnablePlayUI True
  280.     sldTempo.Value = 10
  281.     sldTempo_Click
  282.     Exit Sub
  283. NoLoadSegment:
  284.     MsgBox "Couldn't load this segment", vbOKOnly Or vbCritical, "Couldn't load"
  285. ClickedCancel:
  286. End Sub
  287. Private Sub cmdPlay_Click()
  288.     If Not (dmSeg Is Nothing) Then
  289.         If chkLoop.Value = vbChecked Then
  290.             dmSeg.SetRepeats -1 'Loop infinitely
  291.         Else
  292.             dmSeg.SetRepeats 0 'Don't loop
  293.         End If
  294.         dmp.PlaySegmentEx dmSeg, DMUS_SEGF_DEFAULT, 0
  295.         EnablePlayUI False
  296.     End If
  297. End Sub
  298. Private Sub cmdStop_Click()
  299.     If Not (dmSeg Is Nothing) Then dmp.StopEx dmSeg, 0, 0
  300.     EnablePlayUI True
  301. End Sub
  302. Private Sub DirectXEvent8_DXCallback(ByVal eventid As Long)
  303.     Dim dmNotification As DMUS_NOTIFICATION_PMSG
  304.     'We only have one event
  305.     If Not dmp.GetNotificationPMSG(dmNotification) Then
  306.         MsgBox "Error processing this Notification", vbOKOnly Or vbInformation, "Cannot Process."
  307.         Exit Sub
  308.     Else
  309.         If dmNotification.lNotificationOption = DMUS_NOTIFICATION_SEGEND Then 'The segment has ended
  310.             EnablePlayUI True
  311.         End If
  312.     End If
  313. End Sub
  314. Private Sub Form_Load()
  315.     InitAudio
  316.     EnableTempoControl False
  317. End Sub
  318. Private Sub InitAudio()
  319.     On Error GoTo FailedInit
  320.     'We need to create our objects now
  321.     Set dmp = dx.DirectMusicPerformanceCreate
  322.     Set dml = dx.DirectMusicLoaderCreate
  323.     Dim dmusAudio As DMUS_AUDIOPARAMS
  324.     'Now call init audio
  325.     dmp.InitAudio Me.hWnd, DMUS_AUDIOF_ALL, dmusAudio, Nothing, DMUS_APATH_SHARED_STEREOPLUSREVERB, 128
  326.     dmp.SetMasterAutoDownload True
  327.     'Now add a notification for the segment
  328.     dmp.AddNotificationType DMUS_NOTIFY_ON_SEGMENT
  329.     'Create an event so we can receive these
  330.     dmEvent = dx.CreateEvent(Me)
  331.     dmp.SetNotificationHandle dmEvent
  332.     Exit Sub
  333. FailedInit:
  334.     MsgBox "Could not initialize DirectMusic." & vbCrLf & "This sample will exit.", vbOKOnly Or vbInformation, "Exiting..."
  335.     CleanupAudio
  336.     Unload Me
  337.     End
  338. End Sub
  339. Private Sub CleanupAudio()
  340.     'Cleanup everything
  341.     On Error Resume Next
  342.     dmp.RemoveNotificationType DMUS_NOTIFY_ON_SEGMENT
  343.     dx.DestroyEvent dmEvent
  344.     If Not (dmSeg Is Nothing) Then dmp.StopEx dmSeg, 0, 0
  345.     Set dmSeg = Nothing
  346.     Set dml = Nothing
  347.     If Not (dmp Is Nothing) Then dmp.CloseDown
  348.     Set dmp = Nothing
  349. End Sub
  350. Private Sub Form_Unload(Cancel As Integer)
  351.     CleanupAudio
  352. End Sub
  353. Private Function GetFolder(ByVal sFile As String) As String
  354.     Dim lCount As Long
  355.     For lCount = Len(sFile) To 1 Step -1
  356.         If Mid$(sFile, lCount, 1) = "\" Then
  357.             GetFolder = Left$(sFile, lCount)
  358.             Exit Function
  359.         End If
  360.     Next
  361.     GetFolder = vbNullString
  362. End Function
  363. Public Sub EnablePlayUI(fEnable As Boolean)
  364.     'Enable/Disable the buttons
  365.     If fEnable Then
  366.         chkLoop.Enabled = True
  367.         cmdStop.Enabled = False
  368.         cmdPlay.Enabled = True
  369.         cmdOpen.Enabled = True
  370.         cmdPlay.SetFocus
  371.     Else
  372.         chkLoop.Enabled = False
  373.         cmdStop.Enabled = True
  374.         cmdPlay.Enabled = False
  375.         cmdOpen.Enabled = False
  376.         cmdStop.SetFocus
  377.     End If
  378. End Sub
  379. Private Sub sldTempo_Click()
  380.     'Update the tempo now
  381.     dmp.SetMasterTempo (sldTempo.Value / 10)
  382. End Sub
  383. Private Sub sldTempo_Scroll()
  384.     sldTempo_Click
  385. End Sub
  386. Private Sub sldVolume_Click()
  387.     sldVolume_Scroll
  388. End Sub
  389. Private Sub sldVolume_Scroll()
  390.     'Update the volume
  391.     dmp.SetMasterVolume sldVolume.Value
  392. End Sub
  393. Private Sub EnableTempoControl(ByVal fEnable As Boolean)
  394.     'If this is a wave file, turn off tempo control
  395.     fraTempo.Enabled = fEnable
  396.     sldTempo.Enabled = fEnable
  397.     lbl(4).Enabled = fEnable
  398.     lbl(5).Enabled = fEnable
  399.     lbl(6).Enabled = fEnable
  400.     If Not fEnable Then
  401.         sldTempo.TickStyle = sldNoTicks
  402.     Else
  403.         sldTempo.TickStyle = sldBottomRight
  404.     End If
  405. End Sub
  406.