0
0

Hi to all,
I really hope to get help as I having problems encoding raw samples with Lame and I until now I didn’t solve.
My scenario is this:
1) I load a mp3 file (STEREO – 16bits)
2) I generate the waveform
3) I select 2 points in the waveform and I get the position in the sound of the 2 points. So I have point1 and point2.
4) Now I would encode the raw audio samples between point2 and point1 in a new mp3 file. So I have (point2 – point1) samples to be encoded. Well I can’t.
I post here the relevant code:

Some settings and initializations:
[code:2pup2r99] // this are 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.dwSampleRate = 22050; // INPUT FREQUENCY
beConfig.format.LHV1.dwReSampleRate = 0; // DON’T RESAMPLE
beConfig.format.LHV1.nMode = BE_MP3_MODE_STEREO; // OUTPUT IN STEREO
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;

// Init the MP3 Stream - This function is the first to call before starting an encoding stream
err = beInitStream(&beConfig, &dwSamples, &dwMP3Buffer, &hbeStream);

// Check result
if(err != BE_ERR_SUCCESSFUL)
{
    fprintf(stderr,"Error opening encoding stream (%lu)\n", err);
    return -1;
}

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

// Check if Buffer are allocated properly
if(!pMP3Buffer )
{
    printf("Out of memory");
    return -1;
}

[/code:2pup2r99]

More settings:
[code:2pup2r99]
DWORD dwWrite=0;
DWORD dwDone=0;
DWORD dwFileSize=0;

//start conversion
uint start = m_WaveDisplay->m_ListaMarcatori.first(); //start point
uint stop = m_WaveDisplay->m_ListaMarcatori.last(); //end point

size_t totSamples = stop – start; //samples to be compressed

short* dst = new short[totSamples];
short* src = (short*)m_wave->getSoundStream(); //original sound buffer
memcpy(dst, src + start, totSamples ); // I copy the interested part
// now in dst I should have the buffer to convert

bool done = false;
uint temp = 0;
uint recordPos = 0;[/code:2pup2r99]

Convert the buffer
[code:2pup2r99]
while( 1 ) // I would parse all buffer
{
temp = recordPos + dwSamples;
if (temp < (int)totSamples)
{
// Encode samples
err = beEncodeChunk(hbeStream, dwSamples, dst + recordPos, pMP3Buffer, &dwWrite);
// Check result
if(err != BE_ERR_SUCCESSFUL)
{
beCloseStream(hbeStream);
fprintf(stderr,"beEncodeChunk() failed (%lu)", err);
return -1;
}

        // write dwWrite bytes that are returned in the pMP3Buffer to disk
        if( fwrite( pMP3Buffer, 1, dwWrite, pFileOut ) != dwWrite )
        {
            fprintf(stderr,&quot;Output file write error&quot;);
            return -1;
        }
        // update pos
        recordPos = recordPos + dwSamples;
        temp = 0;
    }
    else
    {
        done = true;
        break;
    }
}

[/code:2pup2r99]

I check if there are remaining samples to be converted

[code:2pup2r99] if( done == true )
{
// there are remaining data in the buffer
uint rimamenti = 0;
rimamenti = totSamples – recordPos;

    // Encode samples
    err = beEncodeChunk(hbeStream, rimamenti, dst + recordPos, pMP3Buffer, &amp;dwWrite);
    // Check result
    if(err != BE_ERR_SUCCESSFUL)
    {
        beCloseStream(hbeStream);
        fprintf(stderr,&quot;beEncodeChunk() failed (%lu)&quot;, err);
        return -1;
    }

    // write dwWrite bytes that are returned in the pMP3Buffer to disk
    if(fwrite(pMP3Buffer,1,dwWrite,pFileOut) != dwWrite)
    {
        fprintf(stderr,&quot;Output file write error&quot;);
        return -1;
    }

}[/code:2pup2r99]

And finally:

[code:2pup2r99] // Deinit the stream
err = beDeinitStream(hbeStream, pMP3Buffer, &dwWrite);
// Check result
if(err != BE_ERR_SUCCESSFUL)
{

    beCloseStream(hbeStream);
    fprintf(stderr,&quot;beExitStream failed (%lu)&quot;, err);
    return -1;
}
// Are there any bytes returned from the DeInit call?
// If so, write them to disk
if( dwWrite )
{
    if( fwrite( pMP3Buffer, 1, dwWrite, pFileOut ) != dwWrite )
    {
        fprintf(stderr,&quot;Output file write error&quot;);
        return -1;
    }
}

// close the MP3 Stream
beCloseStream( hbeStream );

// Delete MP3 Buffer
delete [] pMP3Buffer;

// Close output file
fclose( pFileOut );
if ( beWriteInfoTag )
{
    // Write the INFO Tag
    beWriteInfoTag( hbeStream, strFileOut );
}
else
{
    beWriteVBRHeader( strFileOut );
}

delete[] dst;[/code:2pup2r99]

The result is not what I expected. I’m sure I did some mistake with variable type or lenght but I can not find where my code is wrong.
The result is a file with half silence and half sound and the duration of the created file is half of what I expected.

I really need help please.

Best Regards,
Franco

  • You must to post comments
0
0

Hi Franco,

You have already started a forum thread about this: http://52.88.2.202/forum/viewtopic.php?t=12528
We would prefer it if you didn’t start multiple threads for a single issue.

As I said in email, I would advise you to contact whoever supports the lame encoder.

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.