From: Larry on
"Larry" <dontmewithme(a)got.it> ha scritto nel messaggio
news:4b42809f$0$1112$4fafbaef(a)reader4.news.tin.it...

> All I can do is set up a buffer of 176400 Bytes OR I can always push 30
> buffers of 5880 each! (I love this last one, I do get perfect sound!) Also
> 5880 is really a small chuck.

I would like to move this form reading from a file to reading from that
buffer (5880)

// Init the MP3 Stream
beInitStream(&bec, &dwSamples, &dwMP3Buffer, &hbeStream);

// Allocate MP3 buffer
PBYTE pMP3Buffer = new BYTE[dwMP3Buffer];

// Allocate WAV buffer
PSHORT pWAVBuffer = new SHORT[dwSamples];

// Convert All PCM samples
while ( ( dwRead = fread(pWAVBuffer, sizeof(SHORT), dwSamples, pFileIn)) >
0 )
{
// Encode samples
beEncodeChunk(hbeStream, dwRead, pWAVBuffer, pMP3Buffer, &dwWrite);

// write dwWrite bytes that are returned in tehe pMP3Buffer to disk
fwrite(pMP3Buffer,1,dwWrite,pFileOut) != dwWrite)
}

From: ScottMcP [MVP] on

I don't follow where all your numbers come from, and don't know
anything about the Lame-enc DLL. But you seem to be making a mountain
out of a molehill here.

If the DLL will only accept 2304 samples at a time then why don't you
just make your wave buffer some multiple of this, as large as you
like? Then for each WAV buffer that is completed you would execute a
little loop to call the DLL several times until the entire WAV buffer
has been converted, passing it 2304 samples each time around the loop.

From: Larry on

"ScottMcP [MVP]" <scottmcp(a)mvps.org> ha scritto nel messaggio
news:5aeebde2-54e8-413d-bef7-4d026af223b2(a)g18g2000vbr.googlegroups.com...
>

> If the DLL will only accept 2304 samples at a time then why don't you
> just make your wave buffer some multiple of this, as large as you
> like? Then for each WAV buffer that is completed you would execute a
> little loop to call the DLL several times until the entire WAV buffer
> has been converted, passing it 2304 samples each time around the loop.

I'll be encoding 2304 at a time!

Now, I have problems encoding the data form the WAVEform API (internal mic)
I thought I would try to use the same code I used to encode an mp3 from a
WAVE file as shown below:

***********
// BE_ERR beInitStream( PBE_CONFIG pbeConfig,
PDWORD dwSamples,
PDWORD dwBufferSize,
PHBE_STREAM phbeStream );

// BE_ERR beEncodeChunk( HBE_STREAM hbeStream,
DWORD nSamples,
PSHORT pSamples,
PBYTE pOutput,
PDWORD pdwOutput );

// Init the MP3 Stream
beInitStream(&bec, &dwSamples, &dwMP3Buffer, &hbeStream);

// Allocate MP3 buffer
pMP3Buffer = new BYTE[dwMP3Buffer];

// Allocate WAV buffer
pWAVBuffer = new SHORT[dwSamples];

// dwSamples = 2304, dwRead ritorna 2304

while ( ( dwRead = fread(pWAVBuffer, sizeof(SHORT), dwSamples, pFileIn)) >
0 )
{
// Encode samples
beEncodeChunk(hbeStream, dwRead, pWAVBuffer, pMP3Buffer, &dwWrite);

// Write dwWrite bytes that are returned in tehe pMP3Buffer to disk
fwrite(pMP3Buffer, 1, dwWrite, pFileOut);
}
**********

So I went like this:

**********
// dwSamples is 2304
// buff[k].lpData is 6912 bytes long (LPSTR)
// I'm gonna read it 2304 bytes at a time (3 times)
// then store its value in PSHORT pWAVChunk = new SHORT[dwSamples]
// Loop...
int dwChunkSeek = 0;
int dwLoop = 0;
int k = 0;

while(1)
{
// CALLBACK EVENT
WaitForSingleObject(hevent, INFINITE);

if(buff[k].dwFlags & WHDR_DONE)
{
// Encode chunk by chunk

while(dwLoop < 3)
{
// copy part of the buffer
memcpy(pWAVChunk, buff[k].lpData + dwChunkSeek, 2304);

// Encode samples
beEncodeChunk(hbeStream, 2304, pWAVChunk, pMP3Buffer,&dwWrite);

// Save
hFile.write((LPSTR)pMP3Buffer, dwWrite);

dwChunkSeek += 2304;
++dwLoop;
}

dwLoop = 0;
dwChunkSeek = 0;
waveInAddBuffer(hwi, &buff[k], sizeof(WAVEHDR));
}
}
***********

but I get a bad mp3 file with noise only...

probably I got something wrong here:

memcpy(pWAVChunk, buff[k].lpData + dwChunkSeek, 2304);

or here?

beEncodeChunk(hbeStream, 2304, pWAVChunk, pMP3Buffer,&dwWrite);

thanks

From: ScottMcP [MVP] on
On Jan 8, 7:36 am, "Larry" <dontmewit...(a)got.it> wrote:
> probably I got something wrong here:
>
> memcpy(pWAVChunk, buff[k].lpData + dwChunkSeek, 2304);
> ...
> dwChunkSeek += 2304;

Yea, looks like it.

sizeof(char) is 1
sizeof(short) is 2

lpData is a char*, so adding 2304 increments it by 2304 bytes. But
the data it is really pointing at is an array of shorts. So to
increment lpData by 2304 shorts you need to add 2*2304 = 4608.


From: Larry on

"ScottMcP [MVP]" <scottmcp(a)mvps.org> ha scritto nel messaggio
news:2106d319-4286-4838-9958-9f94a0df1381(a)r26g2000vbi.googlegroups.com...

> lpData is a char*, so adding 2304 increments it by 2304 bytes. But
> the data it is really pointing at is an array of shorts. So to
> ncrement lpData by 2304 shorts you need to add 2*2304 = 4608.

I'm sorry, that is not working either...do you think I should cast? what I
dont get is the following

// dwSamples = 2304;
dwRead = fread(pWAVBuffer, sizeof(SHORT), dwSamples, pFileIn));

basically that is reading sizeof(SHORT)*dwSamples (4608). Yet, dwRead is
2304...

I record the same time a WAVE and MP3 file, this is the main code for my
project:

while(1)
{
if(stop_thread_flag)
break;
// CALLBACK EVENT
WaitForSingleObject(hevent, INFINITE);

if(buff[k].dwFlags & WHDR_DONE && !stop_thread_flag)
{
// Save WAVE
hFile2.write(buff[k].lpData, buff[k].dwBytesRecorded);
dwWAVBufferCounter += buff[k].dwBytesRecorded;

// Encode chunk by chunk (dwLoop = 0)
while(dwLoop <= 3)
{
// copy part of the buffer...PSHORT pWAVChunk = new SHORT[dwSamples]
memcpy(pWAVChunk, buff[k].lpData + dwChunkSeek*sizeof(SHORT), 2304);

// Encode samples
result = beEncodeChunk(hbeStream, 2304, pWAVChunk, pMP3Buffer,
&dwWrite);

//printf("%d -> %d \n", hashChunk[dwLoop], dwWrite);
// Save MP3
hFile.write((LPSTR)pMP3Buffer, dwWrite);
dwChunkSeek += 2304;
dwMP3BufferCounter += dwWrite;
++dwLoop;
}
dwLoop = 0;
dwChunkSeek = 0;
waveInAddBuffer(hwi, &buff[k], sizeof(WAVEHDR));
}
if(k == num_buffers -1)
k = 0;
else
k++;

}

I am really stuck at that :-(