Home | History | Annotate | Download | only in test
      1 /*
      2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
      3  *
      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.
      9  */
     10 
     11 /*
     12  * testG711.cpp : Defines the entry point for the console application.
     13  */
     14 
     15 #include <stdio.h>
     16 #include <stdlib.h>
     17 #include <string.h>
     18 
     19 /* include API */
     20 #include "g711_interface.h"
     21 
     22 /* Runtime statistics */
     23 #include <time.h>
     24 #define CLOCKS_PER_SEC_G711 1000
     25 
     26 /* function for reading audio data from PCM file */
     27 int readframe(int16_t* data, FILE* inp, int length) {
     28 
     29   short k, rlen, status = 0;
     30 
     31   rlen = (short) fread(data, sizeof(int16_t), length, inp);
     32   if (rlen < length) {
     33     for (k = rlen; k < length; k++)
     34       data[k] = 0;
     35     status = 1;
     36   }
     37 
     38   return status;
     39 }
     40 
     41 int main(int argc, char* argv[]) {
     42   char inname[80], outname[40], bitname[40];
     43   FILE* inp;
     44   FILE* outp;
     45   FILE* bitp = NULL;
     46   int framecnt, endfile;
     47 
     48   int16_t framelength = 80;
     49 
     50   int err;
     51 
     52   /* Runtime statistics */
     53   double starttime;
     54   double runtime;
     55   double length_file;
     56 
     57   int16_t stream_len = 0;
     58   int16_t shortdata[480];
     59   int16_t decoded[480];
     60   int16_t streamdata[500];
     61   int16_t speechType[1];
     62   char law[2];
     63   char versionNumber[40];
     64 
     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");
     68 
     69     printf("\n");
     70     printf("\nG.711 test application\n\n");
     71     printf("Usage:\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");
     78     exit(0);
     79 
     80   }
     81 
     82   /* Get version and print */
     83   WebRtcG711_Version(versionNumber, 40);
     84 
     85   printf("-----------------------------------\n");
     86   printf("G.711 version: %s\n\n", versionNumber);
     87   /* Get frame length */
     88   framelength = atoi(argv[1]);
     89 
     90   /* Get compression law */
     91   strcpy(law, argv[2]);
     92 
     93   /* Get Input and Output files */
     94   sscanf(argv[3], "%s", inname);
     95   sscanf(argv[4], "%s", outname);
     96   if (argc == 6) {
     97     sscanf(argv[5], "%s", bitname);
     98     if ((bitp = fopen(bitname, "wb")) == NULL) {
     99       printf("  G.711: Cannot read file %s.\n", bitname);
    100       exit(1);
    101     }
    102   }
    103 
    104   if ((inp = fopen(inname, "rb")) == NULL) {
    105     printf("  G.711: Cannot read file %s.\n", inname);
    106     exit(1);
    107   }
    108   if ((outp = fopen(outname, "wb")) == NULL) {
    109     printf("  G.711: Cannot write file %s.\n", outname);
    110     exit(1);
    111   }
    112   printf("\nInput:  %s\nOutput: %s\n", inname, outname);
    113   if (argc == 6) {
    114     printf("\nBitfile:  %s\n", bitname);
    115   }
    116 
    117   starttime = clock() / (double) CLOCKS_PER_SEC_G711; /* Runtime statistics */
    118 
    119   /* Initialize encoder and decoder */
    120   framecnt = 0;
    121   endfile = 0;
    122   while (endfile == 0) {
    123     framecnt++;
    124     /* Read speech block */
    125     endfile = readframe(shortdata, inp, framelength);
    126 
    127     /* G.711 encoding */
    128     if (!strcmp(law, "A")) {
    129       /* A-law encoding */
    130       stream_len = WebRtcG711_EncodeA(NULL, shortdata, framelength, streamdata);
    131       if (argc == 6) {
    132         /* Write bits to file */
    133         if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) !=
    134             static_cast<size_t>(stream_len)) {
    135           return -1;
    136         }
    137       }
    138       err = WebRtcG711_DecodeA(NULL, streamdata, stream_len, decoded,
    139                                speechType);
    140     } else if (!strcmp(law, "u")) {
    141       /* u-law encoding */
    142       stream_len = WebRtcG711_EncodeU(NULL, shortdata, framelength, streamdata);
    143       if (argc == 6) {
    144         /* Write bits to file */
    145         if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) !=
    146             static_cast<size_t>(stream_len)) {
    147           return -1;
    148         }
    149       }
    150       err = WebRtcG711_DecodeU(NULL, streamdata, stream_len, decoded,
    151                                speechType);
    152     } else {
    153       printf("Wrong law mode\n");
    154       exit(1);
    155     }
    156     if (stream_len < 0 || err < 0) {
    157       /* exit if returned with error */
    158       printf("Error in encoder/decoder\n");
    159     } else {
    160       /* Write coded speech to file */
    161       if (fwrite(decoded, sizeof(short), framelength, outp) !=
    162           static_cast<size_t>(framelength)) {
    163         return -1;
    164       }
    165     }
    166   }
    167 
    168   runtime = (double)(clock() / (double) CLOCKS_PER_SEC_G711 - starttime);
    169   length_file = ((double) framecnt * (double) framelength / 8000);
    170   printf("\n\nLength of speech file: %.1f s\n", length_file);
    171   printf("Time to run G.711:      %.2f s (%.2f %% of realtime)\n\n",
    172          runtime,
    173          (100 * runtime / length_file));
    174   printf("---------------------END----------------------\n");
    175 
    176   fclose(inp);
    177   fclose(outp);
    178 
    179   return 0;
    180 }
    181