📄 get_audio.c
字号:
const char* lpszFileName = gfp->inPath; FILE * musicin; SNDFILE *gs_pSndFileIn; SF_INFO gs_wfInfo; gfc->input_bitrate=0; if (gfp->input_format==sf_mp3) { mp3data_struct mp3data;#ifdef AMIGA_MPEGA if (-1==lame_decode_initfile(lpszFileName,&mp3data)) { ERRORF("Error reading headers in mp3 input file %s.\n", lpszFileName); LAME_ERROR_EXIT(); }#endif#ifdef HAVEMPGLIB if ((musicin = fopen(lpszFileName, "rb")) == NULL) { ERRORF("Could not find \"%s\".\n", lpszFileName); LAME_ERROR_EXIT(); } if (-1==lame_decode_initfile(musicin,&mp3data)) { ERRORF("Error reading headers in mp3 input file %s.\n", lpszFileName); 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 if ((musicin = fopen(lpszFileName, "rb")) == NULL) { ERRORF("Could not find \"%s\".\n", lpszFileName); LAME_ERROR_EXIT(); } if (-1==lame_decode_ogg_initfile(musicin,&mp3data)) { ERRORF("Error reading headers in mp3 input file %s.\n", lpszFileName); LAME_ERROR_EXIT(); } gfp->num_channels=0; gfp->in_samplerate=0; gfc->input_bitrate=0; gfp->num_samples=0;#else ERRORF("LAME not compiled with libvorbis support.\n"); LAME_ERROR_EXIT();#endif } else { /* Try to open the sound file */ /* set some defaults incase input is raw PCM */ gs_wfInfo.seekable=(gfp->input_format!=sf_raw); /* if user specified -r, set to not seekable */ gs_wfInfo.samplerate=gfp->in_samplerate; gs_wfInfo.pcmbitwidth=16; gs_wfInfo.channels=gfp->num_channels; if (DetermineByteOrder()==order_littleEndian) { if (gfp->swapbytes) gs_wfInfo.format=SF_FORMAT_RAW_BE; else gs_wfInfo.format=SF_FORMAT_RAW_LE; } else { if (gfp->swapbytes) gs_wfInfo.format=SF_FORMAT_RAW_LE; else gs_wfInfo.format=SF_FORMAT_RAW_BE; } gs_pSndFileIn=sf_open_read(lpszFileName,&gs_wfInfo); musicin = (SNDFILE *) gs_pSndFileIn; /* Check result */ if (gs_pSndFileIn==NULL) { sf_perror(gs_pSndFileIn); ERRORF("Could not open sound file \"%s\".\n", lpszFileName); LAME_ERROR_EXIT(); } if ((gs_wfInfo.format==SF_FORMAT_RAW_LE) || (gs_wfInfo.format==SF_FORMAT_RAW_BE)) gfp->input_format=sf_raw;#ifdef _DEBUG_SND_FILE DEBUGF("\n\nSF_INFO structure\n"); DEBUGF("samplerate :%d\n",gs_wfInfo.samplerate); DEBUGF("samples :%d\n",gs_wfInfo.samples); DEBUGF("channels :%d\n",gs_wfInfo.channels); DEBUGF("pcmbitwidth :%d\n",gs_wfInfo.pcmbitwidth); DEBUGF("format :"); /* new formats from sbellon@sbellon.de 1/2000 */ if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_WAV) DEBUGF("Microsoft WAV format (big endian). "); if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_AIFF) DEBUGF("Apple/SGI AIFF format (little endian). "); if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_AU) DEBUGF("Sun/NeXT AU format (big endian). "); if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_AULE) DEBUGF("DEC AU format (little endian). "); if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_RAW) DEBUGF("RAW PCM data. "); if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_PAF) DEBUGF("Ensoniq PARIS file format. "); if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_SVX) DEBUGF("Amiga IFF / SVX8 / SV16 format. "); if ((gs_wfInfo.format&SF_FORMAT_TYPEMASK)==SF_FORMAT_NIST) DEBUGF("Sphere NIST format. "); if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM) DEBUGF("PCM data in 8, 16, 24 or 32 bits."); if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_FLOAT) DEBUGF("32 bit Intel x86 floats."); if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_ULAW) DEBUGF("U-Law encoded."); if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_ALAW) DEBUGF("A-Law encoded."); if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_IMA_ADPCM) DEBUGF("IMA ADPCM."); if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_MS_ADPCM) DEBUGF("Microsoft ADPCM."); if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_BE) DEBUGF("Big endian PCM data."); if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_LE) DEBUGF("Little endian PCM data."); if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_S8) DEBUGF("Signed 8 bit PCM."); if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_PCM_U8) DEBUGF("Unsigned 8 bit PCM."); if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_SVX_FIB) DEBUGF("SVX Fibonacci Delta encoding."); if ((gs_wfInfo.format&SF_FORMAT_SUBMASK)==SF_FORMAT_SVX_EXP) DEBUGF("SVX Exponential Delta encoding."); DEBUGF("\n"); DEBUGF("pcmbitwidth :%d\n",gs_wfInfo.pcmbitwidth); DEBUGF("sections :%d\n",gs_wfInfo.sections); DEBUGF("seekable :\n",gs_wfInfo.seekable);#endif gfp->num_samples = gs_wfInfo.samples; gfp->num_channels = gs_wfInfo.channels; gfp->in_samplerate = gs_wfInfo.samplerate; gfc->pcmbitwidth=gs_wfInfo.pcmbitwidth; } if (gfp->num_samples==MAX_U_32_NUM) { struct stat sb;#ifdef __riscos__ _kernel_swi_regs reg;#endif /* try to figure out num_samples */#ifndef __riscos__ if (0==stat(lpszFileName,&sb)) {#else /* __riscos__ */ reg.r[0]=17; reg.r[1]=(int) lpszFileName; _kernel_swi(OS_File,®,®); if (reg.r[0] == 1) { sb.st_size=reg.r[4];#endif /* __riscos__ */ /* try file size, assume 2 bytes per sample */ if (gfp->input_format == sf_mp3) { 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;}/************************************************************************** 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.*************************************************************************/int read_samples_pcm(lame_global_flags *gfp,short sample_buffer[2304],int frame_size,int samples_to_read){ int samples_read; int rcode; lame_internal_flags *gfc=gfp->internal_flags; SNDFILE * gs_pSndFileIn; gs_pSndFileIn = (SNDFILE *)gfp->musicin; samples_read=sf_read_short(gs_pSndFileIn,sample_buffer,samples_to_read); rcode = samples_read; if (8==gfc->pcmbitwidth) for (; samples_read > 0; sample_buffer[--samples_read] *= 256); return(rcode);}#endif /* ifdef LIBSNDFILE */#ifdef LAMESNDFILE/************************************************************************ ************************************************************************ ************************************************************************ ************************************************************************ ************************************************************************ ************************************************************************ * * OLD ISO/LAME routines follow. Used if you dont have LIBSNDFILE * or for stdin/stdout support * ************************************************************************ ************************************************************************ ************************************************************************ ************************************************************************ ************************************************************************ ************************************************************************//* Replacement for forward fseek(,,SEEK_CUR), because fseek() fails on pipes */int fskip(FILE *sf,long num_bytes,int dummy){ char data[1024]; int nskip = 0; while (num_bytes > 0) { nskip = (num_bytes>1024) ? 1024 : num_bytes; num_bytes -= fread(data,(size_t)1,(size_t)nskip,sf); } /* return 0 if last read was successful */ return num_bytes;}/************************************************************************** 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.*************************************************************************/int read_samples_pcm(lame_global_flags *gfp,short sample_buffer[2304], int frame_size,int samples_to_read){ lame_internal_flags *gfc=gfp->internal_flags; int samples_read; int iswav=(gfp->input_format==sf_wave); if (16==gfc->pcmbitwidth) { samples_read = fread(sample_buffer, 2, (unsigned int)samples_to_read, gfp->musicin); }else if (8==gfc->pcmbitwidth) { signed char temp[2304]; int i; samples_read = fread(temp, 1, (unsigned int)samples_to_read, gfp->musicin); for (i=0 ; i<samples_read; ++i) sample_buffer[i]=(short int)temp[i]*256; }else{ ERRORF("Only 8 and 16 bit input files supported \n"); LAME_ERROR_EXIT(); } if (ferror(gfp->musicin)) { ERRORF("Error reading input file\n"); LAME_ERROR_EXIT(); } /* Samples are big-endian. If this is a little-endian machine we must swap */ if ( NativeByteOrder == order_unknown ) { NativeByteOrder = DetermineByteOrder(); if ( NativeByteOrder == order_unknown ) { ERRORF("byte order not determined\n" ); LAME_ERROR_EXIT(); } } /* intel=littleEndian */ if (!iswav && ( NativeByteOrder == order_littleEndian )) SwapBytesInWords( sample_buffer, samples_read ); if (iswav && ( NativeByteOrder == order_bigEndian )) SwapBytesInWords( sample_buffer, samples_read ); if (gfp->swapbytes==TRUE) SwapBytesInWords( sample_buffer, samples_read ); return samples_read;}/* AIFF Definitions */#define IFF_ID_FORM 0x464f524d /* "FORM" */#define IFF_ID_AIFF 0x41494646 /* "AIFF" */#define IFF_ID_COMM 0x434f4d4d /* "COMM" */#define IFF_ID_SSND 0x53534e44 /* "SSND" */#define IFF_ID_MPEG 0x4d504547 /* "MPEG" */#define WAV_ID_RIFF 0x52494646 /* "RIFF" */#define WAV_ID_WAVE 0x57415645 /* "WAVE" */#define WAV_ID_FMT 0x666d7420 /* "fmt " */#define WAV_ID_DATA 0x64617461 /* "data" */typedef struct fmt_chunk_data_struct { short format_tag; /* Format category */ u_short channels; /* Number of channels */ u_long samples_per_sec; /* Sampling rate */ u_long avg_bytes_per_sec; /* For buffer estimation */ u_short block_align; /* Data block size */ u_short bits_per_sample; /* for PCM data, anyway... */} fmt_chunk_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){ lame_internal_flags *gfc=gfp->internal_flags; fmt_chunk_data wave_info; int is_wav = 0; long data_length = 0, file_length, subSize = 0; int loop_sanity = 0; memset(&wave_info, 0, sizeof(wave_info)); file_length = Read32BitsHighLow(sf); if (Read32BitsHighLow(sf) != WAV_ID_WAVE) return 0; for (loop_sanity = 0; loop_sanity < 20; ++loop_sanity) { u_int type = Read32BitsHighLow(sf); if (type == WAV_ID_FMT) { subSize = Read32BitsLowHigh(sf);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -