📄 mp3lib.cpp
字号:
unsigned int nch, ch, ns, s, sb;
nch = MAD_NCHANNELS(&frame->header);
ns = MAD_NSBSAMPLES(&frame->header);
for (ch = 0; ch < nch; ++ch) {
for (s = 0; s < ns; ++s) {
for (sb = 0; sb < 32; ++sb) {
frame->sbsample[ch][s][sb] =
mad_f_mul(frame->sbsample[ch][s][sb], scalefactor);
}
}
}
}
static
void equalizer_filter(struct mad_frame *frame, mad_fixed_t eqfactor[32])
{
unsigned int nch, ch, ns, s, sb;
nch = MAD_NCHANNELS(&frame->header);
ns = MAD_NSBSAMPLES(&frame->header);
for (ch = 0; ch < nch; ++ch) {
for (s = 0; s < ns; ++s) {
for (sb = 0; sb < 32; ++sb) {
frame->sbsample[ch][s][sb] =
mad_f_mul(frame->sbsample[ch][s][sb], eqfactor[sb]);
}
}
}
}
/*
static __inline
signed long linear_dither(unsigned int bits, mad_fixed_t sample,
mad_fixed_t *error)
{
mad_fixed_t quantized, check;
sample += *error;
quantized = sample;
check = (sample >> MAD_F_FRACBITS) + 1;
if (check & ~1) {
if (sample >= MAD_F_ONE) {
quantized = MAD_F_ONE - 1;
}
else if (sample < -MAD_F_ONE) {
quantized = -MAD_F_ONE;
}
}
quantized &= ~((1L << (MAD_F_FRACBITS + 1 - bits)) - 1);
*error = sample - quantized;
return quantized >> (MAD_F_FRACBITS + 1 - bits);
}
*/
static inline
signed long linear_dither(unsigned int bits, mad_fixed_t sample,
mad_fixed_t *error, unsigned long *clipped,
mad_fixed_t *clipping)
{
mad_fixed_t quantized, check;
/* dither */
sample += *error;
/* clip */
quantized = sample;
check = (sample >> MAD_F_FRACBITS) + 1;
if (check & ~1) {
if (sample >= MAD_F_ONE) {
quantized = MAD_F_ONE - 1;
++*clipped;
if (sample - quantized > *clipping &&
mad_f_abs(*error) < (MAD_F_ONE >> (MAD_F_FRACBITS + 1 - bits)))
*clipping = sample - quantized;
}
else if (sample < -MAD_F_ONE) {
quantized = -MAD_F_ONE;
++*clipped;
if (quantized - sample > *clipping &&
mad_f_abs(*error) < (MAD_F_ONE >> (MAD_F_FRACBITS + 1 - bits)))
*clipping = quantized - sample;
}
}
/* quantize */
quantized &= ~((1L << (MAD_F_FRACBITS + 1 - bits)) - 1);
/* error */
*error = sample - quantized;
/* scale */
return quantized >> (MAD_F_FRACBITS + 1 - bits);
}
static
unsigned int pack_pcm(unsigned char *data, unsigned int nsamples,
mad_fixed_t const *left, mad_fixed_t const *right,
int resolution, unsigned long *clipped,
mad_fixed_t *clipping)
{
static mad_fixed_t left_err, right_err;
unsigned char const *start;
register signed long sample0, sample1;
//int effective;
//int bytes;
start = data;
// effective = resolution;//(resolution > 24) ? 24 : resolution;
// bytes = resolution >>3;
if (right) { /* stereo */
while (nsamples--) {
sample0 = linear_dither(16, *left++, &left_err, clipped,clipping);
sample1 = linear_dither(16, *right++, &right_err, clipped,clipping);
data[0] = sample0 >> 0;
data[1] = sample0 >> 8;
data[2] = sample1 >> 0;
data[3] = sample1 >> 8;
data += 4; //bytes << 1;
}
}
else { /* mono */
while (nsamples--)
{
sample0 = linear_dither(16, *left++, &left_err, clipped,clipping);
data[1] = sample0 >> 8;
data[0] = sample0 >> 0;
data += 2;
}
}
return data - start;
}
MP3LIB_API void mp3_lib_create_instance(void **pp)
{
if(pp != NULL && *pp == NULL)
*pp = new Mp3libState();
}
MP3LIB_API void mp3_lib_release_instance(void *vps)
{
Mp3libState *ps = (Mp3libState*) vps;
delete ps;
}
// This is an example of an exported function.
MP3LIB_API void mp3_lib_init(void *vps, int Equalizer,char* eq)
{
//MessageBox(GetActiveWindow(),_T("In"),_T(""),MB_OK);
Mp3libState *ps = (Mp3libState*) vps;
if (ps->begin)
{
mad_stream_init(&ps->stream);
mad_frame_init(&ps->frame);
mad_synth_init(&ps->synth);
ps->currentpos=0;
ps->framepos=0;
ps->begin=1;
ps->seeking=1;
}
ps->equalizer=Equalizer;
ps->clipped=0;
ps->attenuation=MAD_F_ONE;
if (eq)
{
set_eq(1,eq,eq[10]);
}
}
// This is an example of an exported function.
MP3LIB_API void mp3_lib_finalize(void *vps)
{
Mp3libState *ps = (Mp3libState*) vps;
mad_frame_finish(&ps->frame);
mad_stream_finish(&ps->stream);
mad_synth_finish(&ps->synth);
ps->begin = 1;
}
//static char buffer[40000];
MP3LIB_API int mp3_lib_decode_header(void *vps, unsigned char * inbuff, int insize, int* Freq, int* ch, int* BitRate)
{
Mp3libState *ps = (Mp3libState*) vps;
ps->resample=0;
mad_stream_buffer(&ps->stream, (const unsigned char *)inbuff, insize);
while (mad_frame_decode(&ps->frame, &ps->stream) == -1);
if (!*Freq)
{
*Freq=ps->frame.header.samplerate;
}
else
{
ps->resample=1;
ps->resample_rate=*Freq;
}
*BitRate=ps->frame.header.bitrate;
*ch=(ps->frame.header.mode > 0) ? 2 : 1;
mp3_lib_finalize(vps);
mp3_lib_init(vps, ps->equalizer,0);
return 1;
}
MP3LIB_API int mp3_lib_decode_buffer(void *vps,unsigned char * inbuff, int insize, char *outmemory, int outmemsize, int *done, int* inputpos)
{
Mp3libState *ps = (Mp3libState*) vps;
int retval=0;
resample_state rs;
mad_fixed_t const *ch1, *ch2;
mad_fixed_t *rch1, *rch2;
int resolution=16;
// unsigned long *clipped;
// mad_fixed_t *clipping;
if (inbuff)
{
// int remainder = stream.bufend - stream.this_frame;
// memcpy(buffer, stream.this_frame, remainder);
// memcpy(buffer+remainder,inbuff,insize+remainder);
mad_stream_buffer(&ps->stream, (const unsigned char *)inbuff, insize);
ps->currentpos+=ps->framepos;
ps->begin=0;
}
else if (ps->begin)
{
*done=0;
return 1;
}
int nch;
int output_length=0;
mad_fixed_t clipping=0;
int err=mad_frame_decode(&ps->frame, &ps->stream);
if (err==-1)
{
if (ps->stream.error == MAD_ERROR_BUFLEN)
{
ps->framepos=(int)(ps->stream.this_frame-ps->stream.buffer);
*inputpos=ps->currentpos+ps->framepos;
*done=0;
return ps->stream.bufend - ps->stream.this_frame+1;
// retval=stream.bufend - stream.this_frame+1;
}
else //if((stream.error ==MAD_ERROR_LOSTSYNC||(stream.error==MAD_ERROR_BADDATAPTR)))
{
ps->stream.sync=0;
ps->framepos=(int)(ps->stream.this_frame-ps->stream.buffer);
*inputpos=ps->currentpos+ps->framepos;
*done=0;
return 0;
}
// else
// {
// return -1;
// }
}
ps->seeking=0;
ps->framepos=(int)(ps->stream.this_frame-ps->stream.buffer);
*inputpos=ps->currentpos+ps->framepos;
if (ps->equalizer)
{
attenuate_filter(&ps->frame,ps->attenuation);
equalizer_filter(&ps->frame,eqfactor);
}
mad_synth_frame(&ps->synth,&ps->frame);
nch= ps->synth.pcm.channels;
ch1 = ps->synth.pcm.samples[0];
ch2 = ps->synth.pcm.samples[1];
if (ps->resample)
{
int t;
rch1=(long*)malloc(sizeof(ch1[0])*(ps->synth.pcm.length));
resample_init(&rs,ps->synth.pcm.samplerate,ps->resample_rate);
t=resample_block(&rs,ps->synth.pcm.length,ch1,rch1);
if (nch == 1)
{
rch2 = 0;
}
else
{
rch2=(long*)malloc(sizeof(ch1[1])*(ps->synth.pcm.length));
resample_init(&rs,48000,44100);
t=resample_block(&rs,ps->synth.pcm.length,ch2,rch2);
}
ps->synth.pcm.length=t;
*done=pack_pcm(((unsigned char *)outmemory),
ps->synth.pcm.length, rch1, rch2, resolution, &ps->clipped,&clipping);
free(rch1);
if (rch2)
free(rch2);
if (ps->equalizer)
{
ps->attenuation =
mad_f_tofixed(mad_f_todouble(ps->attenuation) /
mad_f_todouble(MAD_F_ONE +
mad_f_mul(clipping,
conf_attsensitivity)));
}
return retval;
}
if (nch == 1)
ch2 = 0;
*done=pack_pcm(((unsigned char *)outmemory),
ps->synth.pcm.length, ch1, ch2, resolution, &ps->clipped,&clipping);
if (ps->equalizer)
{
ps->attenuation =
mad_f_tofixed(mad_f_todouble(ps->attenuation) /
mad_f_todouble(MAD_F_ONE +
mad_f_mul(clipping,
conf_attsensitivity)));
}
return retval;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -