ACM conversion PCM to MP3

Discussion about everything. New games, 3d math, development tips...
Post Reply
VooDooMilivoje
Posts: 7
Joined: Fri May 20, 2005 11:09 am
Location: Serbia&Montenegro

ACM conversion PCM to MP3

Post by VooDooMilivoje »

Hi everybody!
I am trying to convert Wav(PCM) format to MP3,but i have some problems on doing that.The thing is that I have got some code that I found on the net,which converts MP3 to PCM and it is ok,but I am a beginner in this area and I don’t know how to make it work in the oposit way(PCM ->MP3)
I need to know if the following code is a good approach to this problem?

So,can anybody go through the following lines and give me answer to the several questions?

.
.
// input format
LPWAVEFORMATEX waveFormat = (LPWAVEFORMATEX) LocalAlloc( LPTR, maxFormatSize );
waveFormat->wFormatTag = WAVE_FORMAT_PCM;
waveFormat->nChannels = 2; // stereo
waveFormat->nSamplesPerSec = 44100; // 44.1kHz
waveFormat->wBitsPerSample = 16; // 16 bits
waveFormat->nBlockAlign = 4;
waveFormat->nAvgBytesPerSec = 4 * 44100; // byte-rate
waveFormat->cbSize = 0; <- think this is ok

// define MP3 output format
LPMPEGLAYER3WAVEFORMAT mp3format = (LPMPEGLAYER3WAVEFORMAT) LocalAlloc( LPTR, maxFormatSize );
mp3format->wfx.cbSize = MPEGLAYER3_WFX_EXTRA_BYTES;
mp3format->wfx.wFormatTag = WAVE_FORMAT_MPEGLAYER3;
mp3format->wfx.nChannels = 2;
mp3format->wfx.nAvgBytesPerSec = 16000;
mp3format->wfx.wBitsPerSample = 0;
mp3format->wfx.nBlockAlign = 1;
mp3format->wfx.nSamplesPerSec = 44100; // 44.1kHz
mp3format->fdwFlags = MPEGLAYER3_FLAG_PADDING_OFF;
mp3format->nBlockSize = MP3_BLOCK_SIZE; // 417
mp3format->nFramesPerBlock = 1; // MUST BE ONE
mp3format->nCodecDelay = 0; // 0 for LAME or 1393 for fraunh..
mp3format->wID = MPEGLAYER3_ID_MPEG; <-thik this is ok
.
.

mmr = acmStreamOpen( &g_mp3stream,
NULL,
waveFormat, // from wav
LPWAVEFORMATEX) mp3format, //to mp3
NULL,
0,
0,
0
); //<- I guess this is ok too…
.
.
// open PCM and MP3 files
FILE *fpIn = fopen( SOURCE_PCM, "rb" );
FILE *fpOut = fopen( OUTPUT_MP3, "wb" );


First question: don’t know how to use acmStreamSize to get pcmbufsize( size of the PCM buffer )
mmr = acmStreamSize( g_mp3stream, MP3_BLOCK_SIZE, &pcmbufsize, ACM_STREAMSIZEF_DESTINATION );//<-this don’t work!
Do I need to use ACM_STREAMSIZEF_SOURCE?

// allocate our I/O buffers
LPBYTE mp3buf = (LPBYTE) LocalAlloc( LPTR, MP3_BLOCK_SIZE );
LPBYTE pcmbuf = (LPBYTE) LocalAlloc( LPTR, pcmbufsize );

Second question: is this ok?
// prepare the decoder
ACMSTREAMHEADER mp3streamHead;
ZeroMemory( &mp3streamHead, sizeof(ACMSTREAMHEADER ) );
mp3streamHead.cbStruct = sizeof(ACMSTREAMHEADER );
mp3streamHead.pbSrc = pcmbuf;
mp3streamHead.cbSrcLength = pcmbufsize;
mp3streamHead.pbDst = mp3buf;
mp3streamHead.cbDstLength = MP3_BLOCK_SIZE;
mmr = acmStreamPrepareHeader( g_mp3stream, &mp3streamHead, 0 );
assert( mmr == 0 );

Third question: is this loop good approach anyway?
while(1) {
// suck in some PCM data
int count = fread( pcmbuf, 1, pcmbufsize, fpIn );

if( count != pcmbufsize )
break;

// convert the data
mmr = acmStreamConvert( g_mp3stream, &mp3streamHead, ACM_STREAMCONVERTF_BLOCKALIGN );

// write MP3 to disc
count = fwrite( mp3buf, 1, mp3streamHead.cbDstLengthUsed, fpOut);
};

thanks in advance…
afecelis
Admin
Posts: 3075
Joined: Sun Feb 22, 2004 10:44 pm
Location: Colombia
Contact:

Post by afecelis »

use audacity; free audio editor:
http://audacity.sourceforge.net/

get the lame encoder plugin to export to mp3 here:
http://www.dll-files.com/dllindex/dll-f ... l?lame_enc
VooDooMilivoje
Posts: 7
Joined: Fri May 20, 2005 11:09 am
Location: Serbia&Montenegro

Post by VooDooMilivoje »

thanks for hint, i have solved the problem :wink:
Post Reply