⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 get_audio.c

📁 音频编码
💻 C
📖 第 1 页 / 共 4 页
字号:
read_samples_mp3(lame_global_flags * const gfp,                 FILE * const musicin, short int mpg123pcm[2][1152], int stereo){    int     out;#if defined(AMIGA_MPEGA)  ||  defined(HAVE_MPGLIB)    static const char type_name[] = "MP3 file";    out =        lame_decode_fromfile(musicin, mpg123pcm[0], mpg123pcm[1],                             &mp3input_data);    /*     * out < 0:  error, probably EOF     * out = 0:  not possible with lame_decode_fromfile() ???     * out > 0:  number of output samples     */    if (out < 0) {        memset(mpg123pcm, 0, sizeof(**mpg123pcm) * 2 * 1152);        return 0;    }    if ( lame_get_num_channels( gfp ) != mp3input_data.stereo )	if( silent < 10 ) {	    fprintf(stderr,		    "Error: number of channels has changed in %s - not supported\n",		    type_name);	}    if ( lame_get_in_samplerate( gfp ) != mp3input_data.samplerate )	if( silent < 10 ) {	    fprintf(stderr,		    "Error: sample frequency has changed in %s - not supported\n",		    type_name);	}#else    out = -1;#endif    return out;}intWriteWaveHeader(FILE * const fp, const int pcmbytes,                const int freq, const int channels, const int bits){    int     bytes = (bits + 7) / 8;    /* quick and dirty, but documented */    fwrite("RIFF", 1, 4, fp); /* label */    Write32BitsLowHigh(fp, pcmbytes + 44 - 8); /* length in bytes without header */    fwrite("WAVEfmt ", 2, 4, fp); /* 2 labels */    Write32BitsLowHigh(fp, 2 + 2 + 4 + 4 + 2 + 2); /* length of PCM format declaration area */    Write16BitsLowHigh(fp, 1); /* is PCM? */    Write16BitsLowHigh(fp, channels); /* number of channels */    Write32BitsLowHigh(fp, freq); /* sample frequency in [Hz] */    Write32BitsLowHigh(fp, freq * channels * bytes); /* bytes per second */    Write16BitsLowHigh(fp, channels * bytes); /* bytes per sample time */    Write16BitsLowHigh(fp, bits); /* bits per sample */    fwrite("data", 1, 4, fp); /* label */    Write32BitsLowHigh(fp, pcmbytes); /* length in bytes of raw PCM data */    return ferror(fp) ? -1 : 0;}#if defined(LIBSNDFILE)/*** Copyright (C) 1999 Albert Faber** * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */voidCloseSndFile(sound_file_format input, FILE * musicin){    SNDFILE *gs_pSndFileIn = (SNDFILE *) musicin;    if (input == sf_mp1 || input == sf_mp2 || input == sf_mp3) {#ifndef AMIGA_MPEGA        if (fclose(musicin) != 0) {	    if( silent < 10 ) {		fprintf(stderr, "Could not close audio input file\n");	    }            exit(2);        }#endif    }    else {        if (gs_pSndFileIn) {            if (sf_close(gs_pSndFileIn) != 0) {		if( silent < 10 ) {		    fprintf(stderr, "Could not close sound file \n");		}                exit(2);            }        }    }}FILE   *OpenSndFile(lame_global_flags * gfp, char *inPath){    char   *lpszFileName = inPath;    FILE   *musicin;    SNDFILE *gs_pSndFileIn;    SF_INFO gs_wfInfo;    if (input_format == sf_mp1 ||        input_format == sf_mp2 || input_format == sf_mp3) {#ifdef AMIGA_MPEGA        if (-1 == lame_decode_initfile(lpszFileName, &mp3input_data)) {	    if( silent < 10 ) {		fprintf(stderr, "Error reading headers in mp3 input file %s.\n",			lpszFileName);	    }            exit(1);        }#endif#ifdef HAVE_MPGLIB        if ((musicin = fopen(lpszFileName, "rb")) == NULL) {	    if( silent < 10 ) {		fprintf(stderr, "Could not find \"%s\".\n", lpszFileName);	    }            exit(1);        }        if (-1 == lame_decode_initfile(musicin, &mp3input_data)) {	    if( silent < 10 ) {		fprintf(stderr, "Error reading headers in mp3 input file %s.\n",			lpszFileName);	    }            exit(1);        }#endif        if( -1 == lame_set_num_channels( gfp, mp3input_data.stereo ) ) {	    if( silent < 10 ) {		fprintf( stderr,			 "Unsupported number of channels: %ud\n",			 mp3input_data.stereo );	    }            exit( 1 );        }        (void) lame_set_in_samplerate( gfp, mp3input_data.samplerate );        (void) lame_set_num_samples( gfp, mp3input_data.nsamp );    }    else if (input_format == sf_ogg) {	if( silent < 10 ) {	    fprintf(stderr, "sorry, vorbis support in LAME is deprecated.\n");	}        exit(1);    }    else {        /* Try to open the sound file */        /* set some defaults incase input is raw PCM */        gs_wfInfo.seekable = (input_format != sf_raw); /* if user specified -r, set to not seekable */        gs_wfInfo.samplerate = lame_get_in_samplerate( gfp );        gs_wfInfo.pcmbitwidth = in_bitwidth;        gs_wfInfo.channels = lame_get_num_channels( gfp );	if (in_bitwidth == 8) {	    if (in_signed)		gs_wfInfo.format = SF_FORMAT_RAW_S8;	    else		gs_wfInfo.format = SF_FORMAT_RAW_U8;	} else {	    if (!in_signed) {		fputs("Unsigned input only supported with bitwidth 8\n", stderr);		exit(1);	    }	    if (in_endian != order_unknown) {		if (in_endian == order_littleEndian)		    gs_wfInfo.format = SF_FORMAT_RAW_LE;		else		    gs_wfInfo.format = SF_FORMAT_RAW_BE;	    } else {#ifndef WORDS_BIGENDIAN		/* little endian */		if (swapbytes)		    gs_wfInfo.format = SF_FORMAT_RAW_BE;		else		    gs_wfInfo.format = SF_FORMAT_RAW_LE;#else		if (swapbytes)		    gs_wfInfo.format = SF_FORMAT_RAW_LE;		else		    gs_wfInfo.format = SF_FORMAT_RAW_BE;#endif	    }	}        gs_pSndFileIn = sf_open_read(lpszFileName, &gs_wfInfo);        musicin = (SNDFILE *) gs_pSndFileIn;        /* Check result */        if (gs_pSndFileIn == NULL) {            sf_perror(gs_pSndFileIn);	    if( silent < 10 ) {		fprintf(stderr, "Could not open sound file \"%s\".\n",			lpszFileName);	    }            exit(1);        }        if ((gs_wfInfo.format == SF_FORMAT_RAW_LE) ||            (gs_wfInfo.format == SF_FORMAT_RAW_BE) ||	    (gs_wfInfo.format == SF_FORMAT_RAW_S8) ||	    (gs_wfInfo.format == SF_FORMAT_RAW_U8))	    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 */        switch (gs_wfInfo.format & SF_FORMAT_TYPEMASK) {        case SF_FORMAT_WAV:            DEBUGF("Microsoft WAV format (big endian). ");            break;        case SF_FORMAT_AIFF:            DEBUGF("Apple/SGI AIFF format (little endian). ");            break;        case SF_FORMAT_AU:            DEBUGF("Sun/NeXT AU format (big endian). ");            break;        case SF_FORMAT_AULE:            DEBUGF("DEC AU format (little endian). ");            break;        case SF_FORMAT_RAW:            DEBUGF("RAW PCM data. ");            break;        case SF_FORMAT_PAF:            DEBUGF("Ensoniq PARIS file format. ");            break;        case SF_FORMAT_SVX:            DEBUGF("Amiga IFF / SVX8 / SV16 format. ");            break;        case SF_FORMAT_NIST:            DEBUGF("Sphere NIST format. ");            break;        default:            assert(0);            break;        }        switch (gs_wfInfo.format & SF_FORMAT_SUBMASK) {        case SF_FORMAT_PCM:            DEBUGF("PCM data in 8, 16, 24 or 32 bits.");            break;        case SF_FORMAT_FLOAT:            DEBUGF("32 bit Intel x86 floats.");            break;        case SF_FORMAT_ULAW:            DEBUGF("U-Law encoded.");            break;        case SF_FORMAT_ALAW:            DEBUGF("A-Law encoded.");            break;        case SF_FORMAT_IMA_ADPCM:            DEBUGF("IMA ADPCM.");            break;        case SF_FORMAT_MS_ADPCM:            DEBUGF("Microsoft ADPCM.");            break;        case SF_FORMAT_PCM_BE:            DEBUGF("Big endian PCM data.");            break;        case SF_FORMAT_PCM_LE:            DEBUGF("Little endian PCM data.");            break;        case SF_FORMAT_PCM_S8:            DEBUGF("Signed 8 bit PCM.");            break;        case SF_FORMAT_PCM_U8:            DEBUGF("Unsigned 8 bit PCM.");            break;        case SF_FORMAT_SVX_FIB:            DEBUGF("SVX Fibonacci Delta encoding.");            break;        case SF_FORMAT_SVX_EXP:            DEBUGF("SVX Exponential Delta encoding.");            break;        default:            assert(0);            break;        }        DEBUGF("\n");        DEBUGF("pcmbitwidth       :%d\n", gs_wfInfo.pcmbitwidth);        DEBUGF("sections          :%d\n", gs_wfInfo.sections);        DEBUGF("seekable          :\n", gs_wfInfo.seekable);#endif        (void) lame_set_num_samples( gfp, gs_wfInfo.samples );        if( -1 == lame_set_num_channels( gfp, gs_wfInfo.channels ) ) {	    if( silent < 10 ) {		fprintf( stderr,			 "Unsupported number of channels: %ud\n",			 gs_wfInfo.channels );	    }            exit( 1 );        }        (void) lame_set_in_samplerate( gfp, gs_wfInfo.samplerate );        pcmbitwidth = gs_wfInfo.pcmbitwidth;    }    if (lame_get_num_samples( gfp ) == MAX_U_32_NUM) {        /* try to figure out num_samples */        double  flen = lame_get_file_size( lpszFileName );        if (flen >= 0) {            /* try file size, assume 2 bytes per sample */            if (input_format == sf_mp1 ||                input_format == sf_mp2 || input_format == sf_mp3) {		if (mp3input_data.bitrate>0) {		    double  totalseconds =			(flen * 8.0 / (1000.0 * mp3input_data.bitrate));		    unsigned long tmp_num_samples =			totalseconds * lame_get_in_samplerate( gfp );		    		    (void) lame_set_num_samples( gfp, tmp_num_samples );		    mp3input_data.nsamp = tmp_num_samples;		}            }            else {                lame_set_num_samples( gfp,                    flen / (2 * lame_get_num_channels( gfp )) );            }        }    }    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.*************************************************************************/static intread_samples_pcm(FILE * const musicin, int sample_buffer[2304],                 int frame_size /* unused */ , int samples_to_read){    int     i;    int     samples_read;    samples_read =        sf_read_int((SNDFILE *) musicin, sample_buffer, samples_to_read);    switch (pcmbitwidth) {    case 8:        for (i = 0; i < samples_read; i++)            sample_buffer[i] <<= (8 * sizeof(int) - 8);        break;    case 16:        for (i = 0; i < samples_read; i++)            sample_buffer[i] <<= (8 * sizeof(int) - 16);        break;    case 24:        for (i = 0; i < samples_read; i++)            sample_buffer[i] <<= (8 * sizeof(int) - 24);	break;    case 32:	break;    default:	if( silent < 10 ) {	    fprintf(stderr, "Only 8, 16, 24 and 32 bit input files supported \n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -