#418 lame+libsndfile reads and decodes input twice

open
nobody
None
5
2012-01-18
2012-01-18
lvqcl
No

LAME now calls "sf_command(gs_pSndFileIn, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE)" but it seems that libsndfile reads and decodes an input file during this call (to determine its peak value). Then LAME reads the input file again to encode it to MP3.

IMHO it is better to remove this call and maybe replace sf_read_int(...) with the following:

float insampflt[2 * 1152];
...
...
samples_read = sf_read_float(global.snd_file, insampflt, num_channels * samples_to_read);
for(i=0; i<samples_read; i++)
{
float x = insampflt[i] * ((float)INT_MAX + 1.0);
if (x > (float)INT_MAX) x = (float)INT_MAX;
else if (x < (float)INT_MIN) x = (float)INT_MIN;
insamp[i] = (int)x;
}

Usually it will result in double conversion (int->float->int) but it is better than reading+decoding an input file twice.

Discussion

  • I could be wrong, but IIRC SFC_SET_SCALE_FLOAT_INT_READ looks out for a peak sample riff chunk first. Your code assumes proper +-1 scaling of floating point data, which isn't guaranteed.