0
0

hallo,

I try to modity the recordtodisk example so that I can encode the chunks
to mp3.
The code below encodes but the mp3 length is only a half of
the wav length. :( the mp3 output it plays to fast.
what should I do to syncronize the chunks with the lame encoder?
Please help me.
I have hundreds of variations of this code – not one encodes correct to mp3. I am down …
The encoding runs in a thread and the wav output is very very correct.

void CRipper::RundEncoderThread()
{
WriteWavHeader(fp, soundx, datalength);
soundx->getLength(&soundlength, FMOD_TIMEUNIT_PCM);
do
{
static unsigned int lastrecordpos = 0;
unsigned int recordpos = 0;

    pMain1X->system->getRecordPosition(&recordpos);
    if (recordpos != lastrecordpos)        
    {
        void *ptr1, *ptr2;
        int blocklength;
        unsigned int len1, len2;

        blocklength = (int)recordpos - (int)lastrecordpos;
        if (blocklength < 0) { blocklength += soundlength; }

        //Lock the sound to get access to the raw data.
        soundx->lock(lastrecordpos * 4, blocklength * 4, &ptr1, &ptr2, &len1, &len2);   /* *4 = stereo 16bit.  1 sample = 4 bytes. */

            if (ptr1 && len1)
            {
            datalength+=  fwrite(ptr1, 1, len1, fp);
            memcpy( pWAVBuffer, ptr1, dwSamples );
            dwRead = dwSamples/2;
            err = beEncodeChunk(hbeStream, dwRead , pWAVBuffer, pMP3Buffer, &dwWrite);
            fwrite(pMP3Buffer, 1, dwWrite, pFileOut);
            }

        soundx->unlock(ptr1, ptr2, len1, len2);
    }
    lastrecordpos = recordpos;
    pMain1X->system->update();
    Sleep(5);
} while( isPlaying );

}

  • You must to post comments
0
0

Hello brett,

thank you for reply.

[quote:2q46jlbn]
Have you checked that locking and writing from a thread to .wav (or even .raw) instead of .mp3 works ok?
[/quote:2q46jlbn]

Yes, the writing to .wav is ok. it records exact to 1/100 second.
Writing .wav and writing .mp3 in the same if-block is not a problem.
with both operation the length of the .wav output is exact too.
i think it is not a performance problem.

I think there is a problem with the size of chunks the lame encoder wants and what is given by soundx->lock(…)

============================================

I tried to encode with this variation : (!!! PSEUDO-CODE !!!)
[code:2q46jlbn]
do
{

pMain1X->system->getRecordPosition(&recordpos);
if (recordpos != lastrecordpos)
{
void *ptr1, *ptr2;
int blocklength;
unsigned int len1, len2;

//Lock the sound to get access to the raw data.
soundx->lock(pos, dwSamples , &ptr1, &ptr2, &len1, &len2); //<<<—- new
if (ptr1 && len1)
{
datalength+= fwrite(ptr1, 1, len1, fp);
memcpy( pWAVBuffer, ptr1, dwSamples );
dwRead = dwSamples/2;
err = beEncodeChunk(hbeStream, dwRead , pWAVBuffer, pMP3Buffer, &dwWrite);
fwrite(pMP3Buffer, 1, dwWrite, pFileOut);
}

soundx->unlock(ptr1, ptr2, len1, len2);
}
pos += dwSamples; //<<<—- new
lastrecordpos = recordpos;
pMain1X->system->update();
Sleep(5);
} while( isPlaying );
[/code:2q46jlbn]

The results are exact what I want. The .mp3 and the .wav output have the same length. But the recording of both outputs stops after [u:2q46jlbn]2[/u:2q46jlbn] seconds. ( Ok, I have to build in the rocordposiotion )
That shows me that the thread ist not my problem.
My problem is: [u:2q46jlbn]How make the recording/encoding longer than 2 seconds? better: to find a logic that permanent encodes only the size of one dwSamples per while-loop.[/u:2q46jlbn]

My intention is to encode -without the .mp3 plugin- from a source out of
my application i.e the sound of a tv-program.

I hope you can help me.

best regards

  • You must to post comments
0
0

Hello,

[quote:37mtwwy4]
set the recorder to loop?
[/quote:37mtwwy4]

I dont know what you mean.
I use the recordtodisc example. this delievers the chunks without any loop mode – i cant find that anyhow ?!?

This here writes .wav and .mp3 longer than 2 seconds
but now the .mp3 is again a half of the .wav’s length
There is just a little problem with the wav: at the beginning is a piece of 1,5 sec with no singnals and the sound sets out for about 1/4 sec irregularly, but longer
fragments of 4-5 sec are super ok.

[code:37mtwwy4]
void CRipper::RundEncoderThread()
{
WriteWavHeader(fp, soundx, datalength);
soundx->getLength(&soundlength, FMOD_TIMEUNIT_PCM);
unsigned int recordpos = 0;
DWORD pos = 0;
do
{
void *ptr1, *ptr2;
unsigned int len1, len2;

        //Lock the sound to get access to the raw data.
        soundx-&gt;lock(pos , dwSamples, &amp;ptr1, &amp;ptr2, &amp;len1, &amp;len2);   /* *4 = stereo 16bit.  1 sample = 4 bytes. */
            if (ptr1 &amp;&amp; len1)
            {
            datalength+=  fwrite(ptr1, 1, len1, fp);
            memcpy( pWAVBuffer, ptr1, dwSamples );
            dwRead = dwSamples/2;
            err = beEncodeChunk(hbeStream, dwRead , pWAVBuffer, pMP3Buffer, &amp;dwWrite);
            fwrite(pMP3Buffer, 1, dwWrite, pFileOut);
            }
        pos += dwSamples;
        soundx-&gt;unlock(ptr1, ptr2, len1, len2);
} while( isPlaying );

}
[/code:37mtwwy4]

At the time I use Update() or a Sleep() after unlock() the recording stops
after 2 seconds, the size of the .wav is then 235 kB – not one byte more!
Do you know why?

I think I need a trick to syncronize the record speed or buffer or what ever with my while loop.

Thanks for your patience

best regard

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.