xref: /aosp_15_r20/external/speex/doc/sampledec.c (revision 28e138c64d234588b5cd2a8a403b584bd3036e4e)
1*28e138c6SAndroid Build Coastguard Worker #include <speex/speex.h>
2*28e138c6SAndroid Build Coastguard Worker #include <stdio.h>
3*28e138c6SAndroid Build Coastguard Worker 
4*28e138c6SAndroid Build Coastguard Worker /*The frame size in hardcoded for this sample code but it doesn't have to be*/
5*28e138c6SAndroid Build Coastguard Worker #define FRAME_SIZE 160
main(int argc,char ** argv)6*28e138c6SAndroid Build Coastguard Worker int main(int argc, char **argv)
7*28e138c6SAndroid Build Coastguard Worker {
8*28e138c6SAndroid Build Coastguard Worker    char *outFile;
9*28e138c6SAndroid Build Coastguard Worker    FILE *fout;
10*28e138c6SAndroid Build Coastguard Worker    /*Holds the audio that will be written to file (16 bits per sample)*/
11*28e138c6SAndroid Build Coastguard Worker    short out[FRAME_SIZE];
12*28e138c6SAndroid Build Coastguard Worker    /*Speex handle samples as float, so we need an array of floats*/
13*28e138c6SAndroid Build Coastguard Worker    float output[FRAME_SIZE];
14*28e138c6SAndroid Build Coastguard Worker    char cbits[200];
15*28e138c6SAndroid Build Coastguard Worker    int nbBytes;
16*28e138c6SAndroid Build Coastguard Worker    /*Holds the state of the decoder*/
17*28e138c6SAndroid Build Coastguard Worker    void *state;
18*28e138c6SAndroid Build Coastguard Worker    /*Holds bits so they can be read and written to by the Speex routines*/
19*28e138c6SAndroid Build Coastguard Worker    SpeexBits bits;
20*28e138c6SAndroid Build Coastguard Worker    int i, tmp;
21*28e138c6SAndroid Build Coastguard Worker 
22*28e138c6SAndroid Build Coastguard Worker    /*Create a new decoder state in narrowband mode*/
23*28e138c6SAndroid Build Coastguard Worker    state = speex_decoder_init(&speex_nb_mode);
24*28e138c6SAndroid Build Coastguard Worker 
25*28e138c6SAndroid Build Coastguard Worker    /*Set the perceptual enhancement on*/
26*28e138c6SAndroid Build Coastguard Worker    tmp=1;
27*28e138c6SAndroid Build Coastguard Worker    speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);
28*28e138c6SAndroid Build Coastguard Worker 
29*28e138c6SAndroid Build Coastguard Worker    outFile = argv[1];
30*28e138c6SAndroid Build Coastguard Worker    fout = fopen(outFile, "w");
31*28e138c6SAndroid Build Coastguard Worker 
32*28e138c6SAndroid Build Coastguard Worker    /*Initialization of the structure that holds the bits*/
33*28e138c6SAndroid Build Coastguard Worker    speex_bits_init(&bits);
34*28e138c6SAndroid Build Coastguard Worker    while (1)
35*28e138c6SAndroid Build Coastguard Worker    {
36*28e138c6SAndroid Build Coastguard Worker       /*Read the size encoded by sampleenc, this part will likely be
37*28e138c6SAndroid Build Coastguard Worker         different in your application*/
38*28e138c6SAndroid Build Coastguard Worker       fread(&nbBytes, sizeof(int), 1, stdin);
39*28e138c6SAndroid Build Coastguard Worker       fprintf (stderr, "nbBytes: %d\n", nbBytes);
40*28e138c6SAndroid Build Coastguard Worker       if (feof(stdin))
41*28e138c6SAndroid Build Coastguard Worker          break;
42*28e138c6SAndroid Build Coastguard Worker 
43*28e138c6SAndroid Build Coastguard Worker       /*Read the "packet" encoded by sampleenc*/
44*28e138c6SAndroid Build Coastguard Worker       fread(cbits, 1, nbBytes, stdin);
45*28e138c6SAndroid Build Coastguard Worker       /*Copy the data into the bit-stream struct*/
46*28e138c6SAndroid Build Coastguard Worker       speex_bits_read_from(&bits, cbits, nbBytes);
47*28e138c6SAndroid Build Coastguard Worker 
48*28e138c6SAndroid Build Coastguard Worker       /*Decode the data*/
49*28e138c6SAndroid Build Coastguard Worker       speex_decode(state, &bits, output);
50*28e138c6SAndroid Build Coastguard Worker 
51*28e138c6SAndroid Build Coastguard Worker       /*Copy from float to short (16 bits) for output*/
52*28e138c6SAndroid Build Coastguard Worker       for (i=0;i<FRAME_SIZE;i++)
53*28e138c6SAndroid Build Coastguard Worker          out[i]=output[i];
54*28e138c6SAndroid Build Coastguard Worker 
55*28e138c6SAndroid Build Coastguard Worker       /*Write the decoded audio to file*/
56*28e138c6SAndroid Build Coastguard Worker       fwrite(out, sizeof(short), FRAME_SIZE, fout);
57*28e138c6SAndroid Build Coastguard Worker    }
58*28e138c6SAndroid Build Coastguard Worker 
59*28e138c6SAndroid Build Coastguard Worker    /*Destroy the decoder state*/
60*28e138c6SAndroid Build Coastguard Worker    speex_decoder_destroy(state);
61*28e138c6SAndroid Build Coastguard Worker    /*Destroy the bit-stream truct*/
62*28e138c6SAndroid Build Coastguard Worker    speex_bits_destroy(&bits);
63*28e138c6SAndroid Build Coastguard Worker    fclose(fout);
64*28e138c6SAndroid Build Coastguard Worker    return 0;
65*28e138c6SAndroid Build Coastguard Worker }
66