VS2010 audio file compression call Lame_enc.dll convert WAV format to MP3

Source: Internet
Author: User

/*

My_lame.h

*/

#pragma once
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "BladeMP3EncDLL.h"
Class My_lame
{
Public
My_lame (void);
~my_lame (void);

Hinstancehdll;
Beinitstreambeinitstream;
Beencodechunkbeencodechunk;
Bedeinitstreambedeinitstream;
Beclosestreambeclosestream;
Beversionbeversion;
Bewritevbrheaderbewritevbrheader;
Bewriteinfotagbewriteinfotag;
int Km_convertmp3 ();
};

/*

My_lame.cpp

/*

#include "StdAfx.h"
#include "my_lame.h"
#include <string>
using namespace Std;
My_lame::my_lame (void)
{
Hdll=null;
Beinitstream=null;
Beencodechunk=null;
Bedeinitstream=null;
Beclosestream=null;
Beversion=null;
Bewritevbrheader=null;
Bewriteinfotag=null;
hDLL = LoadLibrary ("Lame_enc.dll");

}


My_lame::~my_lame (void)
{
FreeLibrary (hDLL);

}


int My_lame::km_convertmp3 ()
{

File*pfilein=null;
File*pfileout=null;
Be_versionversion={0,};
Be_configbeconfig={0,};

char* strfilein1= "F:\\1.wav";
char* strfileout1= "F:\\1.mp3";

dworddwsamples=0;
dworddwmp3buffer=0;
hbe_streamhbestream=0;
be_errerr=0;

Pbytepmp3buffer=null;
Pshortpwavbuffer=null;
The Load Lame_enc.dll Library (make sure though, you set the
Project/settings/debug working Directory correctly, otherwhise the DLL can ' t be loaded

hDLL = LoadLibrary ("Lame_enc.dll");

if (NULL = = hDLL)
{
hDLL = LoadLibrary ("Lame_enc.dll");
}

if (NULL = = hDLL)
{
fprintf (stderr, "Error loading Lame_enc. DLL ");
return-1;
}

Get Interface functions from the DLL
Beinitstream= (Beinitstream) GetProcAddress (hDLL, Text_beinitstream);
beencodechunk= (Beencodechunk) GetProcAddress (hDLL, text_beencodechunk);
Bedeinitstream= (Bedeinitstream) GetProcAddress (hDLL, Text_bedeinitstream);
Beclosestream= (Beclosestream) GetProcAddress (hDLL, Text_beclosestream);
beversion= (beversion) GetProcAddress (hDLL, text_beversion);
Bewritevbrheader= (Bewritevbrheader) GetProcAddress (Hdll,text_bewritevbrheader);
Bewriteinfotag = (Bewriteinfotag) GetProcAddress (Hdll,text_bewriteinfotag);

Check If all interfaces is present
if (!beinitstream | |!beencodechunk | |!bedeinitstream | |!beclosestream | |!beversion | |!bewritevbrheader)
{
printf ("Unable to get LAME interfaces");
return-1;
}

Get the version number
Beversion (&version);

printf
"Lame_enc.dll version%u.%0 2u (%u/%u/%u) \ n "
"Lame_enc Engine%u.%0 2u\n "
"Lame_enc homepage at%s\n\n",
Version.bydllmajorversion, Version.bydllminorversion,
Version.byday, Version.bymonth, Version.wyear,
Version.bymajorversion, Version.byminorversion,
Version.zhomepage);

Try to open the WAV file, being sure to open it as a binary file!

Pfilein = fopen (strFileIn1, "RB");

Check File Open Result
if (Pfilein = = NULL)
{
return-1;
}

Open MP3 File
pfileout= fopen (STRFILEOUT1, "wb+");

Check File Open Result
if (pfileout = = NULL)
{
fprintf (stderr, "Error creating file%s", STRFILEOUT1);
return-1;
}

memset (&beconfig,0,sizeof (Beconfig));//Clear all fields

Use the LAME config structure
Beconfig.dwconfig = Be_config_lame;

This is the default settings for Testcase.wav
Beconfig.format.lhv1.dwstructversion= 1;
beconfig.format.lhv1.dwstructsize= sizeof (beconfig);
beconfig.format.lhv1.dwsamplerate= 44100;//INPUT FREQUENCY
beconfig.format.lhv1.dwresamplerate= 0;//DON "T resample
Beconfig.format.lhv1.nmode= be_mp3_mode_jstereo;//OUTPUT in Streo
beconfig.format.lhv1.dwbitrate= 128;//MINIMUM BIT Rate
beconfig.format.lhv1.npreset= lqp_r3mix;//Quality PRESET SETTING
beconfig.format.lhv1.dwmpegversion= mpeg1;//MPEG VERSION (I or II)
beconfig.format.lhv1.dwpsymodel= 0;//use DEFAULT psychoacoustic MODEL
beconfig.format.lhv1.dwemphasis= 0;//NO emphasis turned on
beconfig.format.lhv1.boriginal= true;//SET ORIGINAL FLAG
beconfig.format.lhv1.bwritevbrheader= true;//Write INFO Tag

beconfig.format.lhv1.bnores= true;//No Bit resorvoir

Init the MP3 Stream
Err = Beinitstream (&beconfig, &dwsamples, &dwmp3buffer, &hbestream);

Check result
if (err! = be_err_successful)
{
fprintf (stderr, "Error opening encoding Stream (%lu)", err);
return-1;
}

Allocate MP3 Buffer
Pmp3buffer = new Byte[dwmp3buffer];

Allocate WAV Buffer
Pwavbuffer = new Short[dwsamples];

Check if Buffer is allocated properly
if (!pmp3buffer | |!pwavbuffer)
{
printf ("Out of Memory");
return-1;
}

DWORD dwread=0;
DWORD dwwrite=0;
DWORD dwdone=0;
DWORD dwfilesize=0;

Seek to end of file
Fseek (Pfilein,0,seek_end);

Get the file size
Dwfilesize=ftell (Pfilein);

Seek back to start of WAV file,
But skip the first to bytes, since that ' s the WAV header
Fseek (Pfilein,44,seek_set);


Convert All PCM samples
while ((Dwread=fread (pwavbuffer,sizeof (short), Dwsamples,pfilein)) >0)
{
Encode samples
Err = Beencodechunk (Hbestream, Dwread, Pwavbuffer, Pmp3buffer, &dwwrite);

Check result
if (err! = be_err_successful)
{
Beclosestream (Hbestream);
fprintf (stderr, "Beencodechunk () failed (%lu)", err);
return-1;
}

Write Dwwrite bytes That is returned in Tehe pmp3buffer to disk
if (fwrite (pmp3buffer,1,dwwrite,pfileout)! = dwwrite)
{
fprintf (stderr, "Output file write error");
return-1;
}

Dwdone + = dwread*sizeof (short);

printf ("Done:%0.2f%% \ r", * * (float) dwdone/(float) (dwfilesize));
}

Deinit the Stream
Err = Bedeinitstream (Hbestream, Pmp3buffer, &dwwrite);

Check result
if (err! = be_err_successful)
{

Beclosestream (Hbestream);
fprintf (stderr, "Beexitstream failed (%lu)", err);
return-1;
}

Is there any bytes returned from the DeInit?
If So, write them to disk
if (dwwrite)
{
if (Fwrite (Pmp3buffer, 1, dwwrite, pfileout)! = dwwrite)
{
fprintf (stderr, "Output file write error");
return-1;
}
}

Close the MP3 Stream
Beclosestream (Hbestream);

Delete WAV Buffer
delete [] pwavbuffer;

Delete MP3 Buffer
delete [] pmp3buffer;

Close input File
Fclose (Pfilein);

Close output File
Fclose (pfileout);

if (Bewriteinfotag)
{
Write the INFO Tag
Change
Bewriteinfotag (Hbestream, strfileout);
Bewriteinfotag (Hbestream, STRFILEOUT1);
}
Else
{

Change
Bewritevbrheader (strfileout);
Bewritevbrheader (STRFILEOUT1);
}

return 0;
}


int _tmain (int argc, _tchar* argv[])
{
My_lame lame;
Lame.km_convertmp3 ();

return 0;
}

Then add the DLL file, the corresponding settings, you can.

VS2010 audio file compression call Lame_enc.dll convert WAV format to MP3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.