tsphere.c

来自「speech signal process tools」· C语言 代码 · 共 1,240 行 · 第 1/3 页

C
1,240
字号
#include <stdio.h>#include <sp/sphere.h>#include <util/hsgetopt.h>#define EXAMPLE1_10_BASE           "ex1_10.wav"#define EXAMPLE1_01_BASE           "ex1_01.wav"#define EXAMPLE1_10_WAVPACK_BASE   "ex1_10wp.wav"#define EXAMPLE1_10_SHORTEN_BASE   "ex1_10st.wav"#define EXAMPLE1_10_SHORTPACK_BASE "ex1_10sh.wav"#define EXAMPLE1_01_WAVPACK_BASE   "ex1_01wp.wav"#define EXAMPLE1_01_SHORTEN_BASE   "ex1_01st.wav"#define EXAMPLE1_01_SHORTPACK_BASE "ex1_01sh.wav"#define EXAMPLE2_10_BASE           "ex2_10.wav"#define EXAMPLE2_10_BASE           "ex2_10.wav"#define EXAMPLE2_01_BASE           "ex2_01.wav"#define EXAMPLE2_10_WAVPACK_BASE   "ex2_10wp.wav"#define EXAMPLE2_10_SHORTEN_BASE   "ex2_10st.wav"#define EXAMPLE2_01_WAVPACK_BASE   "ex2_01wp.wav"#define EXAMPLE2_01_SHORTEN_BASE   "ex2_01st.wav"char EXAMPLE1_10[256], EXAMPLE1_01[256], EXAMPLE1_10_WAVPACK[256];char EXAMPLE1_10_SHORTEN[256], EXAMPLE1_01_WAVPACK[256], EXAMPLE1_01_SHORTEN[256];char EXAMPLE1_10_SHORTPACK[256], EXAMPLE1_01_SHORTPACK[256];char EXAMPLE2_10[256], EXAMPLE2_01[256], EXAMPLE2_10_WAVPACK[256];char EXAMPLE2_10_SHORTEN[256], EXAMPLE2_01_WAVPACK[256], EXAMPLE2_01_SHORTEN[256];#define COPY "cp"main(argc,argv)int argc;char **argv;{    char *sphere_lib_dir=".";    int c;    while (( c=hs_getopt( argc, argv, "d:hvmMe" )) != -1 )	switch ( c ) {	  case 'd':	    sphere_lib_dir = hs_optarg;	    break;	  case 'v':	    sp_verbose ++;	    break;	  case 'm':	    mtrf_set_dealloc(0);	    mtrf_set_verbose(1);	    break;	  case 'M':	    mtrf_set_dealloc(1);	    mtrf_set_verbose(1);	    break;	  case 'e':	    	    set_error_util_debug(1);	    break;	  case 'h':	  default:	    printf("Usage: tsphere -[vmMeh] -d sphere_lib_dir\n");	    printf("Illegal argument: %c\n",c);	    exit(100);	}    if (hs_optind != argc){	printf("Usage: tsphere -[v|m] -d sphere_lib_dir\n");	exit(100);    }    hs_resetopt();	    sprintf(EXAMPLE1_10, "%s/%s", sphere_lib_dir,EXAMPLE1_10_BASE);    sprintf(EXAMPLE1_01, "%s/%s", sphere_lib_dir,EXAMPLE1_01_BASE);    sprintf(EXAMPLE1_10_WAVPACK, "%s/%s", sphere_lib_dir,EXAMPLE1_10_WAVPACK_BASE);    sprintf(EXAMPLE1_10_SHORTEN, "%s/%s", sphere_lib_dir,EXAMPLE1_10_SHORTEN_BASE);    sprintf(EXAMPLE1_10_SHORTPACK, "%s/%s", sphere_lib_dir,EXAMPLE1_10_SHORTPACK_BASE);    sprintf(EXAMPLE1_01_WAVPACK, "%s/%s", sphere_lib_dir,EXAMPLE1_01_WAVPACK_BASE);    sprintf(EXAMPLE1_01_SHORTEN, "%s/%s", sphere_lib_dir,EXAMPLE1_01_SHORTEN_BASE);    sprintf(EXAMPLE1_01_SHORTPACK, "%s/%s", sphere_lib_dir,EXAMPLE1_01_SHORTPACK_BASE);    sprintf(EXAMPLE2_10, "%s/%s", sphere_lib_dir,EXAMPLE2_10_BASE);    sprintf(EXAMPLE2_10_WAVPACK, "%s/%s", sphere_lib_dir,EXAMPLE2_10_WAVPACK_BASE);    sprintf(EXAMPLE2_10_SHORTEN, "%s/%s", sphere_lib_dir,EXAMPLE2_10_SHORTEN_BASE);    sprintf(EXAMPLE2_01, "%s/%s", sphere_lib_dir,EXAMPLE2_01_BASE);    sprintf(EXAMPLE2_01_WAVPACK, "%s/%s", sphere_lib_dir,EXAMPLE2_01_WAVPACK_BASE);    sprintf(EXAMPLE2_01_SHORTEN, "%s/%s", sphere_lib_dir,EXAMPLE2_01_SHORTEN_BASE);    header_test();      checksum_pre_post_verification();    write_required_field_test();    write_check_adding_fields_test();    update_test();    large_file_test();      printf("*******  ALL TESTS SUCCESSFULLY COMPLETED *******\n");    exit(0);}large_file_test(){    char *large_file_name = "large.wav";    char *large2_file_name = "large2.wav";        printf("-- Large File Handling:\n");    printf("---- Building CONTROL data file, %s\n",large2_file_name);    make_test_file(large2_file_name,160000,"");    do_large_file_conversion(large_file_name, large2_file_name, "SE-PCM:SBF-01");    do_large_file_conversion(large_file_name, large2_file_name, "SE-SHORTEN:SBF-10");    do_large_file_conversion(large_file_name, large2_file_name, "SE-WAVPACK:SBF-01");    unlink(large2_file_name);    printf("\n");}do_large_file_conversion(modified, control, mode)char *modified, *control, *mode;{    fprintf(stdout,"------ Building Test file '%s' converted by '%s', using control file %s\n",modified,mode,control);    make_test_file(modified,160000,mode);    if (diff_waveforms(modified,control) != 0){	fprintf(stdout,"Successful update failed waveform verification\n");	exit(-1);    }    unlink(modified);}make_test_file(name,size,conversion)char *name, *conversion;int size;{    SP_FILE *spp;    short *wavbuf;    long lint;    long buf_nsamp = 16000;    long nbyte = 2;    long srate = 16000;    int stow, i;    int samps_written=0, status;    if ((wavbuf=(short *)mtrf_malloc(sizeof(short)*buf_nsamp)) == (short *)0){	fprintf(stdout,"Unable to malloc memory for wav buffer\n");	exit(-1);    }    for (i=0; i<buf_nsamp; i++)	wavbuf[i] = 10 * ( i % (16 + (i / 100)));    spp = sp_open(name, "w");    if (spp == NULL) {	fprintf(stdout,"Couldn't open NIST waveform file: %s\n", name);	sp_print_return_status(stdout);	exit(-1);    }    lint = size;    sp_h_set_field(spp, "sample_count", T_INTEGER, (void *) &lint);    sp_h_set_field(spp, "sample_rate", T_INTEGER, (void *) &srate);    sp_h_set_field(spp, "sample_n_bytes", T_INTEGER, (void *) &nbyte);    sp_h_set_field(spp, "sample_byte_format", T_STRING, (void *)"10");    sp_h_set_field(spp, "sample_coding", T_STRING, (void *)"pcm");    lint = 1; sp_h_set_field(spp, "channel_count", T_INTEGER, (void *)&lint);    if (! strsame(conversion,"") )	sp_set_data_mode(spp,conversion);    while (samps_written < size){	stow = (samps_written + buf_nsamp) < size ? buf_nsamp : size - samps_written;	status = sp_write_data((void *)wavbuf, sizeof(short), stow, spp);	if (status != stow){	    fprintf(stdout,"Couldn't write NIST waveform file: %s\n", name);	    sp_print_return_status(stdout);	    status = sp_error(spp);	    sp_print_return_status(stdout);	    sp_close(spp);	    (void) mtrf_free(wavbuf);	    exit(-1);	}		samps_written += stow;    }    if (sp_close(spp) != 0) {	printf("SP_close failed\n");	sp_print_return_status(stdout);	exit(-1);    }    (void) mtrf_free(wavbuf);}write_required_field_test(){    SP_FILE *spp;    char *outfilename="outputz.wav";    short *wavbuf;    long lint;    long buf_nsamp = 16000;    long nbyte = 2;    long srate = 16000;    int stow, i;    int samps_written=0, status;    printf("-- Required Field Test:\n");    if ((wavbuf=(short *)mtrf_malloc(sizeof(short)*buf_nsamp)) == (short *)0){	fprintf(stdout,"Unable to malloc memory for wav buffer\n");	exit(-1);    }    for (i=0; i<buf_nsamp; i++)	wavbuf[i] = 10 * ( i % (16 + (i / 100)));    spp = sp_open(outfilename, "wv");    if (spp == NULL) {	fprintf(stdout,"Couldn't open NIST waveform file: %s\n", outfilename);	sp_print_return_status(stdout);	exit(-1);    }    for (i=0; i<5; i++){	switch (i) {	  case 0: printf("---- Test %d: Missing sample_n_bytes\n",i+1); break;	  case 1: printf("---- Test %d: Missing channel_count\n",i+1); break;	  case 2: printf("---- Test %d: sample_encoding 'ulaw' and missing sample_rate\n",i+1); break;	  case 3: printf("---- Test %d: sample_encoding 'pcm' and missing sample_rate\n",i+1); break;	  case 4: printf("---- Test %d: All fields present\n",i+1); break;	}	lint = buf_nsamp; sp_h_set_field(spp, "sample_count", T_INTEGER, (void *) &lint); 	if (i > 0) sp_h_set_field(spp, "sample_n_bytes", T_INTEGER, (void *) &nbyte);	if (i > 1) { lint = 1;  sp_h_set_field(spp, "channel_count", T_INTEGER, (void *) &lint);}	if (i > 2) { sp_h_set_field(spp, "sample_coding", T_STRING, (void *) "ulaw"); }	if (i > 3) { sp_h_set_field(spp, "sample_coding", T_STRING, (void *) "pcm"); }	if (i > 3) { lint = 16000;  sp_h_set_field(spp, "sample_rate", T_INTEGER, (void *) &lint); }	status = sp_write_data((void *)wavbuf, sizeof(short), buf_nsamp, spp);	if ((i < 4) && (status > 0)){	    fprintf(stdout,"************* TEST FAILED ****************\n");	    sp_print_return_status(stdout);	}	else if ((i == 4) && (status != buf_nsamp)) {	    fprintf(stdout,"************* TEST FAILED ****************\n");	    sp_print_return_status(stdout);	}    }    if (sp_close(spp) != 0) {	printf("SP_close failed\n");	sp_print_return_status(stdout);	exit(-1);    }    (void) mtrf_free(wavbuf);    unlink(outfilename);    printf("\n");}write_check_adding_fields(comp_file,conversion_str)char *comp_file, *conversion_str;{    SP_FILE *spp;    char *outfilename = "outputw.wav";    short *wavbuf;    int i, status;    long nsamp = 16000;    long nbyte = 2;    long srate = 16000;    SP_CHECKSUM expected_checksum=61520;    int expected_sample_size=nsamp;    long lchecksum=expected_checksum;    long channels=1;    printf("------ Data Mode string '%s'\n",conversion_str);    printf("------   Compared to file '%s'\n",comp_file);    if ((wavbuf=(short *)mtrf_malloc(sizeof(short)*nsamp)) == (short *)0){	fprintf(stdout,"Unable to malloc memory for wav buffer\n");	exit(-1);    }    for (i=0; i<16000; i++)	wavbuf[i] = 10 * ( i % (16 + (i / 100)));    for (i=0; i<4; i++){	switch (i) {	  case 0: printf("-------- Test %d: All fields present\n",i+1); break;	  case 1: printf("-------- Test %d: Missing sample_count\n",i+1); break;	  case 2: printf("-------- Test %d: Missing sample_checksum\n",i+1); break;	  case 3: printf("-------- Test %d: Missing sample_count and sample_checksum\n",i+1); break;	}	spp = sp_open(outfilename, "wv");	if (spp == NULL) {	    fprintf(stdout,"Couldn't open NIST waveform file: %s", outfilename);	    sp_print_return_status(stdout);	    exit(-1);	}	/* FILL IN ONLY THE REQUIRED FIELDS */	/*  i = 0  ->  All fields present */	/*  i = 1  ->  missing sample_count */	/*  i = 2  ->  missing checksum */	/*  i = 3  ->  missing sample_count and checksum */	if ((i == 0) || (i == 2))	    sp_h_set_field(spp, "sample_count", T_INTEGER, (void *) &nsamp);	if ((i == 0) || (i == 1))	    sp_h_set_field(spp, "sample_checksum", T_INTEGER, (void *) &lchecksum);	sp_h_set_field(spp, "sample_rate", T_INTEGER, (void *) &srate);	sp_h_set_field(spp, "sample_n_bytes", T_INTEGER, (void *) &nbyte);	sp_h_set_field(spp, "sample_byte_format", T_STRING, (void *)get_natural_byte_order());	sp_h_set_field(spp, "sample_coding", T_STRING, (void *)"pcm");	sp_h_set_field(spp, "channel_count", T_INTEGER, (void *)&channels);	if (! strsame(conversion_str,"")){	    if (sp_set_data_mode(spp,conversion_str) != 0){		printf("Set data mode failed\n");		sp_print_return_status(stdout);		exit(-1);	    }	}	status = sp_write_data((void *)wavbuf, sizeof(short), nsamp, spp);	if (status != nsamp)	    {		fprintf(stdout,"Couldn't write NIST waveform file: %s\n", outfilename);		sp_print_return_status(stdout);		status = sp_error(spp);		sp_print_return_status(stdout);		sp_close(spp);	    }		if (sp_close(spp) != 0) {	    printf("SP_close failed\n");	    sp_print_return_status(stdout);	    exit(-1);	}	    	if (diff_waveforms(outfilename,comp_file) != 0){	    fprintf(stdout,"Successful write failed waveform verification\n");	    exit(-1);	}	{ int chg, ins, del, fail=0;	  if (diff_header(outfilename,comp_file,&chg,&ins,&del,0) != 0) {	      fprintf(stdout,"Successful write failed header verification\n");	      fail=1;	  }	  if ((fail==1) && (chg > 0 || ins > 0 || del > 0)) {	      diff_header(outfilename,comp_file,&chg,&ins,&del,1);	      exit(-1);	  }        }	unlink(outfilename);    }    (void) mtrf_free(wavbuf);}write_check_adding_fields_test(){    printf("-- Write Adding Fields Check:\n");    write_check_adding_fields(EXAMPLE2_10,"SBF-10");    write_check_adding_fields(EXAMPLE2_01,"SBF-01");    write_check_adding_fields(EXAMPLE2_10_SHORTEN,"SE-SHORTEN:SBF-10");    write_check_adding_fields(EXAMPLE2_10_WAVPACK,"SE-WAVPACK:SBF-10");    write_check_adding_fields(EXAMPLE2_10_SHORTEN,"SE-SHORTEN:SBF-10");    write_check_adding_fields(EXAMPLE2_01,"SBF-01");    write_check_adding_fields(EXAMPLE2_01_SHORTEN,"SE-SHORTEN:SBF-01");    write_check_adding_fields(EXAMPLE2_01_WAVPACK,"SE-WAVPACK:SBF-01");    write_check_adding_fields(EXAMPLE2_01_SHORTEN,"SE-SHORTEN:SBF-01");    printf("\n");}update_test(){    printf("-- Update Tests\n");    printf("---- Update of header fields\n");    header_update(EXAMPLE1_10,0);    header_update(EXAMPLE1_01,0);    header_update(EXAMPLE1_10_WAVPACK,0);    header_update(EXAMPLE1_10_SHORTEN,0);    header_update(EXAMPLE1_01_WAVPACK,0);    header_update(EXAMPLE1_01_SHORTEN,0);    printf("---- Update of header fields, expanding the header\n");    header_update(EXAMPLE1_10,1);    header_update(EXAMPLE1_01,1);    header_update(EXAMPLE1_10_WAVPACK,1);    header_update(EXAMPLE1_10_SHORTEN,1);    header_update(EXAMPLE1_01_WAVPACK,1);    header_update(EXAMPLE1_01_SHORTEN,1);    printf("---- Update the waveform format\n");    waveform_update(EXAMPLE1_10,"SBF-10",EXAMPLE1_10);    waveform_update(EXAMPLE1_10,"SBF-01",EXAMPLE1_01);    waveform_update(EXAMPLE1_10,"SE-WAVPACK:SBF-10",EXAMPLE1_10_WAVPACK);    waveform_update(EXAMPLE1_10,"SE-SHORTEN:SBF-10",EXAMPLE1_10_SHORTEN);    waveform_update(EXAMPLE1_10,"SE-WAVPACK:SBF-01",EXAMPLE1_01_WAVPACK);    waveform_update(EXAMPLE1_10,"SE-SHORTEN:SBF-01",EXAMPLE1_01_SHORTEN);    waveform_update(EXAMPLE1_01,"SBF-01",EXAMPLE1_01);    waveform_update(EXAMPLE1_01,"SBF-10",EXAMPLE1_10);    waveform_update(EXAMPLE1_01,"SE-WAVPACK:SBF-01",EXAMPLE1_01_WAVPACK);    waveform_update(EXAMPLE1_01,"SE-SHORTEN:SBF-01",EXAMPLE1_01_SHORTEN);    waveform_update(EXAMPLE1_01,"SE-WAVPACK:SBF-10",EXAMPLE1_10_WAVPACK);    waveform_update(EXAMPLE1_01,"SE-SHORTEN:SBF-10",EXAMPLE1_10_SHORTEN);    waveform_update(EXAMPLE1_10_SHORTEN,"SE-SHORTEN:SBF-10",EXAMPLE1_10_SHORTEN);    waveform_update(EXAMPLE1_10_SHORTEN,"SE-SHORTEN:SBF-01",EXAMPLE1_01_SHORTEN);    waveform_update(EXAMPLE1_10_SHORTEN,"SE-WAVPACK:SBF-10",EXAMPLE1_10_WAVPACK);    waveform_update(EXAMPLE1_10_SHORTEN,"SE-WAVPACK:SBF-01",EXAMPLE1_01_WAVPACK);    waveform_update(EXAMPLE1_10_SHORTEN,"SE-PCM:SBF-10",EXAMPLE1_10);    waveform_update(EXAMPLE1_10_SHORTEN,"SE-PCM:SBF-01",EXAMPLE1_01);    waveform_update(EXAMPLE1_10_WAVPACK,"SE-WAVPACK:SBF-10",EXAMPLE1_10_WAVPACK);    waveform_update(EXAMPLE1_10_WAVPACK,"SE-WAVPACK:SBF-01",EXAMPLE1_01_WAVPACK);    waveform_update(EXAMPLE1_10_WAVPACK,"SE-SHORTEN:SBF-10",EXAMPLE1_10_SHORTEN);    waveform_update(EXAMPLE1_10_WAVPACK,"SE-SHORTEN:SBF-01",EXAMPLE1_01_SHORTEN);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?