📄 common.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(layr, bRate) /* convert bitrate in kbps to index */int layr; /* 1 or 2 */int bRate; /* legal rates from 32 to 448 */{int index = 0;int found = 0; while(!found && index<15) { if(bitrate[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) /* convert samp frq in Hz to index */long sRate; /* legal rates 32000, 44100, 48000 */{ if(sRate == 44100L) return(0); else if(sRate == 48000L) return(1); else if(sRate == 32000L) 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 convert between the Apple SANE extended floating point format* and the IEEE double precision floating point format. These routines are* called from within the Audio Interchange File Format (AIFF) routines.******************************************************************************//**** Apple's 80-bit SANE extended has the following format: 1 15 1 63+-+-------------+-+-----------------------------+|s| e |i| f |+-+-------------+-+-----------------------------+ msb lsb msb lsbThe value v of the number is determined by these fields as follows:If 0 <= e < 32767, then v = (-1)^s * 2^(e-16383) * (i.f).If e == 32767 and f == 0, then v = (-1)^s * (infinity), regardless of i.If e == 32767 and f != 0, then v is a NaN, regardless of i.*** IEEE Draft Standard 754 Double Precision has the following format:MSB+-+---------+-----------------------------+|1| 11 Bits | 52 Bits |+-+---------+-----------------------------+ ^ ^ ^ | | | Sign Exponent Mantissa*//******************************************************************************* double_to_extended()** Purpose: Convert from IEEE double precision format to SANE extended* format.** Passed: Pointer to the double precision number and a pointer to what* will hold the Apple SANE extended format value.** Outputs: The SANE extended format pointer will be filled with the* converted value.** Returned: Nothing.******************************************************************************/void double_to_extended(pd, ps)double *pd;char ps[10];{#ifdef MACINTOSH x96tox80(pd, (extended *) ps);#elseregister unsigned long top2bits;register unsigned short *ps2;register IEEE_DBL *p_dbl;register SANE_EXT *p_ext; p_dbl = (IEEE_DBL *) pd; p_ext = (SANE_EXT *) ps; top2bits = p_dbl->hi & 0xc0000000; p_ext->l1 = ((p_dbl->hi >> 4) & 0x3ff0000) | top2bits; p_ext->l1 |= ((p_dbl->hi >> 5) & 0x7fff) | 0x8000; p_ext->l2 = (p_dbl->hi << 27) & 0xf8000000; p_ext->l2 |= ((p_dbl->lo >> 5) & 0x07ffffff); ps2 = (unsigned short *) & (p_dbl->lo); ps2++; p_ext->s1 = (*ps2 << 11) & 0xf800;#endif}/******************************************************************************* extended_to_double()** Purpose: Convert from SANE extended format to IEEE double precision* format.** Passed: Pointer to the Apple SANE extended format value and a pointer* to what will hold the the IEEE double precision number.** Outputs: The IEEE double precision format pointer will be filled with* the converted value.** Returned: Nothing.******************************************************************************/void extended_to_double(ps, pd)char ps[10];double *pd;{#ifdef MACINTOSH x80tox96((extended *) ps, pd);#elseregister unsigned long top2bits;register IEEE_DBL *p_dbl;register SANE_EXT *p_ext; p_dbl = (IEEE_DBL *) pd; p_ext = (SANE_EXT *) ps; top2bits = p_ext->l1 & 0xc0000000; p_dbl->hi = ((p_ext->l1 << 4) & 0x3ff00000) | top2bits; p_dbl->hi |= (p_ext->l1 << 5) & 0xffff0; p_dbl->hi |= (p_ext->l2 >> 27) & 0x1f; p_dbl->lo = (p_ext->l2 << 5) & 0xffffffe0; p_dbl->lo |= (unsigned long) ((p_ext->s1 >> 11) & 0x1f);#endif}/******************************************************************************* Read Audio Interchange File Format (AIFF) headers.******************************************************************************/int aiff_read_headers(file_ptr, aiff_ptr)FILE *file_ptr;IFF_AIFF *aiff_ptr;{register char i;register long seek_offset;register long sound_position;char temp_sampleRate[10];ChunkHeader Header;Chunk FormChunk;CommonChunk CommChunk;SoundDataChunk SndDChunk; if (fseek(file_ptr, 0, SEEK_SET) != 0) return(-1); if (fread(&FormChunk, sizeof(Chunk), 1, file_ptr) != 1) return(-1);#ifdef IFF_LONG if (*(unsigned long *) FormChunk.ckID != IFF_ID_FORM || *(unsigned long *) FormChunk.formType != IFF_ID_AIFF) return(-1);#else if (strncmp(FormChunk.ckID,IFF_ID_FORM,4) || strncmp(FormChunk.formType,IFF_ID_AIFF,4)) return(-1);#endif /* * chunks need not be in any particular order */ while (fread(&Header, sizeof(ChunkHeader), 1, file_ptr) == 1) {#ifdef IFF_LONG if (*(unsigned long *)Header.ckID == IFF_ID_COMM) {#else if (strncmp(Header.ckID,IFF_ID_COMM,4) == 0) {#endif /* * read comm chunk */ if (fread(&CommChunk.numChannels, sizeof(short), 1, file_ptr) != 1) return(-1); if (fread(&CommChunk.numSampleFrames, sizeof(unsigned long), 1, file_ptr) != 1) return(-1); if (fread(&CommChunk.sampleSize, sizeof(short), 1, file_ptr) != 1) return(-1); if (fread(CommChunk.sampleRate, sizeof(char[10]), 1, file_ptr) != 1) return(-1); for (i = 0; i < sizeof(char[10]); i++) temp_sampleRate[i] = CommChunk.sampleRate[i]; extended_to_double(temp_sampleRate, &aiff_ptr->sampleRate); aiff_ptr->numChannels = CommChunk.numChannels; aiff_ptr->numSampleFrames = CommChunk.numSampleFrames; aiff_ptr->sampleSize = CommChunk.sampleSize;#ifdef IFF_LONG } else if (*(unsigned long *)Header.ckID == IFF_ID_SSND) {#else } else if (strncmp(Header.ckID,IFF_ID_SSND,4) == 0) {#endif /* * read ssnd chunk */ if (fread(&SndDChunk.offset, sizeof(long), 1, file_ptr) != 1) return(-1); if (fread(&SndDChunk.blockSize, sizeof(long), 1, file_ptr) != 1) return(-1); aiff_ptr->blkAlgn.offset = SndDChunk.offset; aiff_ptr->blkAlgn.blockSize = SndDChunk.blockSize; aiff_ptr->sampleType = *(unsigned long *)Header.ckID; /* * record position of sound data */ sound_position = ftell(file_ptr); /* * skip over sound data to look at remaining chunks */ seek_offset = Header.ckSize - sizeof(SoundDataChunk) + sizeof(ChunkHeader); if (fseek(file_ptr, seek_offset, SEEK_CUR) != 0) return(-1); } else { /* * skip unknown chunk */ seek_offset = Header.ckSize; if (fseek(file_ptr, seek_offset, SEEK_CUR) != 0) return(-1); } } return(sound_position);}/******************************************************************************* Seek past some Audio Interchange File Format (AIFF) headers to sound data.******************************************************************************/int aiff_seek_to_sound_data(file_ptr)FILE *file_ptr;{ if (fseek(file_ptr, sizeof(Chunk) + sizeof(SoundDataChunk), SEEK_SET) != 0) return(-1); return(0);}/********************************************************************************* Write Audio Interchange File Format (AIFF) headers.********************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -