This uses FAAC to implement AAC encoding. In addition, the data segment of WAV is PCM, the code will appear many PCM abbreviations. 1 Download and install FAAC
The installation process here is implemented on MAC and Linux, and windows can be similar to the reference.
wget http://downloads.sourceforge.net/faac/faac-1.28.tar.gz
tar zxvf faac-1.28.tar.gz
cd faac-1.28
. Configure
make
sudo make install
If prefix path is used in the default configure, then the installed Lib and. h files are used in/usr/local/lib and/usr/local/include, and later when compiling. 2 FAAC API 2.1 Open FAAC engine Prototype:
Faacenchandle Faacencopen //Returns a FAAC handle
(
unsigned long nsamplerate, //sample rate in bps
unsigned long nchannels, //channel, 1 mono, 2 for dual channel
unsigned long &ninputsamples, //pass reference, Get the original data length that should be received each time the encoding is called
unsigned long &nmaxoutputbytes //Pass reference, get the maximum length of AAC data generated each time the encoding is called
;
2.2 get/set Encoding configuration
Prototype:
To obtain the configuration of the encoder:
Faacencconfigurationptr faacencgetcurrentconfiguration//Get a pointer to the current encoder configuration
(
faacenchandle hencoder // FAAC of handle
);
Set the encoder configuration:
int Faacapi faacencsetconfiguration
(
faacdechandle hdecoder, //FAAC handle previously obtained configuration of faacencconfigurationptr Config//FAAC encoder
);
2.3 Encode
Prototype:
int Faacencencode
(
faacenchandle hencoder, //FAAC handle short
*inputbuffer, //WAV raw data
unsigned int samplesinput, //Ninputsamples value obtained when calling Faacencopen
unsigned char *outputbuffer,// The actual size of the buffer
unsigned int buffersize //OutputBuffer buffer with at least the nmaxoutputbytes byte length obtained when calling Faacencopen
);
2.4 Close FAAC engine
Prototype
void Faacencclose
(
faacenchandle hencoder //FAAC handle previously obtained
);
3 Process
3.1 What to do to prepare.
Sample rate, number of channels (two-channel or mono). And your single sample of WAV is 8-bit or 16-bit. 3.2 Open FAAC Encoder, ready to encode before the call Faacencopen Open FAAC Encoder, a single input sample number ninputsamples and output data maximum bytes nmaxoutputbytes; Based on Ninputsamples and nmaxoutputbytes, the buffers are created for the WAV data and the AAC data to be obtained respectively, the call faacencgetcurrentconfiguration gets the current configuration, and after the configuration is modified, Call Faacencsetconfiguration to set a new configuration. 3.3 Start Coding
Call Faacencencode, the preparation is ready just now, very simple. 3.4 Aftermath
Turn off the encoder, and don't forget to release the buffer, and if you use a file stream, don't forget to close it. 4 test Procedure 4.1 Full Code
/home/michael/development/testspace/in.wav the WAV format audio file to the AAC format file/HOME/MICHAEL/DEVELOPMENT/TESTSPACE/OUT.AAC.
#include <faac.h> #include <stdio.h> typedef unsigned long ULONG;
typedef unsigned int UINT;
typedef unsigned char BYTE;
typedef char _TCHAR; int main (int argc, _tchar* argv[]) {ULONG nsamplerate = 11025; Sample rate UINT nchannels = 1; Channel number UINT npcmbitsize = 16;
Single-sample number of ULONG ninputsamples = 0;
ULONG nmaxoutputbytes = 0;
int nret;
Faacenchandle Hencoder;
Faacencconfigurationptr pconfiguration;
int nbytesread;
int npcmbuffersize;
Byte* Pbpcmbuffer;
Byte* Pbaacbuffer; File* Fpin; WAV file for input file* fpout;
AAC file for Output Fpin = fopen ("/home/michael/development/testspace/in.wav", "RB");
Fpout = fopen ("/HOME/MICHAEL/DEVELOPMENT/TESTSPACE/OUT.AAC", "WB");
(1) Open FAAC engine Hencoder = Faacencopen (Nsamplerate, Nchannels, &ninputsamples, &nmaxoutputbytes); if (Hencoder = = NULL) {printf ("[ERROR] Failed to call Faacencopen () \ n");
return-1;
} npcmbuffersize = Ninputsamples * NPCMBITSIZE/8;
Pbpcmbuffer = new BYTE [npcmbuffersize];
Pbaacbuffer = new BYTE [nmaxoutputbytes];
(2.1) Get current encoding Configuration pconfiguration = Faacencgetcurrentconfiguration (Hencoder);
Pconfiguration->inputformat = Faac_input_16bit;
(2.2) Set encoding Configuration nret = Faacencsetconfiguration (Hencoder, pconfiguration); for (int i = 0; 1; i++) {//read in the actual number of bytes, the maximum will not exceed npcmbuffersize, generally only read to the end of the file is not this value Nbytesread = fread (pbpcmbuf
Fer, 1, npcmbuffersize, Fpin);
Enter the number of samples, with the actual read-in bytes calculated, generally only read the end of the file is not npcmbuffersize/(NPCMBITSIZE/8);
Ninputsamples = Nbytesread/(NPCMBITSIZE/8); (3) Encode nret = Faacencencode (Hencoder, (int*) Pbpcmbuffer, Ninputsamples, Pbaacbuffer, Nmaxoutputby
TES);
Fwrite (Pbaacbuffer, 1, nret, fpout);
printf ("%d:faacencencode returns%d\n", I, nret); if (Nbytesread ≪= 0) {break;
}}//(4) Close FAAC engine nret = Faacencclose (Hencoder);
Delete[] Pbpcmbuffer;
Delete[] Pbaacbuffer;
Fclose (Fpin);
Fclose (fpout);
GetChar ();
return 0;
}
4.2 Compiling and running
Save the above code as a "wav2aac.cpp" file, and then compile:
g++ Wav2aac.cpp-o Wav2aac-l/usr/local/lib-lfaac-i/usr/local/include
Run:
./WAV2AAC
Then you generate the Out.aac file, and listen and see. ~ 5 Reference AUDIOCODING.COM-FAAC Dogfoot–재밌는개발