vbAccelerator - Contents of code file: frmWinAmpPlugin.frm

This file is part of the download VB5 Winamp Plugin Client, which is described in the article Using WinAmp In Plugins From VB.

VERSION 5.00
Begin VB.Form frmWinAmpPlugin 
   Caption         =   "vbAccelerator WinAmp Plugin Sample"
   ClientHeight    =   5280
   ClientLeft      =   6435
   ClientTop       =   2610
   ClientWidth     =   6450
   BeginProperty Font 
      Name            =   "Tahoma"
      Size            =   8.25
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   Icon            =   "frmWinAmpPlugin.frx":0000
   LinkTopic       =   "Form1"
   ScaleHeight     =   5280
   ScaleWidth      =   6450
   Begin VB.PictureBox picWavePlayer 
      BorderStyle     =   0  'None
      Height          =   2475
      Left            =   60
      ScaleHeight     =   2475
      ScaleWidth      =   6315
      TabIndex        =   10
      Top             =   2640
      Width           =   6315
      Begin VB.PictureBox picPlayProgress 
         BorderStyle     =   0  'None
         Height          =   315
         Left            =   0
         ScaleHeight     =   315
         ScaleWidth      =   6315
         TabIndex        =   16
         Top             =   2160
         Width           =   6315
      End
      Begin VB.TextBox txtPlayFile 
         Height          =   315
         Left            =   1080
         TabIndex        =   13
         Top             =   300
         Width           =   4875
      End
      Begin VB.CommandButton cmdPickPlayFile 
         Caption         =   "..."
         Height          =   315
         Left            =   6000
         TabIndex        =   12
         Top             =   300
         Width           =   315
      End
      Begin VB.CommandButton cmdPlay 
         Caption         =   "&Play"
         Height          =   375
         Left            =   1080
         TabIndex        =   11
         Top             =   1080
         Width           =   1275
      End
      Begin VB.Label lblTrackInfo 
         Height          =   315
         Left            =   1080
         TabIndex        =   17
         Top             =   660
         Width           =   4875
      End
      Begin VB.Label lblPlayer 
         BackColor       =   &H80000010&
         Caption         =   " Audio File Player"
         ForeColor       =   &H80000016&
         Height          =   255
         Left            =   0
         TabIndex        =   15
         Top             =   0
         Width           =   6315
      End
      Begin VB.Label Label3 
         Caption         =   "File:"
         Height          =   255
         Left            =   60
         TabIndex        =   14
         Top             =   360
         Width           =   1035
      End
   End
   Begin VB.PictureBox picProgress 
      BorderStyle     =   0  'None
      Height          =   315
      Left            =   60
      ScaleHeight     =   315
      ScaleWidth      =   6315
      TabIndex        =   8
      Top             =   2220
      Width           =   6315
   End
   Begin VB.PictureBox picWaveWriter 
      BorderStyle     =   0  'None
      Height          =   2475
      Left            =   60
      ScaleHeight     =   2475
      ScaleWidth      =   6315
      TabIndex        =   0
      Top             =   60
      Width           =   6315
      Begin VB.CommandButton cmdDecode 
         Caption         =   "&Decode"
         Height          =   375
         Left            =   1080
         TabIndex        =   9
         Top             =   1080
         Width           =   1275
      End
      Begin VB.CommandButton cmdPickDestination 
         Caption         =   "..."
         Height          =   315
         Left            =   6000
         TabIndex        =   7
         Top             =   660
         Width           =   315
      End
      Begin VB.TextBox txtDestination 
         Height          =   315
         Left            =   1080
         TabIndex        =   6
         Top             =   660
         Width           =   4875
      End
      Begin VB.CommandButton cmdPickSource 
         Caption         =   "..."
         Height          =   315
         Left            =   6000
         TabIndex        =   4
         Top             =   300
         Width           =   315
      End
      Begin VB.TextBox txtSource 
         Height          =   315
         Left            =   1080
         TabIndex        =   3
         Top             =   300
         Width           =   4875
      End
      Begin VB.Label lblDestination 
         Caption         =   "Destination:"
         Height          =   255
         Left            =   60
         TabIndex        =   5
         Top             =   720
         Width           =   1035
      End
      Begin VB.Label lblSource 
         Caption         =   "Source File:"
         Height          =   255
         Left            =   60
         TabIndex        =   2
         Top             =   360
         Width           =   1035
      End
      Begin VB.Label lblWaveWriter 
         BackColor       =   &H80000010&
         Caption         =   " Wave File Writer"
         ForeColor       =   &H80000016&
         Height          =   255
         Left            =   0
         TabIndex        =   1
         Top             =   0
         Width           =   6315
      End
   End
End
Attribute VB_Name = "frmWinAmpPlugin"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Enum EDemoMode
   eDemoModeIdle
   eDemoModeDecoding
   eDemoModePlaying
   eDemoModePaused
End Enum
Private m_eMode As EDemoMode

Private m_cWriteProgress As New pcProgressBar
Private m_cPlayProgress As New pcProgressBar

Private m_cConverter As cWinAmpAudioConverter
Private WithEvents m_cWaveConverter As cConverterWaveWriter
Attribute m_cWaveConverter.VB_VarHelpID = -1
Private WithEvents m_cPlayer As cConverterWavePlayer
Attribute m_cPlayer.VB_VarHelpID = -1
Private m_bCancelWave As Boolean

Private Function GetDirectory(ByVal sFile As String) As String
Dim i As Long
Dim sDir As String
   sDir = sFile
   For i = Len(sFile) To 1 Step -1
      If (Mid(sFile, i, 1) = "\") Then
         If (i > 1) Then
            sDir = Left(sFile, i - 1)
            Exit For
         End If
      End If
   Next i
   GetDirectory = sDir
End Function
Private Function GetFileNameWithoutExtension(ByVal sFile As String) As String
Dim i As Long
Dim sRet As String
Dim bExtFound As Boolean
   sRet = sFile
   For i = Len(sFile) To 1 Step -1
      If Not (bExtFound) Then
         If (Mid(sFile, i, 1) = ".") Then
            If (i > 1) Then
               sRet = Left(sFile, i - 1)
               bExtFound = True
            End If
         End If
      End If
      If (Mid(sFile, i, 1) = "\") Then
         sRet = Mid(sRet, i + 1)
         Exit For
      End If
   Next i
   GetFileNameWithoutExtension = sRet
End Function
Private Function PathCombine(ByVal sDir As String, ByVal sPathPart As String)
 As String
   If (Right(sDir, 1) <> "\") Then sDir = sDir & "\"
   PathCombine = sDir & sPathPart
End Function

Private Property Get PluginDir() As String
   PluginDir = PathCombine(App.Path, "Plugins")
End Property

Private Sub cmdDecode_Click()
   If (cmdDecode.Caption = "&Decode") Then
      If (m_cConverter.OpenFile(txtSource.Text)) Then
         cmdPlay.Tag = ""
         SetMode eDemoModeDecoding
         cmdDecode.Caption = "Cancel"
         m_cWaveConverter.Filename = txtDestination.Text
         m_cWaveConverter.Decode m_cConverter
         m_cConverter.CloseFile
      Else
         MsgBox "Could not open the file " & txtSource.Text, vbInformation
      End If
      m_cWriteProgress.Value = 100
      cmdDecode.Enabled = True
      cmdDecode.Caption = "&Decode"
      SetMode eDemoModeIdle
   Else
      cmdDecode.Enabled = False
      m_bCancelWave = True
   End If
End Sub

Private Sub cmdPickDestination_Click()
Dim cD As New pcCommonDialog
Dim sFile As String
   If (cD.VBGetSaveFileName(sFile, _
      Filter:="Wave Files (*.WAV)|*.WAV|All Files (*.*)|*.*", _
      DefaultExt:="WAV", _
      Owner:=Me.hWnd)) Then
      txtDestination.Text = sFile
   End If
End Sub

Private Sub cmdPickPlayFile_Click()
Dim cD As New pcCommonDialog
Dim sFile As String
   
   If (m_cConverter.FileIsOpen) Then
      If Len(cmdPlay.Tag) > 0 Then
         If (cmdPlay.Caption = "&Pause") Then
            m_cPlayer.StopPlay
         End If
      End If
   End If

   cmdPlay.Caption = "&Play"
   cmdPlay.Tag = ""

   If (cD.VBGetOpenFileName(sFile, _
      Filter:="Audio Files
       (*.MP3;*.M4A;*.AAC;*.APE;*.OGG;*.WMA)|*.MP3;*.M4A;*.AAC;*.APE;*.OGG;*.WMA
      |All Files (*.*)|*.*", _
      DefaultExt:="MP3", _
      Owner:=Me.hWnd)) Then
      txtPlayFile.Text = sFile
   End If
   
End Sub

Private Sub cmdPickSource_Click()
Dim cD As New pcCommonDialog
Dim sFile As String
   If (cD.VBGetOpenFileName(sFile, _
      Filter:="Audio Files
       (*.MP3;*.M4A;*.AAC;*.APE;*.OGG;*.WMA)|*.MP3;*.M4A;*.AAC;*.APE;*.OGG;*.WMA
      |All Files (*.*)|*.*", _
      DefaultExt:="MP3", _
      Owner:=Me.hWnd)) Then
      txtSource.Text = sFile
      txtDestination.Text = PathCombine(GetDirectory(sFile),
       GetFileNameWithoutExtension(sFile) & ".WAV")
   End If
End Sub

Private Sub cmdPlay_Click()
   
   If (cmdPlay.Caption = "&Play") Then
      
      ' Paused:
      If Len(cmdPlay.Tag) > 0 Then
         m_cPlayer.Pause False
         cmdPlay.Caption = "&Pause"
         SetMode eDemoModePlaying
      Else
         
         If m_cConverter.OpenFile(txtPlayFile.Text) Then
            cmdPlay.Tag = txtPlayFile.Text
            cmdPlay.Caption = "&Pause"
            m_cPlayer.Play m_cConverter
            m_cPlayProgress.Max = m_cConverter.TrackTotalTime
            ShowTrackPosition
            SetMode eDemoModePlaying
         Else
            SetMode eDemoModeIdle
         End If
         
      End If
   Else
      ' Playing so Pause
      m_cPlayer.Pause True
      cmdPlay.Caption = "&Play"
      SetMode eDemoModePaused
   End If
   
End Sub

Private Sub ShowTrackPosition()
   lblTrackInfo.Caption = ToTime(m_cConverter.TrackCurrentTime) & " / " &
    ToTime(m_cConverter.TrackTotalTime)
End Sub

Private Function ToTime(ByVal lMs As Long) As String
Dim lMinutes As Long
Dim lSeconds As Long
   lMinutes = lMs \ 60000
   lSeconds = (((lMs Mod 60000) \ 1000) * 60) \ 100
   ToTime = format(lMinutes, "#0") & ":" & format(lSeconds, "00")
End Function

Private Sub Command1_Click()
   m_cPlayer.FileSeek 180000
End Sub

Private Sub Form_Load()

   Me.Show
   Me.Refresh
   
   Set m_cWriteProgress = New pcProgressBar
   With m_cWriteProgress
      .XpStyle = True
      .Value = 0
      .Max = 100
      .DrawObject = picProgress
   End With
   Set m_cPlayProgress = New pcProgressBar
   With m_cPlayProgress
      .XpStyle = True
      .Value = 0
      .Max = 100
      .DrawObject = picPlayProgress
   End With
      
   Set m_cConverter = New cWinAmpAudioConverter
   m_cConverter.Init PluginDir, Me.hWnd
   If (m_cConverter.PluginCount = 0) Then
      MsgBox "No plugins could be loaded from the directory " & PluginDir,
       vbExclamation
   End If
   
   Set m_cWaveConverter = New cConverterWaveWriter
   Set m_cPlayer = New cConverterWavePlayer
   m_cPlayer.Attach Me.hWnd

End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   m_cPlayer.StopPlay
End Sub

Private Sub m_cPlayer_Complete()
   cmdPlay.Tag = ""
   cmdPlay.Caption = "&Play"
   SetMode eDemoModeIdle
End Sub

Private Sub m_cPlayer_Progress(ByVal lTime As Long)
   m_cPlayProgress.Value = lTime
   m_cPlayProgress.Max = m_cConverter.TrackTotalTime
   ShowTrackPosition
End Sub

Private Sub m_cWaveConverter_Progress(ByVal lPercentDone As Long, bCancel As
 Boolean)
   If (m_bCancelWave) Then
      bCancel = True
      cmdDecode.Caption = "&Decode"
      cmdDecode.Enabled = True
      SetMode eDemoModeIdle
   Else
      m_cWriteProgress.Value = lPercentDone
   End If
End Sub

Private Sub picPlayProgress_Paint()
   m_cPlayProgress.Draw
End Sub

Private Sub picProgress_Paint()
   m_cWriteProgress.Draw
End Sub

Private Sub SetMode(ByVal eMode As EDemoMode)
   m_eMode = eMode
   Select Case m_eMode
   Case eDemoModeIdle, eDemoModePaused
      picWaveWriter.Enabled = True
      picWavePlayer.Enabled = True
      cmdDecode.Enabled = True
      cmdPlay.Enabled = True
      cmdPickSource.Enabled = True
      cmdPickDestination.Enabled = True
      cmdPickPlayFile.Enabled = True
      
   Case eDemoModeDecoding
      picWaveWriter.Enabled = True
      picWavePlayer.Enabled = False
      cmdDecode.Enabled = True
      cmdPlay.Enabled = False
      cmdPickSource.Enabled = False
      cmdPickDestination.Enabled = False
      cmdPickPlayFile.Enabled = False
   
   Case eDemoModePlaying
      picWaveWriter.Enabled = False
      picWavePlayer.Enabled = True
      cmdDecode.Enabled = False
      cmdPlay.Enabled = True
      cmdPickSource.Enabled = False
      cmdPickDestination.Enabled = False
      cmdPickPlayFile.Enabled = True

   End Select
End Sub

Public Sub Test()

End Sub