📄 get_audio.c
字号:
} exit(1); } return samples_read;}#else /* defined(LIBSNDFILE) *//************************************************************************ ************************************************************************ ************************************************************************ ************************************************************************ ************************************************************************ ************************************************************************ * * OLD ISO/LAME routines follow. Used if you dont have LIBSNDFILE * or for stdin/stdout support * ************************************************************************ ************************************************************************ ************************************************************************ ************************************************************************ ************************************************************************ ************************************************************************//************************************************************************unpack_read_samples - read and unpack signed low-to-high byte or unsigned single byte input. (used for read_samples function) Output integers are stored in the native byte order (little or big endian). -jd in: samples_to_read bytes_per_sample swap_order - set for high-to-low byte order input stream i/o: pcm_in out: sample_buffer (must be allocated up to samples_to_read upon call)returns: number of samples read*/static intunpack_read_samples( const int samples_to_read, const int bytes_per_sample, const int swap_order, int *sample_buffer, FILE *pcm_in ){ int samples_read; int i; int *op; /* output pointer */ unsigned char *ip = (unsigned char *) sample_buffer; /* input pointer */ const int b = sizeof(int) * 8;#define GA_URS_IFLOOP( ga_urs_bps ) \ if( bytes_per_sample == ga_urs_bps ) \ for( i = samples_read * bytes_per_sample; (i -= bytes_per_sample) >=0;) samples_read = fread( sample_buffer, bytes_per_sample, samples_to_read, pcm_in); op = sample_buffer + samples_read; GA_URS_IFLOOP( 1 ) *--op = (ip[i] ^ 0x80)<<(b-8) | 0x7f<<(b-16);/* convert from unsigned*/ if( swap_order == 0 ) { GA_URS_IFLOOP( 2 ) *--op = ip[i]<<(b-16) | ip[i+1]<<(b-8); GA_URS_IFLOOP( 3 ) *--op = ip[i]<<(b-24) | ip[i+1]<<(b-16) | ip[i+2]<<(b-8); GA_URS_IFLOOP( 4 ) *--op = ip[i]<<(b-32) | ip[i+1]<<(b-24) | ip[i+2]<<(b-16) | ip[i+3] << (b-8); } else { GA_URS_IFLOOP( 2 ) *--op = ip[i]<<(b-8) | ip[i+1]<<(b-16); GA_URS_IFLOOP( 3 ) *--op = ip[i]<<(b-8) | ip[i+1]<<(b-16) | ip[i+2]<<(b-24); GA_URS_IFLOOP( 4 ) *--op = ip[i]<<(b-8) | ip[i+1]<<(b-16) | ip[i+2]<<(b-24) | ip[i+3]<<(b-32); }#undef GA_URS_IFLOOP return( samples_read );}/************************************************************************** read_samples()** PURPOSE: reads the PCM samples from a file to the buffer** SEMANTICS:* Reads #samples_read# number of shorts from #musicin# filepointer* into #sample_buffer[]#. Returns the number of samples read.*************************************************************************/intread_samples_pcm(FILE * musicin, int sample_buffer[2304], int frame_size, int samples_to_read){ int samples_read; int iswav = (input_format == sf_wave); int hi_lo_order; /* byte order of input stream */ if( (32 == pcmbitwidth) || (24 == pcmbitwidth) || (16 == pcmbitwidth) ) { /* assume only recognized wav files are */ /* in little endian byte order */ hi_lo_order = (!iswav == !pcmswapbytes); samples_read = unpack_read_samples(samples_to_read, pcmbitwidth/8, hi_lo_order,sample_buffer, musicin ); } else if( 8 == pcmbitwidth ) { samples_read = unpack_read_samples( samples_to_read, 1, 0, sample_buffer, musicin ); } else { if( silent < 10 ) { fprintf(stderr, "Only 8, 16, 24 and 32 bit input files supported \n"); } exit(1); } if (ferror(musicin)) { if( silent < 10 ) { fprintf(stderr, "Error reading input file\n"); } exit(1); } return samples_read;}/* AIFF Definitions */#define IFF_ID_FORM 0x464f524d /* "FORM" */#define IFF_ID_AIFF 0x41494646 /* "AIFF" */#define IFF_ID_AIFC 0x41494643 /* "AIFC" */#define IFF_ID_COMM 0x434f4d4d /* "COMM" */#define IFF_ID_SSND 0x53534e44 /* "SSND" */#define IFF_ID_MPEG 0x4d504547 /* "MPEG" */#define IFF_ID_NONE 0x4e4f4e45 /* "NONE" */ /* AIFF-C data format */#define IFF_ID_2CBE 0x74776f73 /* "twos" */ /* AIFF-C data format */#define IFF_ID_2CLE 0x736f7774 /* "sowt" */ /* AIFF-C data format */#define WAV_ID_RIFF 0x52494646 /* "RIFF" */#define WAV_ID_WAVE 0x57415645 /* "WAVE" */#define WAV_ID_FMT 0x666d7420 /* "fmt " */#define WAV_ID_DATA 0x64617461 /* "data" *//***************************************************************************** * * Read Microsoft Wave headers * * By the time we get here the first 32-bits of the file have already been * read, and we're pretty sure that we're looking at a WAV file. * *****************************************************************************/static intparse_wave_header(lame_global_flags * gfp, FILE * sf){ int format_tag = 0; int channels = 0; int block_align = 0; int bits_per_sample = 0; int samples_per_sec = 0; int avg_bytes_per_sec = 0; int is_wav = 0; long data_length = 0, file_length, subSize = 0; int loop_sanity = 0; file_length = Read32BitsHighLow(sf); if (Read32BitsHighLow(sf) != WAV_ID_WAVE) return 0; for (loop_sanity = 0; loop_sanity < 20; ++loop_sanity) { int type = Read32BitsHighLow(sf); if (type == WAV_ID_FMT) { subSize = Read32BitsLowHigh(sf); if (subSize < 16) { /*DEBUGF( "'fmt' chunk too short (only %ld bytes)!", subSize); */ return 0; } format_tag = Read16BitsLowHigh(sf); subSize -= 2; channels = Read16BitsLowHigh(sf); subSize -= 2; samples_per_sec = Read32BitsLowHigh(sf); subSize -= 4; avg_bytes_per_sec = Read32BitsLowHigh(sf); subSize -= 4; block_align = Read16BitsLowHigh(sf); subSize -= 2; bits_per_sample = Read16BitsLowHigh(sf); subSize -= 2; /* DEBUGF(" skipping %d bytes\n", subSize); */ if (subSize > 0) { if (fskip(sf, (long) subSize, SEEK_CUR) != 0) return 0; }; } else if (type == WAV_ID_DATA) { subSize = Read32BitsLowHigh(sf); data_length = subSize; is_wav = 1; /* We've found the audio data. Read no further! */ break; } else { subSize = Read32BitsLowHigh(sf); if (fskip(sf, (long) subSize, SEEK_CUR) != 0) return 0; } } if (format_tag != 1) { return 0; /* oh no! non-supported format */ } if (is_wav) { /* make sure the header is sane */ if( -1 == lame_set_num_channels( gfp, channels ) ) { if( silent < 10 ) { fprintf( stderr, "Unsupported number of channels: %ud\n", channels ); } exit( 1 ); } (void) lame_set_in_samplerate( gfp, samples_per_sec ); pcmbitwidth = bits_per_sample; (void) lame_set_num_samples( gfp, data_length / (channels * ((bits_per_sample+7) / 8)) ); } return is_wav;}/************************************************************************* aiff_check2** PURPOSE: Checks AIFF header information to make sure it is valid.* returns 0 on success, 1 on errors************************************************************************/intaiff_check2(const char *file_name, IFF_AIFF * const pcm_aiff_data){ if (pcm_aiff_data->sampleType != IFF_ID_SSND) { if( silent < 10 ) { fprintf(stderr, "Sound data is not PCM in '%s'\n", file_name); } return 1; } if (pcm_aiff_data->sampleSize != sizeof(short) * CHAR_BIT) { if( silent < 10 ) { fprintf(stderr, "Sound data is not %i bits in '%s'\n", sizeof(short) * CHAR_BIT, file_name); } return 1; } if (pcm_aiff_data->numChannels != 1 && pcm_aiff_data->numChannels != 2) { if( silent < 10 ) { fprintf(stderr, "Sound data is not mono or stereo in '%s'\n", file_name); } return 1; } if (pcm_aiff_data->blkAlgn.blockSize != 0) { if( silent < 10 ) { fprintf(stderr, "Block size is not 0 bytes in '%s'\n", file_name); } return 1; } /* A bug, since we correctly skip the offset earlier in the code. if (pcm_aiff_data->blkAlgn.offset != 0) { fprintf(stderr, "Block offset is not 0 bytes in '%s'\n", file_name); return 1; } */ return 0;}/***************************************************************************** * * Read Audio Interchange File Format (AIFF) headers. * * By the time we get here the first 32 bits of the file have already been * read, and we're pretty sure that we're looking at an AIFF file. * *****************************************************************************/static intparse_aiff_header(lame_global_flags * gfp, FILE * sf){ int is_aiff = 0; long chunkSize = 0, subSize = 0, typeID = 0, dataType = 0; IFF_AIFF aiff_info; memset(&aiff_info, 0, sizeof(aiff_info)); chunkSize = Read32BitsHighLow(sf); typeID = Read32BitsHighLow(sf); if ((typeID != IFF_ID_AIFF)&&(typeID != IFF_ID_AIFC)) return 0; while (chunkSize > 0) { int type = Read32BitsHighLow(sf); chunkSize -= 4; /* DEBUGF( "found chunk type %08x '%4.4s'\n", type, (char*)&type); */ /* don't use a switch here to make it easier to use 'break' for SSND */ if (type == IFF_ID_COMM) { subSize = Read32BitsHighLow(sf); chunkSize -= subSize; aiff_info.numChannels = Read16BitsHighLow(sf); subSize -= 2; aiff_info.numSampleFrames = Read32BitsHighLow(sf); subSize -= 4; aiff_info.sampleSize = Read16BitsHighLow(sf); subSize -= 2; aiff_info.sampleRate = ReadIeeeExtendedHighLow(sf); subSize -= 10; if (typeID == IFF_ID_AIFC) { dataType = Read32BitsHighLow(sf); subSize -= 4; if ((dataType != IFF_ID_2CLE) && (dataType != IFF_ID_2CBE) && (dataType != IFF_ID_NONE)) return 0; if (aiff_info.sampleSize == 16) pcmswapbytes = (!swapbytes == (dataType == IFF_ID_2CLE)); } if (fskip(sf, (long) subSize, SEEK_CUR) != 0) return 0; } else if (type == IFF_ID_SSND) { subSize = Read32BitsHighLow(sf); chunkSize -= subSize; aiff_info.blkAlgn.offset = Read32BitsHighLow(sf); subSize -= 4; aiff_info.blkAlgn.blockSize = Read32BitsHighLow(sf); subSize -= 4; if (fskip(sf, (long) aiff_info.blkAlgn.offset, SEEK_CUR) != 0) return 0; aiff_info.sampleType = IFF_ID_SSND; is_aiff = 1; /* We've found the audio data. Read no further! */ break; } else { subSize = Read32BitsHighLow(sf); chunkSize -= subSize; if (fskip(sf, (long) subSize, SEEK_CUR) != 0) return 0; } } /* DEBUGF("Parsed AIFF %d\n", is_aiff); */ if (is_aiff) { /* make sure the header is sane */ if (0 != aiff_check2("name" /*???????????? */ , &aiff_info)) return 0; if( -1 == lame_set_num_channels( gfp, aiff_info.numChannels ) ) { if( silent < 10 ) { fprintf( stderr, "Unsupported number of channels: %ud\n", aiff_info.numChannels ); } exit( 1 ); } (void) lame_set_in_samplerate( gfp, (int)aiff_info.sampleRate ); pcmbitwidth = aiff_info.sampleSize; (void) lame_set_num_samples( gfp, aiff_info.numSampleFrames ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -