|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionAumplib (fully qualified name: BackgroundAumplib was designed to provide a clean, easy, and powerful audio conversion interface in C# to prominent Open Source audio conversion libraries (which were written in C/C++). These libraries have existed on the Internet for some time, but were not readily usable by C# programmers because the data structures were not clearly mapped or they were not in DLL form. Aumplib "wraps" these libraries using P/Invoke and offers various objects that simplify using them. Using the codeAumplib is made up of various "wrapper" classes that communicate with the third-party DLLs, and TestForm.cs defines a class called Three private variables are declared that are important further on in the code. These are private Aumpel audioConverter = new Aumpel();
private Aumpel.soundFormat inputFileFormat;
private Aumpel.soundFormat outputFileFormat;
As we scan further, we reach the definitions of the delegate functions. These are crucial to the operation of Aumplib. The delegates do two things:
If you want MP3 decoding capabilities in your class, you will have to define at least two delegates (the MP3 decoding delegate takes slightly different parameters). // Conversion callback (lame,libsndfile)
private static void
ReportStatus(int totalBytes,
int processedBytes, Aumpel aumpelObj)
{
progressBar1.Value = (int)(((float)processedBytes/(float)totalBytes)*100);
}
The first delegate, // Decoding callback (madlldlib)
private static bool
ReportStatusMad(uint frameCount,
uint byteCount, ref MadlldlibWrapper.mad_header mh)
{
progressBar1.Value = (int)(((float)byteCount/(float)soundFileSize)*100);
return true;
}
The second delegate we declare, Next, we come upon the event handlers of the form. First, we define the handlers for protected void
convertButton_Click (object sender, System.EventArgs e)
{
// Set conversion type
switch((string)comboBox1.SelectedItem)
{
case "WAV":
outputFileFormat = Aumpel.soundFormat.WAV;
break;
case "MP3":
outputFileFormat = Aumpel.soundFormat.MP3;
break;
case "AU":
outputFileFormat = Aumpel.soundFormat.AU;
break;
case "AIFF":
outputFileFormat = Aumpel.soundFormat.AIFF;
break;
default:
MessageBox.Show("You must select a type to convert to.",
"Error", MessageBoxButtons.OK);
return;
}
...
First, ...
// Convert to MP3
if ( (int)outputFileFormat == (int)Aumpel.soundFormat.MP3 )
{
try
{
Aumpel.Reporter defaultCallback = new Aumpel.Reporter(ReportStatus);
audioConverter.Convert(inputFile,
(int)inputFileFormat, outputFile,
(int)outputFileFormat, defaultCallback);
progressBar1.Value = 0;
destFileLabel.Text = outputFile = "";
sourceFileLabel.Text = inputFile = "";
MessageBox.Show("Conversion finished.", "Done.", MessageBoxButtons.OK);
}
catch (Exception ex)
{
ShowExceptionMsg(ex);
return;
}
}
...
Using ...
// From MP3:
else if ( (int)inputFileFormat == (int)Aumpel.soundFormat.MP3 )
{
try
{
MadlldlibWrapper.Callback defaultCallback =
new MadlldlibWrapper.Callback(ReportStatusMad);
// Determine file size
FileInfo fi = new FileInfo(inputFile);
soundFileSize = (int)fi.Length;
audioConverter.Convert(inputFile,
outputFile, outputFileFormat, defaultCallback);
progressBar1.Value = 0;
destFileLabel.Text = outputFile = "";
sourceFileLabel.Text = inputFile = "";
MessageBox.Show("Conversion finished.", "Done.", MessageBoxButtons.OK);
}
catch (Exception ex)
{
ShowExceptionMsg(ex);
return;
}
}
...
If you are decoding from MP3 to WAV, the above code executes. Notice that where the MP3 encoding block defines ...
// Non-MP3 soundfile conversion:
else
{
try
{
Aumpel.Reporter defaultCallback = new Aumpel.Reporter(ReportStatus);
audioConverter.Convert(inputFile,
(int)inputFileFormat,
outputFile,
(int)(outputFileFormat | Aumpel.soundFormat.PCM_16),
defaultCallback);
progressBar1.Value = 0;
destFileLabel.Text = outputFile = "";
sourceFileLabel.Text = inputFile = "";
MessageBox.Show("Conversion finished.", "Done.", MessageBoxButtons.OK);
}
catch (Exception ex)
{
ShowExceptionMsg(ex);
return;
}
}
}
If neither conversion to MP3 nor from MP3 is specified, then a non-MP3 conversion is attempted. Again, an overload of the That's basically it. When the "Convert" button is pressed, the above code begins to fire, and if everything is set appropriately, the conversion will take place, showing you its progress via the progress bar. Notes
History
|
||||||||||||||||||||||