tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

testG711.cc (4909B)


      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 <cstdint>
     16 #include <cstdio>
     17 #include <cstdlib>
     18 #include <cstring>
     19 #include <ctime>
     20 
     21 #include "modules/audio_coding/codecs/g711/g711_interface.h"
     22 
     23 /* Runtime statistics */
     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 = nullptr;
     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  /* Get version and print */
     75  WebRtcG711_Version(versionNumber, 40);
     76 
     77  printf("-----------------------------------\n");
     78  printf("G.711 version: %s\n\n", versionNumber);
     79  /* Get frame length */
     80  int framelength_int = atoi(argv[1]);
     81  if (framelength_int < 0) {
     82    printf("  G.722: Invalid framelength %d.\n", framelength_int);
     83    exit(1);
     84  }
     85  framelength = static_cast<size_t>(framelength_int);
     86 
     87  /* Get compression law */
     88  strcpy(law, argv[2]);
     89 
     90  /* Get Input and Output files */
     91  sscanf(argv[3], "%s", inname);
     92  sscanf(argv[4], "%s", outname);
     93  if (argc == 6) {
     94    sscanf(argv[5], "%s", bitname);
     95    if ((bitp = fopen(bitname, "wb")) == nullptr) {
     96      printf("  G.711: Cannot read file %s.\n", bitname);
     97      exit(1);
     98    }
     99  }
    100 
    101  if ((inp = fopen(inname, "rb")) == nullptr) {
    102    printf("  G.711: Cannot read file %s.\n", inname);
    103    exit(1);
    104  }
    105  if ((outp = fopen(outname, "wb")) == nullptr) {
    106    printf("  G.711: Cannot write file %s.\n", outname);
    107    exit(1);
    108  }
    109  printf("\nInput:  %s\nOutput: %s\n", inname, outname);
    110  if (argc == 6) {
    111    printf("\nBitfile:  %s\n", bitname);
    112  }
    113 
    114  starttime = clock() / (double)CLOCKS_PER_SEC_G711; /* Runtime statistics */
    115 
    116  /* Initialize encoder and decoder */
    117  framecnt = 0;
    118  endfile = false;
    119  while (!endfile) {
    120    framecnt++;
    121    /* Read speech block */
    122    endfile = readframe(shortdata, inp, framelength);
    123 
    124    /* G.711 encoding */
    125    if (!strcmp(law, "A")) {
    126      /* A-law encoding */
    127      stream_len = WebRtcG711_EncodeA(shortdata, framelength, streamdata);
    128      if (argc == 6) {
    129        /* Write bits to file */
    130        if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) !=
    131            stream_len) {
    132          return -1;
    133        }
    134      }
    135      WebRtcG711_DecodeA(streamdata, stream_len, decoded, speechType);
    136    } else if (!strcmp(law, "u")) {
    137      /* u-law encoding */
    138      stream_len = WebRtcG711_EncodeU(shortdata, framelength, streamdata);
    139      if (argc == 6) {
    140        /* Write bits to file */
    141        if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) !=
    142            stream_len) {
    143          return -1;
    144        }
    145      }
    146      WebRtcG711_DecodeU(streamdata, stream_len, decoded, speechType);
    147    } else {
    148      printf("Wrong law mode\n");
    149      exit(1);
    150    }
    151    /* Write coded speech to file */
    152    if (fwrite(decoded, sizeof(short), framelength, outp) != framelength) {
    153      return -1;
    154    }
    155  }
    156 
    157  runtime = (double)(clock() / (double)CLOCKS_PER_SEC_G711 - starttime);
    158  length_file = ((double)framecnt * (double)framelength / 8000);
    159  printf("\n\nLength of speech file: %.1f s\n", length_file);
    160  printf("Time to run G.711:      %.2f s (%.2f %% of realtime)\n\n", runtime,
    161         (100 * runtime / length_file));
    162  printf("---------------------END----------------------\n");
    163 
    164  fclose(inp);
    165  fclose(outp);
    166 
    167  return 0;
    168 }