www.pudn.com > WMACompressor.zip > Form1.vb


Imports Yeti.MMedia 
Imports Yeti.MMedia.Wmf 
Imports WaveLib 
 
Public Class Form1 
  Inherits System.Windows.Forms.Form 
  Private m_Config As AudioWriterConfig 
  Private Compressing As Boolean 
 
#Region " Windows Form Designer generated code " 
 
  Public Sub New() 
    MyBase.New() 
 
    'This call is required by the Windows Form Designer. 
    InitializeComponent() 
 
    'Add any initialization after the InitializeComponent() call 
 
  End Sub 
 
  'Form overrides dispose to clean up the component list. 
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) 
    If disposing Then 
      If Not (components Is Nothing) Then 
        components.Dispose() 
      End If 
    End If 
    MyBase.Dispose(disposing) 
  End Sub 
 
  'Required by the Windows Form Designer 
  Private components As System.ComponentModel.IContainer 
 
  'NOTE: The following procedure is required by the Windows Form Designer 
  'It can be modified using the Windows Form Designer.   
  'Do not modify it using the code editor. 
  Friend WithEvents Label1 As System.Windows.Forms.Label 
  Friend WithEvents TextBoxInFile As System.Windows.Forms.TextBox 
  Friend WithEvents Label2 As System.Windows.Forms.Label 
  Friend WithEvents TextBoxOutFile As System.Windows.Forms.TextBox 
  Friend WithEvents ButtonInFile As System.Windows.Forms.Button 
  Friend WithEvents ButtonOutFile As System.Windows.Forms.Button 
  Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox 
  Friend WithEvents ProgressBar As System.Windows.Forms.ProgressBar 
  Friend WithEvents ButtonConfig As System.Windows.Forms.Button 
  Friend WithEvents ButtonCompress As System.Windows.Forms.Button 
  Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog 
  Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog 
   Private Sub InitializeComponent() 
    Me.Label1 = New System.Windows.Forms.Label 
    Me.TextBoxInFile = New System.Windows.Forms.TextBox 
    Me.Label2 = New System.Windows.Forms.Label 
    Me.TextBoxOutFile = New System.Windows.Forms.TextBox 
    Me.ButtonInFile = New System.Windows.Forms.Button 
    Me.ButtonOutFile = New System.Windows.Forms.Button 
    Me.GroupBox1 = New System.Windows.Forms.GroupBox 
    Me.ProgressBar = New System.Windows.Forms.ProgressBar 
    Me.ButtonConfig = New System.Windows.Forms.Button 
    Me.ButtonCompress = New System.Windows.Forms.Button 
    Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog 
    Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog 
    Me.SuspendLayout() 
    ' 
    'Label1 
    ' 
    Me.Label1.Location = New System.Drawing.Point(8, 8) 
    Me.Label1.Name = "Label1" 
    Me.Label1.TabIndex = 0 
    Me.Label1.Text = "Wave file:" 
    ' 
    'TextBoxInFile 
    ' 
    Me.TextBoxInFile.Location = New System.Drawing.Point(8, 24) 
    Me.TextBoxInFile.Name = "TextBoxInFile" 
    Me.TextBoxInFile.ReadOnly = True 
    Me.TextBoxInFile.Size = New System.Drawing.Size(288, 20) 
    Me.TextBoxInFile.TabIndex = 1 
    Me.TextBoxInFile.Text = "" 
    ' 
    'Label2 
    ' 
    Me.Label2.Location = New System.Drawing.Point(8, 56) 
    Me.Label2.Name = "Label2" 
    Me.Label2.Size = New System.Drawing.Size(224, 23) 
    Me.Label2.TabIndex = 2 
    Me.Label2.Text = "Windows Media Audio (WMA) file (result):" 
    ' 
    'TextBoxOutFile 
    ' 
    Me.TextBoxOutFile.Location = New System.Drawing.Point(8, 72) 
    Me.TextBoxOutFile.Name = "TextBoxOutFile" 
    Me.TextBoxOutFile.ReadOnly = True 
    Me.TextBoxOutFile.Size = New System.Drawing.Size(288, 20) 
    Me.TextBoxOutFile.TabIndex = 3 
    Me.TextBoxOutFile.Text = "" 
    ' 
    'ButtonInFile 
    ' 
    Me.ButtonInFile.Location = New System.Drawing.Point(304, 24) 
    Me.ButtonInFile.Name = "ButtonInFile" 
    Me.ButtonInFile.TabIndex = 4 
    Me.ButtonInFile.Text = "Browse..." 
    ' 
    'ButtonOutFile 
    ' 
    Me.ButtonOutFile.Enabled = False 
    Me.ButtonOutFile.Location = New System.Drawing.Point(304, 72) 
    Me.ButtonOutFile.Name = "ButtonOutFile" 
    Me.ButtonOutFile.TabIndex = 5 
    Me.ButtonOutFile.Text = "Browse..." 
    ' 
    'GroupBox1 
    ' 
    Me.GroupBox1.Location = New System.Drawing.Point(8, 112) 
    Me.GroupBox1.Name = "GroupBox1" 
    Me.GroupBox1.Size = New System.Drawing.Size(368, 8) 
    Me.GroupBox1.TabIndex = 6 
    Me.GroupBox1.TabStop = False 
    ' 
    'ProgressBar 
    ' 
    Me.ProgressBar.Dock = System.Windows.Forms.DockStyle.Bottom 
    Me.ProgressBar.Location = New System.Drawing.Point(0, 167) 
    Me.ProgressBar.Name = "ProgressBar" 
    Me.ProgressBar.Size = New System.Drawing.Size(386, 16) 
    Me.ProgressBar.TabIndex = 7 
    ' 
    'ButtonConfig 
    ' 
    Me.ButtonConfig.Enabled = False 
    Me.ButtonConfig.Location = New System.Drawing.Point(216, 136) 
    Me.ButtonConfig.Name = "ButtonConfig" 
    Me.ButtonConfig.TabIndex = 8 
    Me.ButtonConfig.Text = "Config..." 
    ' 
    'ButtonCompress 
    ' 
    Me.ButtonCompress.Enabled = False 
    Me.ButtonCompress.Location = New System.Drawing.Point(304, 136) 
    Me.ButtonCompress.Name = "ButtonCompress" 
    Me.ButtonCompress.TabIndex = 9 
    Me.ButtonCompress.Text = "Compress" 
    ' 
    'OpenFileDialog1 
    ' 
    Me.OpenFileDialog1.DefaultExt = "wav" 
    Me.OpenFileDialog1.Filter = "Wave files (*.wav)|*.wav|All files (*.*)|*.*""" 
    Me.OpenFileDialog1.Title = "Wave file to compress" 
    ' 
    'SaveFileDialog1 
    ' 
    Me.SaveFileDialog1.DefaultExt = "wma" 
    Me.SaveFileDialog1.Filter = "Windows Media Audio files (*.wma)|*.wma|All files (*.*)|*.*""" 
    Me.SaveFileDialog1.Title = "Compressed file" 
    ' 
    'Form1 
    ' 
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) 
    Me.ClientSize = New System.Drawing.Size(386, 183) 
    Me.Controls.Add(Me.ButtonCompress) 
    Me.Controls.Add(Me.ButtonConfig) 
    Me.Controls.Add(Me.ProgressBar) 
    Me.Controls.Add(Me.GroupBox1) 
    Me.Controls.Add(Me.ButtonOutFile) 
    Me.Controls.Add(Me.ButtonInFile) 
    Me.Controls.Add(Me.TextBoxOutFile) 
    Me.Controls.Add(Me.Label2) 
    Me.Controls.Add(Me.TextBoxInFile) 
    Me.Controls.Add(Me.Label1) 
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle 
    Me.MaximizeBox = False 
    Me.Name = "Form1" 
    Me.Text = "WMA Compress" 
    Me.ResumeLayout(False) 
 
  End Sub 
 
#End Region 
 
  Private Sub RefreshControls() 
    If (Compressing) Then 
      ButtonInFile.Enabled = False 
      ButtonOutFile.Enabled = False 
      ButtonConfig.Enabled = False 
      ButtonCompress.Enabled = False 
    Else 
      ButtonInFile.Enabled = True 
      ButtonOutFile.Enabled = True 
      ButtonConfig.Enabled = True 
      ButtonCompress.Enabled = True 
    End If 
  End Sub 
 
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Compressing = False 
    m_Config = New WmaWriterConfig(New WaveFormat(44100, 16, 2), WmaWriterProfileManager.AudioSystemProfiles(6)) 
  End Sub 
 
  Private Sub ButtonInFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonInFile.Click 
    Dim s As WaveStream 
    Dim cfg As WmaWriterConfig 
    If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then 
      Try 
        s = New WaveStream(OpenFileDialog1.FileName) 
        Try 
          cfg = New WmaWriterConfig(s.Format, CType(m_Config, WmaWriterConfig).Profile) 
          TextBoxInFile.Text = OpenFileDialog1.FileName 
          TextBoxOutFile.Text = System.IO.Path.ChangeExtension(TextBoxInFile.Text, ".wma") 
          m_Config = cfg 
        Finally 
          s.Close() 
        End Try 
      Catch 
        MessageBox.Show(Me, "Invalid wave file or format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
        TextBoxInFile.Text = "" 
        TextBoxOutFile.Text = "" 
      End Try 
    End If 
  End Sub 
 
  Private Sub ButtonOutFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOutFile.Click 
    SaveFileDialog1.FileName = TextBoxOutFile.Text 
    If SaveFileDialog1.ShowDialog(Me) = DialogResult.OK Then 
      TextBoxOutFile.Text = SaveFileDialog1.FileName 
    End If 
 
  End Sub 
 
  Private Sub ButtonConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonConfig.Click 
    Dim cfg As Config 
    cfg = New Config 
    cfg.EditWmaWriter1.Config = m_Config 
    If cfg.ShowDialog(Me) = DialogResult.OK Then 
      m_Config = cfg.EditWmaWriter1.Config 
    End If 
  End Sub 
 
  Private Sub ButtonCompress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCompress.Click 
    Dim InStr As WaveStream 
    Dim writer As AudioWriter 
    Dim buff() As Byte 
    Dim read As Integer 
    Dim actual As Integer 
    Dim total As Long 
 
    If System.IO.File.Exists(TextBoxOutFile.Text) Then 
      If MessageBox.Show(Me, "Override the existing file?", "File exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> DialogResult.Yes Then 
        Exit Sub 
      End If 
    End If 
    Try 
      ProgressBar.Value = 0 
      Me.Text = "Audio Compress" 
      Compressing = True 
      Try 
        RefreshControls() 
        InStr = New WaveStream(TextBoxInFile.Text) 
        Try 
          writer = New WmaWriter(New System.IO.FileStream(TextBoxOutFile.Text, IO.FileMode.Create), m_Config) 
          Try 
            buff = New Byte(writer.OptimalBufferSize - 1) {} 
            read = 0 
            actual = 0 
            total = InStr.Length 
            Cursor.Current = Cursors.WaitCursor 
            Try 
              read = InStr.Read(buff, 0, buff.Length) 
              While (read > 0) 
                Application.DoEvents() 
                writer.Write(buff, 0, read) 
                actual += read 
                ProgressBar.Value = (CLng(actual) * 100) / total 
                Me.Text = String.Format("Audio Compress - {0}% compresssed", ProgressBar.Value) 
                Application.DoEvents() 
                read = InStr.Read(buff, 0, buff.Length) 
              End While 
              Me.Text = "Audio Compress - Done" 
            Finally 
              Cursor.Current = Cursors.Default 
            End Try 
          Finally 
            writer.Close() 
          End Try 
        Finally 
          InStr.Close() 
        End Try 
      Finally 
        Compressing = False 
        RefreshControls() 
      End Try 
    Catch ex As Exception 
      MessageBox.Show(Me, ex.Message, "An exception has ocurred with the following message", MessageBoxButtons.OK, MessageBoxIcon.Error) 
      If IO.File.Exists(TextBoxOutFile.Text) Then 
        IO.File.Delete(TextBoxOutFile.Text) 
      End If 
    End Try 
  End Sub 
 
  Private Sub TextBoxInFile_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxInFile.TextChanged 
    If TextBoxInFile.Text <> "" Then 
      ButtonConfig.Enabled = True 
      ButtonOutFile.Enabled = True 
      ButtonCompress.Enabled = True 
    Else 
      ButtonCompress.Enabled = False 
      ButtonConfig.Enabled = False 
      ButtonOutFile.Enabled = False 
    End If 
  End Sub 
 
  Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing 
    e.Cancel = Compressing 
  End Sub 
End Class