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 "webrtc/modules/audio_coding/codecs/g711/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 bool readframe(int16_t* data, FILE* inp, size_t length) {
     28   size_t rlen = fread(data, sizeof(int16_t), length, inp);
     29   if (rlen >= length)
     30     return false;
     31   memset(data + rlen, 0, (length - rlen) * sizeof(int16_t));
     32   return true;
     33 }
     34 
     35 int main(int argc, char* argv[]) {
     36   char inname[80], outname[40], bitname[40];
     37   FILE* inp;
     38   FILE* outp;
     39   FILE* bitp = NULL;
     40   int framecnt;
     41   bool endfile;
     42 
     43   size_t framelength = 80;
     44 
     45   /* Runtime statistics */
     46   double starttime;
     47   double runtime;
     48   double length_file;
     49 
     50   size_t stream_len = 0;
     51   int16_t shortdata[480];
     52   int16_t decoded[480];
     53   uint8_t streamdata[1000];
     54   int16_t speechType[1];
     55   char law[2];
     56   char versionNumber[40];
     57 
     58   /* handling wrong input arguments in the command line */
     59   if ((argc != 5) && (argc != 6)) {
     60     printf("\n\nWrong number of arguments or flag values.\n\n");
     61 
     62     printf("\n");
     63     printf("\nG.711 test application\n\n");
     64     printf("Usage:\n\n");
     65     printf("./testG711.exe framelength law infile outfile \n\n");
     66     printf("framelength: Framelength in samples.\n");
     67     printf("law        : Coding law, A och u.\n");
     68     printf("infile     : Normal speech input file\n");
     69     printf("outfile    : Speech output file\n\n");
     70     printf("outbits    : Output bitstream file [optional]\n\n");
     71     exit(0);
     72 
     73   }
     74 
     75   /* Get version and print */
     76   WebRtcG711_Version(versionNumber, 40);
     77 
     78   printf("-----------------------------------\n");
     79   printf("G.711 version: %s\n\n", versionNumber);
     80   /* Get frame length */
     81   int framelength_int = atoi(argv[1]);
     82   if (framelength_int < 0) {
     83       printf("  G.722: Invalid framelength %d.\n", framelength_int);
     84       exit(1);
     85   }
     86   framelength = static_cast<size_t>(framelength_int);
     87 
     88   /* Get compression law */
     89   strcpy(law, argv[2]);
     90 
     91   /* Get Input and Output files */
     92   sscanf(argv[3], "%s", inname);
     93   sscanf(argv[4], "%s", outname);
     94   if (argc == 6) {
     95     sscanf(argv[5], "%s", bitname);
     96     if ((bitp = fopen(bitname, "wb")) == NULL) {
     97       printf("  G.711: Cannot read file %s.\n", bitname);
     98       exit(1);
     99     }
    100   }
    101 
    102   if ((inp = fopen(inname, "rb")) == NULL) {
    103     printf("  G.711: Cannot read file %s.\n", inname);
    104     exit(1);
    105   }
    106   if ((outp = fopen(outname, "wb")) == NULL) {
    107     printf("  G.711: Cannot write file %s.\n", outname);
    108     exit(1);
    109   }
    110   printf("\nInput:  %s\nOutput: %s\n", inname, outname);
    111   if (argc == 6) {
    112     printf("\nBitfile:  %s\n", bitname);
    113   }
    114 
    115   starttime = clock() / (double) CLOCKS_PER_SEC_G711; /* Runtime statistics */
    116 
    117   /* Initialize encoder and decoder */
    118   framecnt = 0;
    119   endfile = false;
    120   while (!endfile) {
    121     framecnt++;
    122     /* Read speech block */
    123     endfile = readframe(shortdata, inp, framelength);
    124 
    125     /* G.711 encoding */
    126     if (!strcmp(law, "A")) {
    127       /* A-law encoding */
    128       stream_len = WebRtcG711_EncodeA(shortdata, framelength, streamdata);
    129       if (argc == 6) {
    130         /* Write bits to file */
    131         if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) !=
    132             stream_len) {
    133           return -1;
    134         }
    135       }
    136       WebRtcG711_DecodeA(streamdata, stream_len, decoded, speechType);
    137     } else if (!strcmp(law, "u")) {
    138       /* u-law encoding */
    139       stream_len = WebRtcG711_EncodeU(shortdata, framelength, streamdata);
    140       if (argc == 6) {
    141         /* Write bits to file */
    142         if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) !=
    143             stream_len) {
    144           return -1;
    145         }
    146       }
    147       WebRtcG711_DecodeU(streamdata, stream_len, decoded, speechType);
    148     } else {
    149       printf("Wrong law mode\n");
    150       exit(1);
    151     }
    152     /* Write coded speech to file */
    153     if (fwrite(decoded, sizeof(short), framelength, outp) != framelength) {
    154       return -1;
    155     }
    156   }
    157 
    158   runtime = (double)(clock() / (double) CLOCKS_PER_SEC_G711 - starttime);
    159   length_file = ((double) framecnt * (double) framelength / 8000);
    160   printf("\n\nLength of speech file: %.1f s\n", length_file);
    161   printf("Time to run G.711:      %.2f s (%.2f %% of realtime)\n\n",
    162          runtime,
    163          (100 * runtime / length_file));
    164   printf("---------------------END----------------------\n");
    165 
    166   fclose(inp);
    167   fclose(outp);
    168 
    169   return 0;
    170 }
    171