speex encoder decode

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013420428/article/details/82900794

speex encoder

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <getopt.h>

#include <speex/speex.h>
#include <speex/speex_header.h>
#include <speex/speex_stereo.h>

/*The frame size in hardcoded for this sample code but it doesn’t have to be*/
#define FRAME_SIZE 160
int main(int argc, char **argv)
{
    char *inFile;
    FILE *fin;
    short in[FRAME_SIZE];
    float input[FRAME_SIZE];
    char cbits[200];
    int nbBytes;

    if (argc < 2)
    {
        printf("Usge: speexenc in.wav >> out.wav\n");
        exit(0);
    }

    /*Holds the state of the encoder*/
    void *state;
    /*Holds bits so they can be read and written to by the Speex routines*/
    SpeexBits bits;
    int i, tmp;

    /*Create a new encoder state in narrowband mode*/
    state = speex_encoder_init(&speex_nb_mode);

    /*Set the quality to 8 (15 kbps)*/
    tmp = 8;
    speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);
    
    inFile = argv[1];
    fin = fopen(inFile, "r");
    if(!fin)
    {
        printf("open file error.\n");
    }

    /*Initialization of the structure that holds the bits*/
    speex_bits_init(&bits);

    while (1)
    {
        /*Read a 16 bits/sample audio frame*/
        fread(in, sizeof(short), FRAME_SIZE, fin);
        if(feof(fin))
            break;

        /*Copy the 16 bits values to float so Speex can work on them*/
        for (int i=0; i<FRAME_SIZE; i++)
        {
            input[i] = in[i];
        }

        /*Flush all the bits in the struct so we can encode a new frame*/
        speex_bits_reset(&bits);
        
        /*Encode the frame*/
        speex_encode(state, input, &bits);

        /*Copy the bits to an array of char that can be written*/
        nbBytes = speex_bits_write(&bits, cbits, 200);

        /*Write the size of the frame first. This is what sampledec expects but
        it’s likely to be different in your own application*/
        fwrite(&nbBytes, sizeof(int), 1, stdout);
        /*Write the compressed data*/
        fwrite(cbits, 1, nbBytes, stdout);
    }

    /*Destroy the encoder state*/
    speex_encoder_destroy(state);
    /*Destroy the bit-packing struct*/
    speex_bits_destroy(&bits);
    fclose(fin);
    return 0;

}

gcc speexenc.c -o speexenc -lspeex
./speexenc in.wav >> out.wav

speex decoder

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <getopt.h>

#include <speex/speex.h>
#include <speex/speex_header.h>
#include <speex/speex_stereo.h>

/*The frame size in hardcoded for this sample code but it doesn’t have to be*/
#define FRAME_SIZE 160
int main(int argc, char **argv)
{
    char *inFile;
    FILE *fin;
    short in[FRAME_SIZE];
    float input[FRAME_SIZE];
    char cbits[200];
    int nbBytes;

    if (argc < 2)
    {
        printf("Usge: speexenc in.wav >> out.wav\n");
        exit(0);
    }

    /*Holds the state of the encoder*/
    void *state;
    /*Holds bits so they can be read and written to by the Speex routines*/
    SpeexBits bits;
    int i, tmp;

    /*Create a new encoder state in narrowband mode*/
    state = speex_encoder_init(&speex_nb_mode);

    /*Set the quality to 8 (15 kbps)*/
    tmp = 8;
    speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);
    
    inFile = argv[1];
    fin = fopen(inFile, "r");
    if(!fin)
    {
        printf("open file error.\n");
    }

    /*Initialization of the structure that holds the bits*/
    speex_bits_init(&bits);

    while (1)
    {
        /*Read a 16 bits/sample audio frame*/
        fread(in, sizeof(short), FRAME_SIZE, fin);
        if(feof(fin))
            break;

        /*Copy the 16 bits values to float so Speex can work on them*/
        for (int i=0; i<FRAME_SIZE; i++)
        {
            input[i] = in[i];
        }

        /*Flush all the bits in the struct so we can encode a new frame*/
        speex_bits_reset(&bits);
        
        /*Encode the frame*/
        speex_encode(state, input, &bits);

        /*Copy the bits to an array of char that can be written*/
        nbBytes = speex_bits_write(&bits, cbits, 200);

        /*Write the size of the frame first. This is what sampledec expects but
        it’s likely to be different in your own application*/
        fwrite(&nbBytes, sizeof(int), 1, stdout);
        /*Write the compressed data*/
        fwrite(cbits, 1, nbBytes, stdout);
    }

    /*Destroy the encoder state*/
    speex_encoder_destroy(state);
    /*Destroy the bit-packing struct*/
    speex_bits_destroy(&bits);
    fclose(fin);
    return 0;

}

gcc speexdnc.c -o speexdnc -lspeex
./speexdnc new.wav out.wav

猜你喜欢

转载自blog.csdn.net/u013420428/article/details/82900794