WAV to MP3 converter in C#.NET


In this article I will show how to convert WAV format audio files to MP3 format audio files with bit rate and sampling frequency. For converting I used LAME encoder.

WAV to MP3 converter in C#.NET



Introduction


In this article I will show how to convert WAV format audio files to MP3 format audio files with bit rate and sampling frequency. For converting I used LAME encoder. Here is a step by step procedure to convert WAV format audio files to MP3 audio files in C#.NET.

Step1 : Designing Part


Create windows application in visual studio and add 2 Label Controls, 2 Textbox Controls, 2 Combo Box Controls, 3 Button Controls, 1 OpenFileDialog Control, 1 FolderBrowserDialog Control, 1 ProgressBar Control.

Rename corresponding controls properties as shown below & arrange particular format shown in below image.

textbox1 name ---> TotalFiles
textbox2 name ---> SaveFilesPath
textbox1 enabled --->false
textbox2 enabled ---> false
textbox1 readonly ---> true
textbox2 readonly ---> true
button1 name ---> BrowseButton
button1 text ---> Browse
button2 name ---> SaveAsButton
button2 text ---> Save
button3 name ---> ConvertButton
button3 text ---> Convert
label1 text ---> Bitrate (Kbps)
label2 text ---> Sampling Rate (KHz)
combobox1 name ---> BitrateComboBox
combobox1 items ---> 32,40,48,56,64,80,96,112,128,144,160
combobox2 name ---> SamplingrateComboBox
combobox2 items ---> 16,22.05,24
progressbar1 visible ---> false
openfiledialog1 name ---> OpenWavFilesDialog
openfiledialog1 filter ---> WAV Files|*.wav
openfiledialog1 multiselect ---> true
folderbrowsedialog1 name ---> SaveMP3FolderBrowserDialog

Step2 : Gather Requirements


Here we use LAME encoder for converting WAV files to MP3 files.
You can download this from lame.sourceforge.net/download.php
You can also find LAME encoder file in this application download file.
Now add LAME.exe file to this application executable file location.(because easy to access this file via Application.StartupPath)
i.e. D:/WAVtoMP3/WAVtoMP3/bin/debug/WindowsApplicaion.exe
or D:/WAVtoMP3/WAVtoMP3/bin/release/WindowsApplicaion.exe

Step3 : Coding Part


Now we can browse WAV files via OpenWavFilesDialog.
so double click on Browse Button & write below code in BrowseButton click event

if (OpenWavFilesDialog.ShowDialog() == DialogResult.OK)
{
TotalFiles.Text = "Total Files = "+ OpenWavFilesDialog.FileNames.Length.ToString();
}

After selecting wav files and click on open button in the OpenWavFilesDialog control, TotalFiles textbox display total number of wav files we select.
Next we give a path for saving converted MP3 files.
so we give this path via SaveMP3FolderBrowserDialogbox control.
so double click on Save Button & write below code in SaveAsButton click event

if (SaveMP3FolderBrowserDialog.ShowDialog() == DialogResult.OK)
{
SaveFilesPath.Text=SaveMP3FolderBrowserDialog.SelectedPath.ToString();
}

After selecting path for saving converted MP3 files you can see the path in SaveFilesPath textbox.
Now we can assign default values for converting bitrates for MP3 file.
so double click on form & write below code in form load event

BitrateComboBox.SelectedIndex = 8;
SamplingrateComboBox.SelectedIndex = 1;


Step 4: LAME Encoder Commandline Arguments


Now we can write code for converting process.
Here we can use LAME encoder file for converting process.
LAME.exe file is a commandline tool.
so first we can open this file via ProcessStartInfo.
After that, assign arguments and start the process.
Commandline arguments for convert wav file to mp3 file
---> lame [options] inputfile [outputfile]
Ex: fixed bit rate jstereo 128 kbps encoding:
lame sample.wav sample.mp3
---> options contains bitrate, sampling frequency, and modes
Ex: for bitrate ---> -b n (n = 32,40,48,56,64,80,96,112,128,144,160)
for sampling frequency ---> --resample n (n=16,22.05,24)
Ex: for modes
---> -m m mono
-m s stereo
-m j joint stereo
-m f forced mid/side stereo
-m d dual (independent) channels
-m i intensity stereo
-m a auto
So double click on Convert Button & write below code in ConvertButton click event

if (OpenWavFilesDialog.FileNames.Length > 0 && SaveMP3FolderBrowserDialog.SelectedPath != "")
{
progressBar1.Visible = true;
BrowseButton.Enabled = false;
SaveAsButton.Enabled = false;
BitrateComboBox.Enabled = false;
SamplingrateComboBox.Enabled = false;
ConvertButton.Enabled = false;
try
{
progressBar1.Maximum = OpenWavFilesDialog.FileNames.Length;
foreach (string fn in OpenWavFilesDialog.FileNames)
{

ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = Application.StartupPath + @"\lame.exe";
psi.Arguments = "-b" + BitrateComboBox.SelectedItem.ToString() + " --resample " + SamplingrateComboBox.SelectedItem.ToString() + " -m j " +
"\"" + fn + "\"" + " " +
"\"" + SaveFilesPath.Text + "\\" + Path.GetFileNameWithoutExtension(fn)+".mp3" + "\"";
Process p = Process.Start(psi);
p.Close();
p.Dispose();
progressBar1.Value = progressBar1.Value + 1;
Thread.Sleep(1000);
if (progressBar1.Value == progressBar1.Maximum)
{

MessageBox.Show(OpenWavFilesDialog.FileNames.Length + " File(s) Convertd Successfully", "Success");
progressBar1.Visible = false;
progressBar1.Value = 0;
TotalFiles.Clear();
BrowseButton.Enabled = true;
SaveFilesPath.Clear();
SaveAsButton.Enabled = true;
BitrateComboBox.Enabled = true;
SamplingrateComboBox.Enabled = true;
ConvertButton.Enabled = true;
OpenWavFilesDialog.Reset();
SaveMP3FolderBrowserDialog.Reset();
}
}

}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

}

Total Code


Here is the total code for this application

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace WavToMP3
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

private void BrowseButton_Click(object sender, EventArgs e)
{
if (OpenWavFilesDialog.ShowDialog() == DialogResult.OK)
{
TotalFiles.Text = "Total Files = "+OpenWavFilesDialog.FileNames.Length.ToString();
}
}

private void SaveAsButton_Click(object sender, EventArgs e)
{
if (SaveMP3FolderBrowserDialog.ShowDialog() == DialogResult.OK)
{
SaveFilesPath.Text=SaveMP3FolderBrowserDialog.SelectedPath.ToString();
}
}

private void ConvertButton_Click(object sender, EventArgs e)
{
if (OpenWavFilesDialog.FileNames.Length > 0 && SaveMP3FolderBrowserDialog.SelectedPath != "")
{
progressBar1.Visible = true;
BrowseButton.Enabled = false;
SaveAsButton.Enabled = false;
BitrateComboBox.Enabled = false;
SamplingrateComboBox.Enabled = false;
ConvertButton.Enabled = false;
try
{
progressBar1.Maximum = OpenWavFilesDialog.FileNames.Length;
foreach (string fn in OpenWavFilesDialog.FileNames)
{

ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = Application.StartupPath + @"\lame.exe";
psi.Arguments = "-b" + BitrateComboBox.SelectedItem.ToString() + " --resample " + SamplingrateComboBox.SelectedItem.ToString() + " -m j " +
"\"" + fn + "\"" + " " +
"\"" + SaveFilesPath.Text + "\\" + Path.GetFileNameWithoutExtension(fn)+".mp3" + "\"";
Process p = Process.Start(psi);
p.Close();
p.Dispose();
progressBar1.Value = progressBar1.Value + 1;
Thread.Sleep(1000);
if (progressBar1.Value == progressBar1.Maximum)
{

MessageBox.Show(OpenWavFilesDialog.FileNames.Length + " File(s) Convertd Successfully", "Success");
progressBar1.Visible = false;
progressBar1.Value = 0;
TotalFiles.Clear();
BrowseButton.Enabled = true;
SaveFilesPath.Clear();
SaveAsButton.Enabled = true;
BitrateComboBox.Enabled = true;
SamplingrateComboBox.Enabled = true;
ConvertButton.Enabled = true;
OpenWavFilesDialog.Reset();
SaveMP3FolderBrowserDialog.Reset();
}
}

}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

}

}

private void MainForm_Load(object sender, EventArgs e)
{
BitrateComboBox.SelectedIndex = 8;
SamplingrateComboBox.SelectedIndex = 1;
}
}
}


Attachments

  • WAV to MP3 converter in C#.NET (43883-04010-WAV-MP3-converter-C.NET.rar)
  • Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: