1 /*---------------------------------------------------------------------------*\
2 
3   FILE........: c2demo.c
4   AUTHOR......: David Rowe
5   DATE CREATED: 15/11/2010
6 
7   Encodes and decodes a file of raw speech samples using Codec 2.
8   Demonstrates use of Codec 2 function API.
9 
10   cd codec2/build_linux
11   ./demo/c2demo ../raw/hts1a.raw his1a_out.raw
12   aplay -f S16_LE hts1a_out.raw
13 
14 \*---------------------------------------------------------------------------*/
15 
16 /*
17   Copyright (C) 2010 David Rowe
18 
19   All rights reserved.
20 
21   This program is free software; you can redistribute it and/or modify
22   it under the terms of the GNU Lesser General Public License version 2.1, as
23   published by the Free Software Foundation.  This program is
24   distributed in the hope that it will be useful, but WITHOUT ANY
25   WARRANTY; without even the implied warranty of MERCHANTABILITY or
26   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
27   License for more details.
28 
29   You should have received a copy of the GNU Lesser General Public License
30   along with this program; if not, see <http://www.gnu.org/licenses/>.
31 */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include "codec2.h"
36 
main(int argc,char * argv[])37 int main(int argc, char *argv[])
38 {
39     struct CODEC2 *codec2;
40     FILE          *fin;
41     FILE          *fout;
42 
43     if (argc != 3) {
44         printf("usage: %s InputRawSpeechFile OutputRawSpeechFile\n", argv[0]);
45         exit(1);
46     }
47 
48     if ( (fin = fopen(argv[1],"rb")) == NULL ) {
49         fprintf(stderr, "Error opening input speech file: %s\n", argv[1]);
50         exit(1);
51     }
52 
53     if ( (fout = fopen(argv[2],"wb")) == NULL ) {
54         fprintf(stderr, "Error opening output speech file: %s\n", argv[2]);
55          exit(1);
56     }
57 
58     /* Note only one set of Codec 2 states is required for an encoder
59        and decoder pair. */
60     codec2 = codec2_create(CODEC2_MODE_1300);
61     size_t nsam = codec2_samples_per_frame(codec2);
62     short speech_samples[nsam];
63     /* Bits from the encoder are packed into bytes */
64     unsigned char compressed_bytes[codec2_bytes_per_frame(codec2)];
65 
66     while(fread(speech_samples, sizeof(short), nsam, fin) == nsam) {
67         codec2_encode(codec2, compressed_bytes, speech_samples);
68         codec2_decode(codec2, speech_samples, compressed_bytes);
69         fwrite(speech_samples, sizeof(short), nsam, fout);
70     }
71 
72     codec2_destroy(codec2);
73     fclose(fin);
74     fclose(fout);
75 
76     return 0;
77 }
78