📄 rgl_write_v1.c
字号:
// rgl_write.c
// Copyright (c) 2002 Cisco Systems Inc.
// Author: M. A. Ramalho, Cisco Systems, Sarasota, FL 34238
// Last Revision: December 12, 2002
//
// Expects 5 or 6 arguments where:
//
// 1) where mu_or_a = 0 for A-law encoded random output
// 1 for mu-law encoded random output
// 2 for A-law encoded sinusiodal output
// 3 for mu-law encoded sinusoidal output
//
// 2) 1 <= frame_size <= 2000: frame size of data generated
//
// 3) min_value and max_value are integers in the range 0 to 255
//
// 4) min_value <= max_value
//
// 5) 1 <= num_of_frames <= 9999: is the number of frames of size
// "frame_size" produced
//
// 6) 0< frequency < 4000; frequency (in Hz) if sinusoidal output
// is desired (signaled by mu_or_a = 2 or mu_or_a = 3).
//
// This program generates pseudo-random G711 mu-law or A-law encoded
// data for subsequent use by the "rgl_encode.c" RGL codec program.
//
// The specification of the number of data points per frame to be
// produced is specified by the "frame_size" parameter
// (1 <= frame_size <= 2000).
//
// This program assumes the quantization levels are specified by
// parameters "min_value" and "max_value" are from "0" (most neg) to
// "255" (most positive). The value of "max_value" must be greater
// than or equal to the value "min_value".
//
// This program generates pseudo random or sinusoida input data with
// data points ranging from quantization level "min_value" to
// "max_value". For the random data case, the program verifies that
// "min_value" and "max_value" are represented in the pseudo-random
// data points generated. If "min_vlaue" is not represented, a data
// point at "min_value" is inserted approximately 1/3 through the
// data frame. Likewise, if "max_value" is not represented a data
// point at "max_value" is inserted approximately 2/3 through the
// data frame. For the sinusiodal data case, a sinusoid of frequency
// "frequency" (in Hz) is created with random initial phase.
//
// After the pseudo-random or sinusoida data is created in the
// required quantization level range, it is translated to its
// equivalent G.711 mu-law or A-law encoded value.
//
// For A-law output (i.e., parameter mu_or_a = 0), the program writes
// data to file "data_XXXX_YYYY.al" where XXXX equals the frame size
// and YYYY equals the number of frames of size frame_size produced
// (e.g., data_80_1.al for 80 byte frame_size and num_of_frames = 1
// and data_2000_2.al for 2000 byte frame_size and num_of_frames =2).
//
// For mu-law output (i.e., mu_or_a = 1), the program writes data to
// file "data_XXXX_YYYY.mu" in similar format to the A-law case above.
//
// Example: "rgl_write 0 80 120 135 1" would produce 80 pseudo-random
// samples in the quantization range 120 through 135. At least one
// data point will have value 120 (before A-law translation). At least
// one data point will have value 135 (before A-law translation). The
// resulting pseudo-random data frame is A-law encoded and written to
// "data_80_1.al". "rgl_write 0 80 120 135 2" would produce one file
// (named "data_80_2.al") that contained two such frames, containing
// a total of 160 bytes of pseudo random A-law encoded data. As yet
// another example "rgl_write 3 80 120 139 2 123.4" would produce
// two, 80-byte long frames of mu-law encoded sinusoidal data
// with samples between 120 & 139 (inclusive) based on a 123.4 Hz
// sinusoid sampled at 8000 Hz (with random initial phase). The phase
// is continuous between frames if more than one frame is generated.
//
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define error(msg) { fprintf (stderr, "%s\n", msg); exit (1); }
#define max_frame_size 2000
#define min_data_value 0
#define max_data_value 255
#define true 1
#define false 0
typedef unsigned char uint8;
// Main Program begins here
int main (int argc, char *argv[])
{
char name_buffer[80];
float scale_rand, f_temp, mean_value, amplitude;
double freq, init_phase, pi = 3.1415926535;
unsigned long int k=0;
int i, frame_size, num_written, num_of_frames;
int min_input, max_input, mu_or_a, max_flag, min_flag;
uint8 u_temp, index_min, index_max, output_frame[max_frame_size];
FILE *output_file;
// check for the correct number of arguments
switch (argc) {
case 6:
mu_or_a = atoi(argv [1]);
if ( (mu_or_a < 0) || (mu_or_a > 3) ) {
error ("mu_or_a parameter must be zero, one, two or three");
}
frame_size = atoi(argv [2]);
if ( (frame_size > max_frame_size) || (frame_size < 1) ) {
error ("frame size must be between 1 and 2000 bytes");
}
min_input = atoi (argv [3]);
if ( (min_input > max_data_value) || (min_input < min_data_value) ) {
error ("minimum value input must be between 0 and 255");
}
max_input = atoi(argv [4]);
if ( (max_input > max_data_value) || (max_input < min_data_value) ) {
error ("minimum value input must be between 0 and 255");
}
if ( min_input == max_input ) {
(void) fprintf(stdout, "Warning: minimum value equals max value! \n");
(void) fprintf(stdout,
"Warning: Writing file with identical values for each sample.\n");
}
if ( min_input > max_input ) {
error ("max_input must be greater than or equal to min_input");
}
num_of_frames = atoi(argv [5]);
if ( (num_of_frames < 1) || (num_of_frames >9999) ) {
error (" number of frames must be between 1 and 9999");
}
break;
case 7:
mu_or_a = atoi(argv [1]);
if ( (mu_or_a < 0) || (mu_or_a > 3) ) {
error ("mu_or_a parameter must be zero, one, two or three");
}
frame_size = atoi(argv [2]);
if ( (frame_size > max_frame_size) || (frame_size < 1) ) {
error ("frame size must be between 1 and 2000 bytes");
}
min_input = atoi (argv [3]);
if ( (min_input > max_data_value) || (min_input < min_data_value) ) {
error ("minimum value input must be between 0 and 255");
}
max_input = atoi(argv [4]);
if ( (max_input > max_data_value) || (max_input < min_data_value) ) {
error ("minimum value input must be between 0 and 255");
}
if ( min_input == max_input ) {
(void) fprintf(stdout, "Warning: minimum value equals max value! \n");
(void) fprintf(stdout,
"Warning: Writing file with identical values for each sample.\n");
}
if ( min_input > max_input ) {
error ("max_input must be greater than or equal to min_input");
}
num_of_frames = atoi(argv [5]);
if ( (num_of_frames < 1) || (num_of_frames >9999) ) {
error (" number of frames must be between 1 and 9999");
}
freq = (double) atof(argv[6]);
if ( (freq < 0) || (freq > 4000) ) {
error (" frequency must be between 0 and 4000");
}
break;
default:
error ("Error: Program needs 5 or 6 arguments. Program terminating.");
break;
}
// Build output file name
// Build base file name
i = 0;
name_buffer [i++] = '.';
if (mu_or_a == 0 || mu_or_a == 2) {
sprintf(name_buffer, "data_%d_%d.al", frame_size, num_of_frames);
}
else {
sprintf(name_buffer, "data_%d_%d.mu", frame_size, num_of_frames);
}
// Open output file
output_file = fopen (name_buffer, "wb");
if (output_file == NULL) error ("can't open output file for writing");
// need to call rand() before generating data (otherwise rand() always
// returns its first value near min_input)
(void) rand();
//random initial phase
init_phase = 2 * pi * (((double) rand())/((double) RAND_MAX));
// Create a frame size of random mu-law data
while (num_of_frames > 0){
if (mu_or_a == 2 || mu_or_a ==3)
{ /* create sinusoidal data frame */
max_flag = false; min_flag = false; /* Initialize flags */
mean_value=(((float) (max_input - min_input)) / 2)+(float) min_input;
amplitude = ((float) (max_input - min_input + 0.9999)) / 2;
for(i=0; i< frame_size; i++){
f_temp = amplitude * (float) sin ( (2*pi*((double) k)*freq /8000)
+ init_phase);
u_temp = (uint8)(f_temp + mean_value + 0.5);
output_frame[i] = u_temp;
k++; /*index for time for continuity between frame boundaries */
if (u_temp == min_input) min_flag = true;
if (u_temp == max_input) max_flag = true;
}
if (min_flag != true || max_flag != true)
{
(void) fprintf(stdout,
"Error: freqency is too high or low to exercise min_input \n");
error (" and/or max_input - Program terminating");
}
}
else /* else create a random data frame */
{
// Initialize flags
max_flag = false; min_flag = false;
// Create random values in the output array between min_value and
// min_value. Check (through use of min_flag and max_flag) that
// min_value and max_value are represented. If not, logic after this
// inserts them in the output array.
scale_rand = (float)
(((float)(max_input-min_input+0.9999999))/((float) RAND_MAX));
for(i=0; i< frame_size; i++){
f_temp = (float) (scale_rand * ((float) rand()));
u_temp = (uint8) (((uint8)(f_temp)) + min_input);
output_frame[i] = u_temp;
if (u_temp == min_input) min_flag = true;
if (u_temp == max_input) max_flag = true;
}
// If min_value is not represented in the output array (of size
// frame_size), then set a point approximately 1/3 through the array
// to min_input.
if (min_flag == false){
index_min = (int) (((float) frame_size)/3);
// make sure that the replaced value isn't the only occurance of that value
if (output_frame[index_min] == max_input){
output_frame[index_min - 1] = max_input;
}
output_frame[index_min] = min_input;
}
// If max_value is not represented in the output array, then set a
// point approximately 2/3 through the array to min_input. In the case
// of a very small frame size and a min_value is set above , make sure
// a different point in the array is used.
if (max_flag == false){
index_max = (int) ((((float) frame_size)*2)/3);
if ((min_flag == false) && (index_max == index_min)) index_max=index_min+1;
output_frame[index_max] = max_input;
}
}
// Map the values 0 through 255 to their A-law or mu-law equivalent.
if (mu_or_a == 0 || mu_or_a ==2) { /* A-law mapping */
for(i=0; i< frame_size; i++){
if (output_frame[i] < 128) output_frame[i] = 127 - output_frame[i];
output_frame[i] = (output_frame[i] ^ 0x55); /* even bit inversion */
}
}
else { /* mu-law mapping */
for(i=0; i< frame_size; i++){
if (output_frame[i] > 127) {
output_frame[i] = (output_frame[i] & 0x7F); /* mask MSB to zero */
output_frame[i] = 255 - output_frame[i]; /* rest of mu-law rule */
}
}
}
num_written = (int) fwrite (output_frame, sizeof(uint8), frame_size,
output_file);
num_of_frames = num_of_frames - 1;
}
(void) fprintf(stdout,
"Created file : %s - without errors \n\n", name_buffer);
fclose(output_file); /* Close file */
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -