📄 mp3libce.cpp
字号:
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, 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;
}
// This is an example of an exported function.
MP3LIBCE_API void Init(int Equalizer,char* eq)
{
//MessageBox(GetActiveWindow(),_T("In"),_T(""),MB_OK);
if (begin)
{
mad_stream_init(&stream);
mad_frame_init(&frame);
mad_synth_init(&synth);
currentpos=0;
framepos=0;
begin=1;
seeking=1;
}
equalizer=Equalizer;
clipped=0;
attenuation=MAD_F_ONE;
if (eq)
{
set_eq(1,eq,eq[10]);
}
}
// This is an example of an exported function.
MP3LIBCE_API void Exit(void)
{
mad_frame_finish(&frame);
mad_stream_finish(&stream);
mad_synth_finish(&synth);
begin=1;
}
MP3LIBCE_API void SeekStart()
{
Exit();
}
MP3LIBCE_API void SeekEnd()
{
Init(equalizer,0);
}
static
struct id3_tag *get_id3(const unsigned char *stream, id3_length_t tagsize,
struct input *input)
{
struct id3_tag *tag = 0;
id3_byte_t const *data;
data = stream;
// mad_stream_skip(stream, tagsize);
tag = id3_tag_parse(data, tagsize);
return tag;
}
int tagsize=0;
int tagleft=0;
unsigned char* id3_buff=0;
MP3LIBCE_API int GetHeaderInfo(unsigned char * inbuff, int insize, int* Freq, int* ch, int* BitRate, LPTSTR Title, LPTSTR Artist, LPTSTR Album)
{
resample=0;
mad_stream_buffer(&stream, (const unsigned char *)inbuff, insize);
while (mad_frame_decode(&frame, &stream) == -1)
{
if (stream.error == MAD_ERROR_LOSTSYNC)
{
if (!tagsize)
{
tagleft=tagsize = id3_tag_query(stream.this_frame,stream.bufend - stream.this_frame);
}
if (tagleft > 0)
{
struct id3_tag *tag;
if (tagleft>stream.bufend - stream.this_frame)
{
if (!id3_buff) id3_buff=(unsigned char*)malloc(tagsize);
memcpy(id3_buff+(tagsize-tagleft),stream.this_frame,stream.bufend - stream.this_frame);
tagleft-=stream.bufend - stream.this_frame;
return 32768;
}
else if (id3_buff)
{
memcpy(id3_buff+(tagsize-tagleft),stream.this_frame,tagleft);
tagleft=0;
}
tagleft=0;
if (id3_buff)
{
tag = get_id3(id3_buff, tagsize, NULL);
free(id3_buff);
}
else
tag = get_id3(stream.this_frame, tagsize, NULL);
if (tag)
{
unsigned int i;
struct id3_frame const *frame;
id3_ucs4_t const *ucs4;
id3_latin1_t *latin1;
char const spaces[] = " ";
struct {
char const *id;
char const *name;
} const info[] = {
{ ID3_FRAME_TITLE, "Title" },
{ ID3_FRAME_ARTIST, "Artist" },
{ ID3_FRAME_ALBUM, "Album" },
};
/* text information */
for (i = 0; i < sizeof(info) / sizeof(info[0]); ++i) {
union id3_field const *field;
unsigned int nstrings, namelen, j;
char const *name;
frame = id3_tag_findframe(tag, info[i].id, 0);
if (frame == 0)
continue;
field = &frame->fields[1];
nstrings = id3_field_getnstrings(field);
name = info[i].name;
namelen = name ? strlen(name) : 0;
for (j = 0; j < nstrings; ++j) {
ucs4 = id3_field_getstrings(field, j);
if (strcmp(info[i].id, ID3_FRAME_GENRE) == 0)
ucs4 = id3_genre_name(ucs4);
latin1 = id3_ucs4_latin1duplicate(ucs4);
if (latin1 == 0)
continue;
if (i==0)
{
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,(char*)latin1,-1,Title,200);
}
else if (i==1)
{
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,(char*)latin1,-1,Artist,200);
}
else
{
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,(char*)latin1,-1,Album,200);
}
free(latin1);
}
}
id3_tag_delete(tag);
}
}
}
else if (stream.error == MAD_ERROR_BUFLEN)
{
framepos=(int)(stream.this_frame-stream.buffer);
if (stream.bufend - stream.this_frame==insize)
return 32768;
else if (stream.bufend - stream.this_frame)
return stream.bufend - stream.this_frame;
else
return 32768;
}
else //if (stream.error == MAD_ERROR_BADFRAMELEN)
{
framepos=(int)(stream.this_frame-stream.buffer);
return 32768;
}
}
if (!*Freq)
{
*Freq=frame.header.samplerate;
}
else
{
resample=1;
resample_rate=*Freq;
}
*BitRate=frame.header.bitrate;
*ch=(frame.header.mode > 0) ? 2 : 1;
Exit();
Init(equalizer,0);
return 0;
}
MP3LIBCE_API int DecompressAudio(unsigned char * inbuff, int insize, char *outmemory, int outmemsize, int *done, int* inputpos)
{
//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(&stream, (const unsigned char *)inbuff, insize);
currentpos+=framepos;
begin=0;
}
else if (begin)
{
*done=0;
return 32768;
}
int nch;
int output_length=0;
mad_fixed_t clipping=0;
int err=mad_frame_decode(&frame, &stream);
if (err==-1)
{
if (stream.error == MAD_ERROR_BUFLEN)
{
framepos=(int)(stream.this_frame-stream.buffer);
*inputpos=currentpos+framepos;
*done=0;
if (stream.bufend - stream.this_frame==insize)
return 32768;
else if (stream.bufend - stream.this_frame)
return stream.bufend - stream.this_frame;
else
return 32768;
}
else if (stream.error == MAD_ERROR_BADFRAMELEN)
{
framepos=(int)(stream.this_frame-stream.buffer);
*inputpos=currentpos+framepos;
*done=0;
return 32768;
}
else //if((stream.error ==MAD_ERROR_LOSTSYNC||(stream.error==MAD_ERROR_BADDATAPTR)))
{
stream.sync=0;
framepos=(int)(stream.this_frame-stream.buffer);
*inputpos=currentpos+framepos;
*done=0;
return 0;
}
// else
// {
// return -1;
// }
}
seeking=0;
framepos=(int)(stream.this_frame-stream.buffer);
*inputpos=currentpos+framepos;
if (equalizer)
{
attenuate_filter(&frame,attenuation);
equalizer_filter(&frame,eqfactor);
}
mad_synth_frame(&synth,&frame);
nch= synth.pcm.channels;
ch1 = synth.pcm.samples[0];
ch2 = synth.pcm.samples[1];
if (resample)
{
int t;
rch1=(long*)malloc(sizeof(ch1[0])*(synth.pcm.length));
resample_init(&rs,synth.pcm.samplerate,resample_rate);
t=resample_block(&rs,synth.pcm.length,ch1,rch1);
if (nch == 1)
{
rch2 = 0;
}
else
{
rch2=(long*)malloc(sizeof(ch1[1])*(synth.pcm.length));
resample_init(&rs,48000,44100);
t=resample_block(&rs,synth.pcm.length,ch2,rch2);
}
synth.pcm.length=t;
*done=pack_pcm(((unsigned char *)outmemory),
synth.pcm.length, rch1, rch2, resolution, &clipped,&clipping);
free(rch1);
if (rch2)
free(rch2);
if (equalizer)
{
attenuation =
mad_f_tofixed(mad_f_todouble(attenuation) /
mad_f_todouble(MAD_F_ONE +
mad_f_mul(clipping,
conf_attsensitivity)));
}
return 0;
}
if (nch == 1)
ch2 = 0;
*done=pack_pcm(((unsigned char *)outmemory),
synth.pcm.length, ch1, ch2, resolution, &clipped,&clipping);
if (equalizer)
{
attenuation =
mad_f_tofixed(mad_f_todouble(attenuation) /
mad_f_todouble(MAD_F_ONE +
mad_f_mul(clipping,
conf_attsensitivity)));
}
return 0;
}
// This is the constructor of a class that has been exported.
// see MP3LibCE.h for the class definition
CMP3LibCE::CMP3LibCE()
{
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -