2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
12 * testG711.cpp : Defines the entry point for the console application.
20 #include "g711_interface.h"
22 /* Runtime statistics */
24 #define CLOCKS_PER_SEC_G711 1000
26 /* function for reading audio data from PCM file */
27 int readframe(int16_t* data, FILE* inp, int length) {
29 short k, rlen, status = 0;
31 rlen = (short) fread(data, sizeof(int16_t), length, inp);
33 for (k = rlen; k < length; k++)
41 int main(int argc, char* argv[]) {
42 char inname[80], outname[40], bitname[40];
46 int framecnt, endfile;
48 int16_t framelength = 80;
52 /* Runtime statistics */
57 int16_t stream_len = 0;
58 int16_t shortdata[480];
60 int16_t streamdata[500];
61 int16_t speechType[1];
63 char versionNumber[40];
65 /* handling wrong input arguments in the command line */
66 if ((argc != 5) && (argc != 6)) {
67 printf("\n\nWrong number of arguments or flag values.\n\n");
70 printf("\nG.711 test application\n\n");
72 printf("./testG711.exe framelength law infile outfile \n\n");
73 printf("framelength: Framelength in samples.\n");
74 printf("law : Coding law, A och u.\n");
75 printf("infile : Normal speech input file\n");
76 printf("outfile : Speech output file\n\n");
77 printf("outbits : Output bitstream file [optional]\n\n");
82 /* Get version and print */
83 WebRtcG711_Version(versionNumber, 40);
85 printf("-----------------------------------\n");
86 printf("G.711 version: %s\n\n", versionNumber);
87 /* Get frame length */
88 framelength = atoi(argv[1]);
90 /* Get compression law */
93 /* Get Input and Output files */
94 sscanf(argv[3], "%s", inname);
95 sscanf(argv[4], "%s", outname);
97 sscanf(argv[5], "%s", bitname);
98 if ((bitp = fopen(bitname, "wb")) == NULL) {
99 printf(" G.711: Cannot read file %s.\n", bitname);
104 if ((inp = fopen(inname, "rb")) == NULL) {
105 printf(" G.711: Cannot read file %s.\n", inname);
108 if ((outp = fopen(outname, "wb")) == NULL) {
109 printf(" G.711: Cannot write file %s.\n", outname);
112 printf("\nInput: %s\nOutput: %s\n", inname, outname);
114 printf("\nBitfile: %s\n", bitname);
117 starttime = clock() / (double) CLOCKS_PER_SEC_G711; /* Runtime statistics */
119 /* Initialize encoder and decoder */
122 while (endfile == 0) {
124 /* Read speech block */
125 endfile = readframe(shortdata, inp, framelength);
128 if (!strcmp(law, "A")) {
130 stream_len = WebRtcG711_EncodeA(shortdata, framelength, streamdata);
132 /* Write bits to file */
133 if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) !=
134 static_cast<size_t>(stream_len)) {
138 err = WebRtcG711_DecodeA(streamdata, stream_len, decoded,
140 } else if (!strcmp(law, "u")) {
142 stream_len = WebRtcG711_EncodeU(shortdata, framelength, streamdata);
144 /* Write bits to file */
145 if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) !=
146 static_cast<size_t>(stream_len)) {
150 err = WebRtcG711_DecodeU(streamdata, stream_len, decoded, speechType);
152 printf("Wrong law mode\n");
155 if (stream_len < 0 || err < 0) {
156 /* exit if returned with error */
157 printf("Error in encoder/decoder\n");
159 /* Write coded speech to file */
160 if (fwrite(decoded, sizeof(short), framelength, outp) !=
161 static_cast<size_t>(framelength)) {
167 runtime = (double)(clock() / (double) CLOCKS_PER_SEC_G711 - starttime);
168 length_file = ((double) framecnt * (double) framelength / 8000);
169 printf("\n\nLength of speech file: %.1f s\n", length_file);
170 printf("Time to run G.711: %.2f s (%.2f %% of realtime)\n\n",
172 (100 * runtime / length_file));
173 printf("---------------------END----------------------\n");