Showing posts with label Audio. Show all posts
Showing posts with label Audio. Show all posts

Friday, 15 July 2016

Convert RAW Audio into mp3 Encoded Audio using FFMPEG

Download lame Source code

Header File
#include "armffmpeg/lame/include/lame.h"

Variables
int pcm_samples_2d[2][RAWBUFSIZE];
lame_global_flags *mp3;

numBytes = RAWBUFSIZE;

Init lame flags.
mp3 = lame_init();
if(!mp3) {
        printf("Unable to initialize mp3 object.");
} else {
        printf("Able to initialize mp3 object.\n");
}

/* set other parameters.*/
lame_set_num_channels(mp3, 1);
/*lame_set_num_samples(mp3, (nSecondsAudio * sampleRate));*/
lame_set_in_samplerate(mp3, gAudSampleRate);
lame_set_quality(mp3, 5);  /* set for high speed and good quality. */
lame_set_mode(mp3, 3);  /* the input audio is mono */

lame_set_out_samplerate(mp3, gAudSampleRate);
lame_set_VBR(mp3, vbr_default);
printf("Able to set a number of parameters too.");
framesize = lame_get_framesize(mp3);
printf("Framesize = %d\n", framesize);

/* set more internal variables. check for failure.*/
if(lame_init_params(mp3) == -1) {
        printf("Something failed in setting internal parameters.");
}

Encode mp3 From RAW Data

memset(pcm_samples_2d[1], 0, (numBytes/2) * sizeof(int));
memset(pcm_samples_2d[0], 0, (numBytes/2) * sizeof(int));
i = 0;
for(i = 0; i < (numBytes/2); i++) {
        pcm_samples_2d[0][i] = *((int *)InAudioBuf + i);
}
encodedBufferSize = lame_encode_buffer(mp3, (const short int *)pcm_samples_2d[0], (const short int *)pcm_samples_2d[1],
                numBytes/2, OutAudioBuf, numBytes/2);


Close Lame module
lame_close(mp3);

FLV Audio File Format

Audio Tag

FLV Audio Data Format
0 = Linear PCM, platform endian
1 = ADPCM
2 = MP3
3 = Linear PCM, little endian
4 = Nellymoser 16-kHz mono
5 = Nellymoser 8-kHz mono
6 = Nellymoser
7 = G.711 A-law logarithmic PCM
8 = G.711 mu-law logarithmic PCM
9 = reserved
10 = AAC        //Sampling Rate 3 for AAC
11 = Speex
14 = MP3 8-Khz
15 = Device-specific sound

Sample Rate

0 = 5.5-kHz
1 = 11-kHz
2 = 22-kHz
3 = 44-kHz

Sound Size
0 = snd8Bit
1 = snd16Bit

Audio Type
0 = sndMono
1 = sndStereo

Convert RAW Audio into AAC Encoded Audio using FFMPEG

AAC - Advanced Audio Codec.

Download FAAC Source code from https://sourceforge.net/projects/faac/

Header File
<faac.h>

Variables
faacEncHandle codec;
faacEncConfigurationPtr conf;
unsigned long* one;
unsigned long* two;

Initialize and configure faac

/* AAC Audio FFMPEG */
one = (unsigned long *)calloc(1,((sizeof(unsigned long))+(32 - 1))/(32)*(32));
if (one == NULL)
{
        printf("Failed to allocate audio output buffer\n");
        return;
}
two = (unsigned long *)calloc(1,((sizeof(unsigned long))+(32 - 1))/(32)*(32));
if (two == NULL)
{
        FREE(one)
                printf("Failed to allocate audio output buffer\n");
        return;
}

codec=faacEncOpen(SampleRate, 1, one, two);
if(!codec){
        printf("Something went wrong");
        FREE(one)
                FREE(two)
                return;
}

conf=faacEncGetCurrentConfiguration(codec);

/* Here you can change configure as per your Requirment
**
MPEG ID's
MPEG2 1
MPEG4 0

AAC object types
MAIN 0
LOW  1
SSR  2
LTP  3

Input Formats
FAAC_INPUT_NULL    0
FAAC_INPUT_16BIT   1
FAAC_INPUT_24BIT   2
FAAC_INPUT_32BIT   3
FAAC_INPUT_FLOAT   4
*/
conf->inputFormat = FAAC_INPUT_16BIT;
conf->mpegVersion=MPEG4;
conf->aacObjectType=SSR;

faacEncSetConfiguration(codec,conf);

Call Encoder APIs to Encode AAC from RAW Audio

gAudInBuf = (unsigned char *) malloc(4000);
if (gAudInBuf == NULL)
{
        printf("Failed to allocate audio input buffer\n");
        break;
}


vdSize=faacEncEncode(codec,(int*)gAudInBuf, *one, adBuffer, *two);

Close faac Encoder module and Free Memory

faacEncClose(codec);
FREE(one)
FREE(two)

RAW to G711(μ-Law) encoding algorithm

/* ===================================================================
 *  @func     ALG_G711ulawEncode
 *
 *  @desc     G711 Encode
 *
 *  @inputs   This function takes the following inputs
 *            src - RAW data buffer
 *            bufsize - Size of RAW data
 *
 *  @outputs  dst - G711 encoded Data
 *
 *  @return   Size of Encoded Data
 *  ==================================================================
int ALG_G711ulawEncode(short *dst, short *src, Int32 bufsize)
{
    unsigned short i;

    short data;

    unsigned short isNegative;

    short nOut;

    short lowByte = 1;

    int outputSize = bufsize / 2;

    for (i = 0; i < outputSize; i++)
    {
        data = *(src + i);
        data >>= 2;
        isNegative = (data < 0 ? 1 : 0);

        if (isNegative)
            data = -data;

        if (data <= 1)
        {
            nOut = (char) data;
        }
        else if (data <= 31)
        {
            nOut = ((data - 1) >> 1) + 1;
        }
        else if (data <= 95)
        {
            nOut = ((data - 31) >> 2) + 16;
        }
        else if (data <= 223)
        {
            nOut = ((data - 95) >> 3) + 32;
        }
        else if (data <= 479)
        {
            nOut = ((data - 223) >> 4) + 48;
        }
        else if (data <= 991)
        {
            nOut = ((data - 479) >> 5) + 64;
        }
        else if (data <= 2015)
        {
            nOut = ((data - 991) >> 6) + 80;
        }
        else if (data <= 4063)
        {
            nOut = ((data - 2015) >> 7) + 96;
        }
        else if (data <= 7903)
        {
            nOut = ((data - 4063) >> 8) + 112;
        }
        else
        {
            nOut = 127;
        }

        if (isNegative)
        {
            nOut = 127 - nOut;
        }
        else
        {
            nOut = 255 - nOut;
        }

        // Pack the bytes in a word
        if (lowByte)
        {
            *(dst + (i >> 1)) = (nOut & 0x00FF);
        }
        else
        {
            *(dst + (i >> 1)) |= ((nOut << 8) & 0xFF00);
        }

        lowByte ^= 0x1;
    }

    return (outputSize);
}

G711(μ-Law) to RAW decoding Algorithm

/* ===================================================================
 *  @func     AUDIO_audioDecode
 *
 *  @desc     Function does the following
 *
 *  @inputs   This function takes the following inputs
 *            src - G711 data buffer
 *            bufsize - Size of G711 data
 *
 *  @outputs  dst - RAW Data
 *
 *  @return   SUCCESS
 *  ==================================================================
 */
int AUDIO_audioDecode(char *src, short *dst,
                      int bufsize)
{
    int sizeConsumed = 0, i = 0;

    sizeConsumed = bufsize;

    for (i = 0; i < sizeConsumed; i++)
    {
        *((unsigned short *) (dst) + i) =
            ALG_G711ulawDecode(*((unsigned char *) (src) + i));
    }
   
    return TRUE;
}

/* ===================================================================
 *  @func     ALG_G711ulawDecode
 *
 *  @desc     Decode G711 Audio
 *
 *  @inputs   This function takes the following inputs
 *            input - G711 data
 *
 *  @return   RAW Data
 *  ==================================================================
 */
short ALG_G711ulawDecode(unsigned short input)
{
    unsigned short isNegative;

    short nOut;

    isNegative = ((input & 0x80) == 0);
    if (isNegative)
        nOut = 127 - input;
    else
        nOut = 255 - input;
    if (nOut < 2)
        nOut *= 2;
    else if (nOut < 16)
        nOut = ((nOut - 1) << 1) + 1 + 1;
    else if (nOut < 32)
        nOut = ((nOut - 16) << 2) + 2 + 31;
    else if (nOut < 48)
        nOut = ((nOut - 32) << 3) + 4 + 95;
    else if (nOut < 64)
        nOut = ((nOut - 48) << 4) + 8 + 223;
    else if (nOut < 80)
        nOut = ((nOut - 64) << 5) + 16 + 479;
    else if (nOut < 96)
        nOut = ((nOut - 80) << 6) + 32 + 991;
    else if (nOut < 112)
        nOut = ((nOut - 96) << 7) + 64 + 2015;
    else
        nOut = ((nOut - 112) << 8) + 128 + 4063;
    if (isNegative)
        nOut = -nOut;
    nOut <<= 2;
    return nOut;
}