Click here to Skip to main content
13,295,218 members (56,768 online)
Click here to Skip to main content


105 bookmarked
Posted 8 Apr 2004

Windows Media Audio compressor

, 8 Apr 2004
Managed C++ Windows Media Audio (WMA) compressor.
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()

    'This call is required by the Windows Form Designer.

    '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
      End If
    End If
  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
  <System.Diagnostics.DebuggerStepThrough()> 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.Label1.Location = New System.Drawing.Point(8, 8)
    Me.Label1.Name = "Label1"
    Me.Label1.TabIndex = 0
    Me.Label1.Text = "Wave file:"
    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 = ""
    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):"
    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 = ""
    Me.ButtonInFile.Location = New System.Drawing.Point(304, 24)
    Me.ButtonInFile.Name = "ButtonInFile"
    Me.ButtonInFile.TabIndex = 4
    Me.ButtonInFile.Text = "Browse..."
    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..."
    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
    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
    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..."
    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"
    Me.OpenFileDialog1.DefaultExt = "wav"
    Me.OpenFileDialog1.Filter = "Wave files (*.wav)|*.wav|All files (*.*)|*.*"""
    Me.OpenFileDialog1.Title = "Wave file to compress"
    Me.SaveFileDialog1.DefaultExt = "wma"
    Me.SaveFileDialog1.Filter = "Windows Media Audio files (*.wma)|*.wma|All files (*.*)|*.*"""
    Me.SaveFileDialog1.Title = "Compressed file"
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(386, 183)
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
    Me.MaximizeBox = False
    Me.Name = "Form1"
    Me.Text = "WMA Compress"

  End Sub

#End Region

  Private Sub RefreshControls()
    If (Compressing) Then
      ButtonInFile.Enabled = False
      ButtonOutFile.Enabled = False
      ButtonConfig.Enabled = False
      ButtonCompress.Enabled = False
      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
        s = New WaveStream(OpenFileDialog1.FileName)
          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
        End Try
        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
      ProgressBar.Value = 0
      Me.Text = "Audio Compress"
      Compressing = True
        InStr = New WaveStream(TextBoxInFile.Text)
          writer = New WmaWriter(New System.IO.FileStream(TextBoxOutFile.Text, IO.FileMode.Create), m_Config)
            buff = New Byte(writer.OptimalBufferSize - 1) {}
            read = 0
            actual = 0
            total = InStr.Length
            Cursor.Current = Cursors.WaitCursor
              read = InStr.Read(buff, 0, buff.Length)
              While (read > 0)
                writer.Write(buff, 0, read)
                actual += read
                ProgressBar.Value = (CLng(actual) * 100) / total
                Me.Text = String.Format("Audio Compress - {0}% compresssed", ProgressBar.Value)
                read = InStr.Read(buff, 0, buff.Length)
              End While
              Me.Text = "Audio Compress - Done"
              Cursor.Current = Cursors.Default
            End Try
          End Try
        End Try
        Compressing = False
      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
      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
      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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Idael Cardoso
Web Developer
France France
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 9 Apr 2004
Article Copyright 2004 by Idael Cardoso
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid