这是一篇很早前发布在自己baidu博客上的文章,转载过来
这是speex-manual里面的第一个例子。它分为两部分,第一部分从音频设备读取声音然后编码保存成文件。

第二部分读取文件并解码,然后送入播放器播放。
注意的是其中对packet大小的处理。编码器必须告诉解码器编码时每一个包的长度才可以成功解码
This section shows sample code for encoding and decoding speech using the Speex API. The commands can be used to encode and decode a file by calling: % sampleenc in_file.sw | sampledec out_file.sw where both files are raw (no header) files encoded at 16 bits per sample (in the machine natural endianness). A.1 sampleenc.c sampleenc takes a raw 16 bits/sample file, encodes it and outputs a Speex stream to stdout. Note that the packing used is not compatible with that of speexenc/speexdec. Listing A.1: Source code for sampleenc

1 #include <speex/speex.h>
2 #include <stdio.h>
3
4 /*The frame size in hardcoded for this sample code but it doesn’t have to be*/
5 #define FRAME_SIZE 160
6 int main(int argc, char **argv)
7 {
8 char *inFile;
9 FILE *fin;
10 short in[FRAME_SIZE];
11 float input[FRAME_SIZE];
12 char cbits[200];
13 int nbBytes;
14 /*Holds the state of the encoder*/
15 void *state;
16 /*Holds bits so they can be read and written to by the Speex routines*/
17 SpeexBits bits;
18 int i, tmp;
19
20 /*Create a new encoder state in narrowband mode*/
21 state = speex_encoder_init(&speex_nb_mode);
22
23 /*Set the quality to 8 (15 kbps)*/
24 tmp=8;
25 speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);
26
27 inFile = argv[1];
28 fin = fopen(inFile, "r");
29
30 /*Initialization of the structure that holds the bits*/
31 speex_bits_init(&bits);
32 while (1)
33 {
34 /*Read a 16 bits/sample audio frame*/
35 fread(in, sizeof(short), FRAME_SIZE, fin);
36 if (feof(fin))
37 break;
38 /*Copy the 16 bits values to float so Speex can work on them*/
36A Sample code
39 for (i=0;i<FRAME_SIZE;i++)
40 input[i]=in[i];
41
42 /*Flush all the bits in the struct so we can encode a new frame*/
43 speex_bits_reset(&bits);
44
45 /*Encode the frame*/
46 speex_encode(state, input, &bits);
47 /*Copy the bits to an array of char that can be written*/
48 nbBytes = speex_bits_write(&bits, cbits, 200);
49
50 /*Write the size of the frame first. This is what sampledec expects but
51 it’s likely to be different in your own application*/
52 fwrite(&nbBytes, sizeof(int), 1, stdout);
53 /*Write the compressed data*/
54 fwrite(cbits, 1, nbBytes, stdout);
55
56 }
57
58 /*Destroy the encoder state*/
59 speex_encoder_destroy(state);
60 /*Destroy the bit-packing struct*/
61 speex_bits_destroy(&bits);
62 fclose(fin);
63 return 0;
64 }
1
A.2 sampledec.c
sampledec reads a Speex stream from stdin, decodes it and outputs it to a raw 16 bits/sample file. Note that the packing used
is not compatible with that of speexenc/speexdec.
Listing A.2: Source code for sampledec

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