2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-06-01 17:46:21 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* testG711.cpp : Defines the entry point for the console application.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
/* include API */
|
|
|
|
|
#include "g711_interface.h"
|
|
|
|
|
|
|
|
|
|
/* Runtime statistics */
|
|
|
|
|
#include <time.h>
|
2013-03-21 13:38:29 +00:00
|
|
|
#define CLOCKS_PER_SEC_G711 1000
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
/* function for reading audio data from PCM file */
|
Reformat existing code. There should be no functional effects.
This includes changes like:
* Attempt to break lines at better positions
* Use "override" in more places, don't use "virtual" with it
* Use {} where the body is more than one line
* Make declaration and definition arg names match
* Eliminate unused code
* EXPECT_EQ(expected, actual) (but use (actual, expected) for e.g. _GT)
* Correct #include order
* Use anonymous namespaces in preference to "static" for file-scoping
* Eliminate unnecessary casts
* Update reference code in comments of ARM assembly sources to match actual current C code
* Fix indenting to be more style-guide compliant
* Use arraysize() in more places
* Use bool instead of int for "boolean" values (0/1)
* Shorten and simplify code
* Spaces around operators
* 80 column limit
* Use const more consistently
* Space goes after '*' in type name, not before
* Remove unnecessary return values
* Use "(var == const)", not "(const == var)"
* Spelling
* Prefer true, typed constants to "enum hack" constants
* Avoid "virtual" on non-overridden functions
* ASSERT(x == y) -> ASSERT_EQ(y, x)
BUG=none
R=andrew@webrtc.org, asapersson@webrtc.org, henrika@webrtc.org, juberti@webrtc.org, kjellander@webrtc.org, kwiberg@webrtc.org
Review URL: https://codereview.webrtc.org/1172163004
Cr-Commit-Position: refs/heads/master@{#9420}
2015-06-11 14:31:38 -07:00
|
|
|
bool readframe(int16_t* data, FILE* inp, int length) {
|
|
|
|
|
short rlen = (short) fread(data, sizeof(int16_t), length, inp);
|
|
|
|
|
if (rlen >= length)
|
|
|
|
|
return false;
|
|
|
|
|
memset(data + rlen, 0, (length - rlen) * sizeof(int16_t));
|
|
|
|
|
return true;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-21 13:38:29 +00:00
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
|
char inname[80], outname[40], bitname[40];
|
|
|
|
|
FILE* inp;
|
|
|
|
|
FILE* outp;
|
|
|
|
|
FILE* bitp = NULL;
|
Reformat existing code. There should be no functional effects.
This includes changes like:
* Attempt to break lines at better positions
* Use "override" in more places, don't use "virtual" with it
* Use {} where the body is more than one line
* Make declaration and definition arg names match
* Eliminate unused code
* EXPECT_EQ(expected, actual) (but use (actual, expected) for e.g. _GT)
* Correct #include order
* Use anonymous namespaces in preference to "static" for file-scoping
* Eliminate unnecessary casts
* Update reference code in comments of ARM assembly sources to match actual current C code
* Fix indenting to be more style-guide compliant
* Use arraysize() in more places
* Use bool instead of int for "boolean" values (0/1)
* Shorten and simplify code
* Spaces around operators
* 80 column limit
* Use const more consistently
* Space goes after '*' in type name, not before
* Remove unnecessary return values
* Use "(var == const)", not "(const == var)"
* Spelling
* Prefer true, typed constants to "enum hack" constants
* Avoid "virtual" on non-overridden functions
* ASSERT(x == y) -> ASSERT_EQ(y, x)
BUG=none
R=andrew@webrtc.org, asapersson@webrtc.org, henrika@webrtc.org, juberti@webrtc.org, kjellander@webrtc.org, kwiberg@webrtc.org
Review URL: https://codereview.webrtc.org/1172163004
Cr-Commit-Position: refs/heads/master@{#9420}
2015-06-11 14:31:38 -07:00
|
|
|
int framecnt;
|
|
|
|
|
bool endfile;
|
2013-03-21 13:38:29 +00:00
|
|
|
|
|
|
|
|
int16_t framelength = 80;
|
|
|
|
|
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
/* Runtime statistics */
|
|
|
|
|
double starttime;
|
|
|
|
|
double runtime;
|
|
|
|
|
double length_file;
|
|
|
|
|
|
|
|
|
|
int16_t stream_len = 0;
|
|
|
|
|
int16_t shortdata[480];
|
|
|
|
|
int16_t decoded[480];
|
2015-02-09 12:55:48 +00:00
|
|
|
uint8_t streamdata[1000];
|
2013-03-21 13:38:29 +00:00
|
|
|
int16_t speechType[1];
|
|
|
|
|
char law[2];
|
|
|
|
|
char versionNumber[40];
|
|
|
|
|
|
|
|
|
|
/* handling wrong input arguments in the command line */
|
|
|
|
|
if ((argc != 5) && (argc != 6)) {
|
|
|
|
|
printf("\n\nWrong number of arguments or flag values.\n\n");
|
|
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf("\nG.711 test application\n\n");
|
|
|
|
|
printf("Usage:\n\n");
|
|
|
|
|
printf("./testG711.exe framelength law infile outfile \n\n");
|
|
|
|
|
printf("framelength: Framelength in samples.\n");
|
|
|
|
|
printf("law : Coding law, A och u.\n");
|
|
|
|
|
printf("infile : Normal speech input file\n");
|
|
|
|
|
printf("outfile : Speech output file\n\n");
|
|
|
|
|
printf("outbits : Output bitstream file [optional]\n\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Get version and print */
|
|
|
|
|
WebRtcG711_Version(versionNumber, 40);
|
|
|
|
|
|
|
|
|
|
printf("-----------------------------------\n");
|
|
|
|
|
printf("G.711 version: %s\n\n", versionNumber);
|
|
|
|
|
/* Get frame length */
|
|
|
|
|
framelength = atoi(argv[1]);
|
2015-06-10 21:15:38 -07:00
|
|
|
if (framelength < 0) {
|
|
|
|
|
printf(" G.711: Invalid framelength %d.\n", framelength);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2013-03-21 13:38:29 +00:00
|
|
|
|
|
|
|
|
/* Get compression law */
|
|
|
|
|
strcpy(law, argv[2]);
|
|
|
|
|
|
|
|
|
|
/* Get Input and Output files */
|
|
|
|
|
sscanf(argv[3], "%s", inname);
|
|
|
|
|
sscanf(argv[4], "%s", outname);
|
|
|
|
|
if (argc == 6) {
|
|
|
|
|
sscanf(argv[5], "%s", bitname);
|
|
|
|
|
if ((bitp = fopen(bitname, "wb")) == NULL) {
|
|
|
|
|
printf(" G.711: Cannot read file %s.\n", bitname);
|
|
|
|
|
exit(1);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2013-03-21 13:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((inp = fopen(inname, "rb")) == NULL) {
|
|
|
|
|
printf(" G.711: Cannot read file %s.\n", inname);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
if ((outp = fopen(outname, "wb")) == NULL) {
|
|
|
|
|
printf(" G.711: Cannot write file %s.\n", outname);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
printf("\nInput: %s\nOutput: %s\n", inname, outname);
|
|
|
|
|
if (argc == 6) {
|
|
|
|
|
printf("\nBitfile: %s\n", bitname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
starttime = clock() / (double) CLOCKS_PER_SEC_G711; /* Runtime statistics */
|
|
|
|
|
|
|
|
|
|
/* Initialize encoder and decoder */
|
|
|
|
|
framecnt = 0;
|
Reformat existing code. There should be no functional effects.
This includes changes like:
* Attempt to break lines at better positions
* Use "override" in more places, don't use "virtual" with it
* Use {} where the body is more than one line
* Make declaration and definition arg names match
* Eliminate unused code
* EXPECT_EQ(expected, actual) (but use (actual, expected) for e.g. _GT)
* Correct #include order
* Use anonymous namespaces in preference to "static" for file-scoping
* Eliminate unnecessary casts
* Update reference code in comments of ARM assembly sources to match actual current C code
* Fix indenting to be more style-guide compliant
* Use arraysize() in more places
* Use bool instead of int for "boolean" values (0/1)
* Shorten and simplify code
* Spaces around operators
* 80 column limit
* Use const more consistently
* Space goes after '*' in type name, not before
* Remove unnecessary return values
* Use "(var == const)", not "(const == var)"
* Spelling
* Prefer true, typed constants to "enum hack" constants
* Avoid "virtual" on non-overridden functions
* ASSERT(x == y) -> ASSERT_EQ(y, x)
BUG=none
R=andrew@webrtc.org, asapersson@webrtc.org, henrika@webrtc.org, juberti@webrtc.org, kjellander@webrtc.org, kwiberg@webrtc.org
Review URL: https://codereview.webrtc.org/1172163004
Cr-Commit-Position: refs/heads/master@{#9420}
2015-06-11 14:31:38 -07:00
|
|
|
endfile = false;
|
|
|
|
|
while (!endfile) {
|
2013-03-21 13:38:29 +00:00
|
|
|
framecnt++;
|
|
|
|
|
/* Read speech block */
|
|
|
|
|
endfile = readframe(shortdata, inp, framelength);
|
|
|
|
|
|
|
|
|
|
/* G.711 encoding */
|
|
|
|
|
if (!strcmp(law, "A")) {
|
|
|
|
|
/* A-law encoding */
|
2014-11-04 13:23:36 +00:00
|
|
|
stream_len = WebRtcG711_EncodeA(shortdata, framelength, streamdata);
|
2013-03-21 13:38:29 +00:00
|
|
|
if (argc == 6) {
|
|
|
|
|
/* Write bits to file */
|
|
|
|
|
if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) !=
|
|
|
|
|
static_cast<size_t>(stream_len)) {
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2013-03-21 13:38:29 +00:00
|
|
|
}
|
2014-11-04 13:23:36 +00:00
|
|
|
err = WebRtcG711_DecodeA(streamdata, stream_len, decoded,
|
2013-03-21 13:38:29 +00:00
|
|
|
speechType);
|
|
|
|
|
} else if (!strcmp(law, "u")) {
|
|
|
|
|
/* u-law encoding */
|
2014-11-04 13:23:36 +00:00
|
|
|
stream_len = WebRtcG711_EncodeU(shortdata, framelength, streamdata);
|
2013-03-21 13:38:29 +00:00
|
|
|
if (argc == 6) {
|
|
|
|
|
/* Write bits to file */
|
|
|
|
|
if (fwrite(streamdata, sizeof(unsigned char), stream_len, bitp) !=
|
|
|
|
|
static_cast<size_t>(stream_len)) {
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2013-03-21 13:38:29 +00:00
|
|
|
}
|
2014-11-04 13:23:36 +00:00
|
|
|
err = WebRtcG711_DecodeU(streamdata, stream_len, decoded, speechType);
|
2013-03-21 13:38:29 +00:00
|
|
|
} else {
|
|
|
|
|
printf("Wrong law mode\n");
|
|
|
|
|
exit(1);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2013-03-21 13:38:29 +00:00
|
|
|
if (stream_len < 0 || err < 0) {
|
|
|
|
|
/* exit if returned with error */
|
|
|
|
|
printf("Error in encoder/decoder\n");
|
|
|
|
|
} else {
|
|
|
|
|
/* Write coded speech to file */
|
|
|
|
|
if (fwrite(decoded, sizeof(short), framelength, outp) !=
|
|
|
|
|
static_cast<size_t>(framelength)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-03-21 13:38:29 +00:00
|
|
|
runtime = (double)(clock() / (double) CLOCKS_PER_SEC_G711 - starttime);
|
|
|
|
|
length_file = ((double) framecnt * (double) framelength / 8000);
|
|
|
|
|
printf("\n\nLength of speech file: %.1f s\n", length_file);
|
|
|
|
|
printf("Time to run G.711: %.2f s (%.2f %% of realtime)\n\n",
|
|
|
|
|
runtime,
|
|
|
|
|
(100 * runtime / length_file));
|
|
|
|
|
printf("---------------------END----------------------\n");
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-03-21 13:38:29 +00:00
|
|
|
fclose(inp);
|
|
|
|
|
fclose(outp);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-03-21 13:38:29 +00:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|