📄 common.c
字号:
void WriteScale(bit_alloc, scfsi, scalar, fr_ps, s)
unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT], scalar[2][3][SBLIMIT];
frame_params *fr_ps;
FILE *s;
{
int stereo = fr_ps->stereo;
int sblimit = fr_ps->sblimit;
int lay = fr_ps->header->lay;
int i,j,k;
if(lay == 2) {
fprintf(s, "SFSI ");
for (i=0;i<sblimit;i++) for (k=0;k<stereo;k++)
if (bit_alloc[k][i]) fprintf(s,"%d",scfsi[k][i]);
fprintf(s, "\nSCFs ");
for (k=0;k<stereo;k++) {
for (i=0;i<sblimit;i++)
if (bit_alloc[k][i])
switch (scfsi[k][i]) {
case 0: for (j=0;j<3;j++)
fprintf(s,"%2d%c",scalar[k][j][i],
(j==2)?';':'-');
break;
case 1:
case 3: fprintf(s,"%2d-",scalar[k][0][i]);
fprintf(s,"%2d;",scalar[k][2][i]);
break;
case 2: fprintf(s,"%2d;",scalar[k][0][i]);
}
fprintf(s, "\n");
}
}
else{ /* lay == 1 */
fprintf(s, "SCFs ");
for (i=0;i<sblimit;i++) for (k=0;k<stereo;k++)
if (bit_alloc[k][i]) fprintf(s,"%2d;",scalar[k][0][i]);
fprintf(s, "\n");
}
}
void WriteSamples(ch, sample, bit_alloc, fr_ps, s)
int ch;
unsigned int FAR sample[SBLIMIT];
unsigned int bit_alloc[SBLIMIT];
frame_params *fr_ps;
FILE *s;
{
int i;
int stereo = fr_ps->stereo;
int sblimit = fr_ps->sblimit;
fprintf(s, "SMPL ");
for (i=0;i<sblimit;i++)
if ( bit_alloc[i] != 0)
fprintf(s, "%d:", sample[i]);
if(ch==(stereo-1) ) fprintf(s, "\n");
else fprintf(s, "\t");
}
int NumericQ(s) /* see if a string lookd like a numeric argument */
char *s;
{
char c;
while( (c = *s++)!='\0' && isspace((int)c)) /* strip leading ws */
;
if( c == '+' || c == '-' )
c = *s++; /* perhaps skip leading + or - */
return isdigit((int)c);
}
int BitrateIndex(ver,layr, bRate) /* convert bitrate in kbps to index */
int layr,ver; /* 1 or 2 */
int bRate; /* legal rates from 32 to 448 */
{
int index = 0;
int found = 0;
while(!found && index<15) {
if(bitrate[ver][layr-1][index] == bRate)
found = 1;
else
++index;
}
if(found)
return(index);
else {
fprintf(stderr, "BitrateIndex: %d (layer %d) is not a legal bitrate\n",
bRate, layr);
return(-1); /* Error! */
}
}
int SmpFrqIndex(sRate, version) /* convert samp frq in Hz to index */
long sRate; /* legal rates 16000, 22050, 24000, 32000, 44100, 48000 */
int *version;
{
if (sRate == 44100L) {
*version = MPEG_AUDIO_ID; return(0);
}
else if (sRate == 48000L) {
*version = MPEG_AUDIO_ID; return(1);
}
else if (sRate == 32000L) {
*version = MPEG_AUDIO_ID; return(2);
}
else if (sRate == 24000L) {
*version = MPEG_PHASE2_LSF; return(1);
}
else if (sRate == 22050L) {
*version = MPEG_PHASE2_LSF; return(0);
}
else if (sRate == 16000L) {
*version = MPEG_PHASE2_LSF; return(2);
}
else {
fprintf(stderr, "SmpFrqIndex: %ld is not a legal sample rate\n", sRate);
return(-1); /* Error! */
}
}
/*******************************************************************************
*
* Allocate number of bytes of memory equal to "block".
*
*******************************************************************************/
void FAR *mem_alloc(block, item)
unsigned long block;
char *item;
{
void *ptr;
#ifdef MACINTOSH
ptr = NewPtr(block);
#endif
#ifdef MSC60
/*ptr = (void FAR *) _fmalloc((unsigned int)block);*/ /* far memory, 92-07-08 sr */
ptr = (void FAR *) malloc((unsigned int)block); /* far memory, 93-08-24 ss */
#endif
#if ! defined (MACINTOSH) && ! defined (MSC60)
ptr = (void FAR *) malloc(block);
#endif
if (ptr != NULL){
#ifdef MSC60
_fmemset(ptr, 0, (unsigned int)block); /* far memory, 92-07-08 sr */
#else
memset(ptr, 0, block);
#endif
}
else{
printf("Unable to allocate %s\n", item);
exit(0);
}
return(ptr);
}
/****************************************************************************
*
* Free memory pointed to by "*ptr_addr".
*
*****************************************************************************/
void mem_free(ptr_addr)
void **ptr_addr;
{
if (*ptr_addr != NULL){
#ifdef MACINTOSH
DisposPtr(*ptr_addr);
#else
free(*ptr_addr);
#endif
*ptr_addr = NULL;
}
}
/*******************************************************************************
*
* Check block of memory all equal to a single byte, else return FALSE
*
*******************************************************************************/
int memcheck(array, test, num)
char *array;
int test; /* but only tested as a char (bottom 8 bits) */
int num;
{
int i=0;
while (array[i] == test && i<num) i++;
if (i==num) return TRUE;
else return FALSE;
}
/*****************************************************************************
*
* Routines to determine byte order and swap bytes
*
*****************************************************************************/
enum byte_order NativeByteOrder = order_unknown;
enum byte_order DetermineByteOrder()
{
char s[ sizeof(long) + 1 ];
union
{
long longval;
char charval[ sizeof(long) ];
} probe;
probe.longval = 0x41424344L; /* ABCD in ASCII */
strncpy( s, probe.charval, sizeof(long) );
s[ sizeof(long) ] = '\0';
/* fprintf( stderr, "byte order is %s\n", s ); */
if ( strcmp(s, "ABCD") == 0 )
return order_bigEndian;
else
if ( strcmp(s, "DCBA") == 0 )
return order_littleEndian;
else
return order_unknown;
}
void SwapBytesInWords( short *loc, int words )
{
int i;
short thisval;
char *dst, *src;
src = (char *) &thisval;
for ( i = 0; i < words; i++ )
{
thisval = *loc;
dst = (char *) loc++;
dst[0] = src[1];
dst[1] = src[0];
}
}
/*****************************************************************************
*
* Read Audio Interchange File Format (AIFF) headers.
*
*****************************************************************************/
int aiff_read_headers( FILE *file_ptr, IFF_AIFF *aiff_ptr )
{
int i, chunkSize, subSize, sound_position;
if ( fseek(file_ptr, 0, SEEK_SET) != 0 )
return -1;
if ( Read32BitsHighLow(file_ptr) != IFF_ID_FORM )
return -1;
chunkSize = Read32BitsHighLow( file_ptr );
if ( Read32BitsHighLow(file_ptr) != IFF_ID_AIFF )
return -1;
sound_position = 0;
while ( chunkSize > 0 )
{
chunkSize -= 4;
switch ( Read32BitsHighLow(file_ptr) )
{
case IFF_ID_COMM:
chunkSize -= subSize = Read32BitsHighLow( file_ptr );
aiff_ptr->numChannels = Read16BitsHighLow( file_ptr );
subSize -= 2;
aiff_ptr->numSampleFrames = Read32BitsHighLow( file_ptr );
subSize -= 4;
aiff_ptr->sampleSize = Read16BitsHighLow( file_ptr );
subSize -= 2;
aiff_ptr->sampleRate = ReadIeeeExtendedHighLow( file_ptr );
subSize -= 10;
while ( subSize > 0 )
{
getc( file_ptr );
subSize -= 1;
}
break;
case IFF_ID_SSND:
chunkSize -= subSize = Read32BitsHighLow( file_ptr );
aiff_ptr->blkAlgn.offset = Read32BitsHighLow( file_ptr );
subSize -= 4;
aiff_ptr->blkAlgn.blockSize = Read32BitsHighLow( file_ptr );
subSize -= 4;
sound_position = ftell( file_ptr ) + aiff_ptr->blkAlgn.offset;
if ( fseek(file_ptr, (long) subSize, SEEK_CUR) != 0 )
return -1;
aiff_ptr->sampleType = IFF_ID_SSND;
break;
default:
chunkSize -= subSize = Read32BitsHighLow( file_ptr );
while ( subSize > 0 )
{
getc( file_ptr );
subSize -= 1;
}
break;
}
}
return sound_position;
}
/*****************************************************************************
*
* Seek past some Audio Interchange File Format (AIFF) headers to sound data.
*
*****************************************************************************/
int aiff_seek_to_sound_data( FILE *file_ptr )
{
if ( fseek(file_ptr, AIFF_FORM_HEADER_SIZE + AIFF_SSND_HEADER_SIZE, SEEK_SET) != 0 )
return(-1);
return(0);
}
/*******************************************************************************
*
* Write Audio Interchange File Format (AIFF) headers.
*
*******************************************************************************/
int aiff_write_headers( FILE *file_ptr, IFF_AIFF *aiff_ptr )
{
int chunkSize;
int sampleBytes = (aiff_ptr->sampleSize / 8) + (aiff_ptr->sampleSize % 8 ? 1 : 0);
if ( fseek(file_ptr, 0L, SEEK_SET) != 0 )
return -1;
/* write FORM chunk */
chunkSize = 8 + 18 + 8 + aiff_ptr->numChannels * aiff_ptr->numSampleFrames * sampleBytes;
Write32BitsHighLow( file_ptr, IFF_ID_FORM );
Write32BitsHighLow( file_ptr, chunkSize );
Write32BitsHighLow( file_ptr, IFF_ID_AIFF );
/* write COMM chunk */
Write32BitsHighLow( file_ptr, IFF_ID_COMM );
Write32BitsHighLow( file_ptr, 18 ); /* chunk size */
Write16BitsHighLow( file_ptr, aiff_ptr->numChannels );
Write32BitsHighLow( file_ptr, aiff_ptr->numSampleFrames );
Write16BitsHighLow( file_ptr, aiff_ptr->sampleSize );
WriteIeeeExtendedHighLow( file_ptr, aiff_ptr->sampleRate );
/* write SSND chunk header */
chunkSize = 8 + aiff_ptr->numChannels * aiff_ptr->numSampleFrames * sampleBytes;
Write32BitsHighLow( file_ptr, IFF_ID_SSND );
Write32BitsHighLow( file_ptr, chunkSize );
Write32BitsHighLow( file_ptr, 0 ); /* offset */
Write32BitsHighLow( file_ptr, 0 ); /* block size */
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -