AMR Audio Codec

Source: Internet
Author: User
Tags fread
Http://blog.csdn.net/dinggo/archive/2007/12/29/2002298.aspx

AMR Audio Codec
Table of Contents 1.    Overview 2.    AMR Code 3.    AMR decoding 4.    AMR frame reading algorithm 5. Resources 1. Overview Many smartphones now support multimedia features, especially audio and video playback, and the AMR file format is a widely supported audio file format for mobile phones.   AMR, full name: Adaptive multi-rate, Adaptive multi-rate, is an audio encoded file format designed to effectively compress speech frequencies.   AMR audio is mainly used for mobile device audio compression, compression ratio is very high, but poor sound quality, mainly used for audio compression of speech class, not suitable for sound quality requirements of the music class audio compression.   AMR codec is based on "3GPP AMR floating-point Speech Codec" to do, 3GPP is also dedicated to the implementation of ANSI-C-based codec code, so that we can be ported on various platforms.   #ifndef amrfilecodec_h #define AMRFILECODEC_H   #define AMR_MAGIC_NUMBER "#! amr/n "  #define PCM_FRAME_SIZE//8khz 8000*0.02=160 #define MAX_AMR_FRAME_SIZE #define Amr_frame_count_per_s Econd//int amrencodemode[] = {4750, 5150, 5900, 6700, 7400, 7950, 10200, 12200}; AMR encoding method   typedef struct {         char chchunkid[4];   & nbsp;      int nchunksize; }xchunkheader;   typedef struct {         short nformattag;           short nchannels;         int nsamplespersec;          int navgbytespersec;          short nblockalign;          short nbitspersample; }waveformat;   typedef struct {         short nformattag;           short nchannels;          int nsamplespersec;          int navgbytespersec;          short nblockalign;          short nbitspersample;          short nexsize; }waveformatx;   typedef struct {         char chriffid[4];           int nriffsize;          Char chriffformat[4]; }riffheader;   typedef struct {         char chfmtid[4];           int nfmtsize;          Waveformat WF; }fmtblock;  //WAVE audio sampling frequency is 8khz//audio sample Unit number = 8000*0.02 = 160 (determined by sampling frequency)//channel number 1:160//         2:160*2 =//BPS decision Sample size//bps = 8--> 8 bit unsigned char//    &nb sp;  to 16-bit unsigned short int encodewavefiletoamrfile (const char* pchwavefilename, const char* Pchamrfilenam e, int nchannels, int nbitspersample);  //decode AMR file into WAVE file int decodeamrfiletowavefile (const char* pchamrfilename, const char* pchwavefilename);   #endif  
2. Amr Code The 3GPP provides a coding code and provides a ENCODER.C program that demonstrates how to compress a 16-bit mono PCM data. (Sampling frequency must be 8khz)   I have extended the program, data bits support 8-bit and 16-bit, can be mono and dual channel.   l         for 8-bit PCM you only need to expand the sample data bit for each sample to 16 bits and move the 7 bits to the left. l         for two-channel, you can only process the left channel data, or you can only work on the right channel data, or to the left and the channel data averaging.   So two small processing, you can standardize the PCM into 3PGG encoder required data format. The   code is in AMRFILEENCODER.C.   #include "amrFileCodec.h"  //Skip Wave file header from wave file, direct to PCM audio data void Skiptopcmaudiodata (file* fpwave) {&nbs p;        Riffheader riff;          Fmtblock FMT;          Xchunkheader Chunk;          Waveformatx wfx;          int bdatablock = 0;           //1. Read RIFF head          fread (&riFF, 1, sizeof (Riffheader), fpwave);           //2. Read the FMT block-if the fmt.nfmtsize>16 description needs to have a secondary size not read          fread (&chunk, 1, sizeof (Xchunkheader), fpwave);          if (chunk.nchunksize>16)           {                    fread (&wfx, 1, sizeof (WAVEFORMATX), fpwave);         }          Else           {                    memcpy (Fmt.chfmtid, Chunk.chchunkid, 4);                    Fmt.nfmtsize = chunk.nchunksize;                    fread (&AMP;FMT.WF, 1 , sizeof (Waveformat), fpwave);         }           //3. Go to data block-some have fact blocks and so on.          while (!bdatablock)           {                    Fread (&chunk, 1, sizeof (Xchunkheader), fpwave);                    if (!MEMCMP (Chunk.chchunkid, "Data", 4))                     {                             Bdatablock = 1;                              break;                   }                   // Because this is not a data block, it skips over the blocks                     fseek (Fpwave, Chunk.nchunksize, seek_cur);         }  //read a full PCM audio frame//return value from WAVE file: 0-Error >0: Full frame size int Rea Dpcmframe (short speech[], file* fpwave, int nchannels, int nbitspersample) {          int nread = 0;          int x = 0, y=0;          unsigned short ush1=0, ush2=0, ush=0;           //Raw PCM audio frame data          unsigned Char Pcmframe_8b1[pcm_frame_size];          unsigned char pcmframe_8b2[pcm_frame_size<<1];          unsigned short pcmframe_16b1[pcm_frame_size];          unsigned short pcmframe_16b2[pcm_frame_size<<1];            if (nbitspersample==8 && nchannels==1)           {                    nread = Fread (pcmframe_8b1, (NBITSPERSAMPLE/8), Pcm_frame_size*nchannels, Fpwave);                    for (x=0; x<pcm_frame_size; x + +) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;         {                             SPEECH[X] = (short) ((short) pcmframe_8b1[x] << 7);                   }         }          Else           if (nbitspersample==8 && nchannels==2)           {                    nread = Fread (pcmframe_8b2, (NBITSPERSAMPLE/8), Pcm_frame_size*nchannels, Fpwave);                    For (x=0, y=0; y<pcm_frame_size; y++,x+=2)                    {                             //1-Take the left channel of two channels                              Speech[y ] = (short) ((short) pcmframe_8b2[x+0] << 7);                             //2-take the right channel of two channels                              //speech[y] = (short) pcmframe_8b2[x+1] << 7);                            //3-Take two channels of average                              //ush1 = (short) pcmframe_8b2[x+0];                             //USH2 = (short) pcmframe_8b2[x+1];                             //ush = (ush1 + ush2) >> 1;                             //speech[y] = (short) ush << 7);                   }         }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.