📄 get_audio.c
字号:
if (subSize < 16) { /*DEBUGF( "'fmt' chunk too short (only %ld bytes)!", subSize); */ return 0; } wave_info.format_tag = Read16BitsLowHigh(sf); subSize -= 2; wave_info.channels = Read16BitsLowHigh(sf); subSize -= 2; wave_info.samples_per_sec = Read32BitsLowHigh(sf); subSize -= 4; wave_info.avg_bytes_per_sec = Read32BitsLowHigh(sf); subSize -= 4; wave_info.block_align = Read16BitsLowHigh(sf); subSize -= 2; wave_info.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 (is_wav) { /* make sure the header is sane */ gfp->num_channels = wave_info.channels; gfp->in_samplerate = wave_info.samples_per_sec; gfc->pcmbitwidth = wave_info.bits_per_sample; gfp->num_samples = data_length / (wave_info.channels * wave_info.bits_per_sample / 8); } return is_wav;}/************************************************************************** aiff_check** PURPOSE: Checks AIFF header information to make sure it is valid.* Exits if not.*************************************************************************/static voidaiff_check2(const char *file_name, IFF_AIFF *pcm_aiff_data){ if (pcm_aiff_data->sampleType != IFF_ID_SSND) { ERRORF("Sound data is not PCM in \"%s\".\n", file_name); LAME_ERROR_EXIT(); } if (pcm_aiff_data->sampleSize != sizeof(short) * BITS_IN_A_BYTE) { ERRORF("Sound data is not %d bits in \"%s\".\n", (unsigned int) sizeof(short) * BITS_IN_A_BYTE, file_name); LAME_ERROR_EXIT(); } if (pcm_aiff_data->numChannels != 1 && pcm_aiff_data->numChannels != 2) { ERRORF("Sound data is not mono or stereo in \"%s\".\n", file_name); LAME_ERROR_EXIT(); } if (pcm_aiff_data->blkAlgn.blockSize != 0) { ERRORF("Block size is not %d bytes in \"%s\".\n", 0, file_name); LAME_ERROR_EXIT(); } if (pcm_aiff_data->blkAlgn.offset != 0) { ERRORF("Block offset is not %d bytes in \"%s\".\n", 0, file_name); LAME_ERROR_EXIT(); }}/***************************************************************************** * * 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){ lame_internal_flags *gfc=gfp->internal_flags; int is_aiff = 0; long chunkSize = 0, subSize = 0; IFF_AIFF aiff_info; memset(&aiff_info, 0, sizeof(aiff_info)); chunkSize = Read32BitsHighLow(sf); if ( Read32BitsHighLow(sf) != IFF_ID_AIFF ) return 0; while ( chunkSize > 0 ) { u_int type = 0; chunkSize -= 4; type = Read32BitsHighLow(sf); /* 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 (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 */ aiff_check2("name", &aiff_info); gfp->num_channels = aiff_info.numChannels; gfp->in_samplerate = aiff_info.sampleRate; gfc->pcmbitwidth = aiff_info.sampleSize; gfp->num_samples = aiff_info.numSampleFrames; } return is_aiff;}/************************************************************************** parse_file_header** PURPOSE: Read the header from a bytestream. Try to determine whether* it's a WAV file or AIFF without rewinding, since rewind* doesn't work on pipes and there's a good chance we're reading* from stdin (otherwise we'd probably be using libsndfile).** When this function returns, the file offset will be positioned at the* beginning of the sound data.*************************************************************************/void parse_file_header(lame_global_flags *gfp,FILE *sf){ lame_internal_flags *gfc=gfp->internal_flags; u_int type = 0; type = Read32BitsHighLow(sf); /* DEBUGF( "First word of input stream: %08x '%4.4s'\n", type, (char*) &type); */ gfc->count_samples_carefully=0; gfp->input_format = sf_raw; if (type == WAV_ID_RIFF) { /* It's probably a WAV file */ if (parse_wave_header(gfp,sf)) { gfp->input_format = sf_wave; gfc->count_samples_carefully=1; } } else if (type == IFF_ID_FORM) { /* It's probably an AIFF file */ if (parse_aiff_header(gfp,sf)) { gfp->input_format = sf_aiff; gfc->count_samples_carefully=1; } } if (gfp->input_format==sf_raw) { /* ** Assume it's raw PCM. Since the audio data is assumed to begin ** at byte zero, this will unfortunately require seeking. */ if (fseek(sf, 0L, SEEK_SET) != 0) { /* ignore errors */ } gfp->input_format = sf_raw; }}void CloseSndFile(sound_file_format input,FILE * musicin){ if (fclose(musicin) != 0){ ERRORF("Could not close audio input file\n"); LAME_FATAL_EXIT(); }}FILE * OpenSndFile(lame_global_flags *gfp){ lame_internal_flags *gfc=gfp->internal_flags; const char* inPath = gfp->inPath; FILE * musicin; struct stat sb; /* set the defaults from info incase we cannot determine them from file */ gfp->num_samples=MAX_U_32_NUM; gfc->input_bitrate=0; if (!strcmp(inPath, "-")) { /* Read from standard input. */#ifdef __EMX__ _fsetmode(stdin,"b");#elif (defined __BORLANDC__) setmode(_fileno(stdin), O_BINARY);#elif (defined __CYGWIN__) setmode(fileno(stdin), _O_BINARY);#elif (defined _WIN32) _setmode(_fileno(stdin), _O_BINARY);#endif musicin = stdin; } else { if ((musicin = fopen(inPath, "rb")) == NULL) { ERRORF("Could not find \"%s\".\n", inPath); LAME_ERROR_EXIT(); } } if (gfp->input_format==sf_mp3) { mp3data_struct mp3data;#ifdef AMIGA_MPEGA if (-1==lame_decode_initfile(inPath,&mp3data)) { ERRORF("Error reading headers in mp3 input file %s.\n", inPath); LAME_ERROR_EXIT(); }#endif#ifdef HAVEMPGLIB if (-1==lame_decode_initfile(musicin,&mp3data)) { ERRORF("Error reading headers in mp3 input file %s.\n", inPath); LAME_ERROR_EXIT(); }#endif gfp->num_channels=mp3data.stereo; gfp->in_samplerate=mp3data.samplerate; gfc->input_bitrate=mp3data.bitrate; gfp->num_samples=mp3data.nsamp; }else if (gfp->input_format==sf_ogg) {#ifdef HAVEVORBIS mp3data_struct mp3data; if (-1==lame_decode_ogg_initfile(musicin,&mp3data)) { ERRORF("Error reading headers in ogg input file %s.\n", inPath); LAME_ERROR_EXIT(); } gfp->num_channels=mp3data.stereo; gfp->in_samplerate=mp3data.samplerate; gfc->input_bitrate=mp3data.bitrate; gfp->num_samples=mp3data.nsamp;#else ERRORF("LAME not compiled with libvorbis support.\n"); LAME_ERROR_EXIT();#endif }else{ if (gfp->input_format != sf_raw) { parse_file_header(gfp,musicin); } if (gfp->input_format==sf_raw) { /* assume raw PCM */ MSGF("Assuming raw pcm input file"); if (gfp->swapbytes==TRUE) MSGF(" : Forcing byte-swapping\n"); else MSGF("\n"); } } if (gfp->num_samples==MAX_U_32_NUM && musicin != stdin) {#ifdef __riscos__ _kernel_swi_regs reg;#endif /* try to figure out num_samples */#ifndef __riscos__ if (0==stat(inPath,&sb)) {#else reg.r[0]=17; reg.r[1]=(int) inPath; _kernel_swi(OS_File,®,®); if (reg.r[0] == 1) { sb.st_size=reg.r[4];#endif /* try file size, assume 2 bytes per sample */ if (gfp->input_format == sf_mp3) { if (gfc->input_bitrate>0) { FLOAT totalseconds = (sb.st_size*8.0/(1000.0*gfc->input_bitrate)); gfp->num_samples= totalseconds*gfp->in_samplerate; } }else{ gfp->num_samples = sb.st_size/(2*gfp->num_channels); } } } return musicin;}#endif /* LAMESNDFILE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -