g729和wav互转格式

470 阅读2分钟

g729格式通常用于电话线路

一些常用的格式转换命令

mp3转8k单channel wav文件

sox 0505.mp3 0505.wav rate 8k channels 1

wav转pcm

ffmpeg -i 0505.wav -f s16le -ar 8000 -acodec 0505.pcm

pcm转g729文件

./encoderTest 0505.pcm

m4a转wav文件

ffmpeg -i test.m4a -acodec pcm_s16le -ac 1 -ar 16000 out.wav

g729转wav文件

ffmpeg -acodec g729 -f g729 -i out.g729 -ar 8000 test.wav

encoderTest.c

/*
 encoderTest.c
 Copyright (C) 2011 Belledonne Communications, Grenoble, France
 Author : Johan Pascal
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program 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 General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
/*****************************************************************************/
/*                                                                           */
/* Test Program for encoder                                                  */
/*    Input: the reconstructed signal : each frame (80 16 bits PCM values)   */
/*           on a row of a text CSV file                                     */
/*    Output: 15 parameters on each row of a text CSV file.                  */
/*                                                                           */
/*****************************************************************************/
#include 
#include 
#include 
#include "typedef.h"
#include "codecParameters.h"
#include "utils.h"
#include "testUtils.h"
#include "bcg729/encoder.h"
int main(int argc, char *argv[] )
{
    /*** get calling argument ***/
    char *filePrefix;
    getArgument(argc, argv, &filePrefix); /* check argument and set filePrefix if needed */
    /*** input and output file pointers ***/
    FILE *fpInput;
    /*** input and output buffers ***/
    int16_t inputBuffer[L_FRAME]; /* output buffer: the signal */ 
    uint8_t bitStream[10]; /* binary output of the encoder */
    // uint16_t outputBuffer[NB_PARAMETERS]; /* output buffer: an array containing the 15 parameters */
    if ( (fpInput = fopen(argv[1], "rb")) == NULL) {
        printf("%s - Error: can't open file  %s\n", argv[0], argv[1]);
        exit(-1);
    }
    /* create the output file(filename is the same than input file with the .out extension) */
    char *outputFile = malloc((strlen(filePrefix)+20)*sizeof(char));
    FILE *fpBinOutput;
    sprintf(outputFile, "%s.g729",filePrefix);
    if ( (fpBinOutput = fopen(outputFile, "wb")) == NULL) {
        printf("%s - Error: can't create file  %s\n", argv[0], outputFile);
        exit(-1);
    }
    
    /*** init of the tested bloc ***/
    bcg729EncoderChannelContextStruct *encoderChannelContext = initBcg729EncoderChannel(0);
    /*** initialisation complete ***/
/* increase LOOP_N to increase input length and perform a more accurate profiling or perf measurement */
#define LOOP_N 1
    int j;
    for (j=0; j
    /* perf measurement */
        printf("begin encoding\n");
        /*** loop over input file ***/
        while(1) /* infinite loop, escape condition is in the reading of data */
        {
            uint8_t bitStreamLength;
            /* read the input data until we have some */
            if (fread(inputBuffer, sizeof(int16_t), L_FRAME, fpInput) != L_FRAME) break;
            bcg729Encoder(encoderChannelContext, inputBuffer, bitStream, &bitStreamLength);
            fwrite(bitStream, sizeof(int8_t), 10, fpBinOutput);
        }
        /* perf measurement */
        printf("close input file\n");
        rewind(fpInput);
    }
    closeBcg729EncoderChannel(encoderChannelContext);
    free(outputFile);
    printf("finish encoding\n");
    exit (0);
}

test文件夹cmakelist.txt增加

include_directories( ${CMAKE_SOURCE_DIR}/../src )
include_directories( ${CMAKE_SOURCE_DIR}/../include )
link_directories(/usr/local/lib)

更多代码细节,参考[开源项目]