📄 tsphere.c
字号:
#include <stdio.h>#include <math.h>#include <string.h>#define SPHERE_PRE_2_2 #include <sp/sphere.h>#include <util/hsgetopt.h>#include <util/memory.h>#if defined(NARCH_SUN) || defined(NARCH_HP) /* cth */#include <sys/types.h>#include <netinet/in.h>#endif#define EX1_10_BASE "ex1_10.wav"#define EX1_10_CORRUPT_BASE "ex1_10.wvc"#define EX1_01_BASE "ex1_01.wav"#define EX1_10_WAVPACK_BASE "ex1_10wp.wav"#define EX1_10_SHORTEN_BASE "ex1_10st.wav"#define EX1_10_SHORTEN_CORRUPT_BASE "ex1_10st.wvc"#define EX1_10_SHORTPACK_BASE "ex1_10sh.wav"#define EX1_01_WAVPACK_BASE "ex1_01wp.wav"#define EX1_01_SHORTEN_BASE "ex1_01st.wav"#define EX1_01_SHORTPACK_BASE "ex1_01sh.wav"#define EX2_10_BASE "ex2_10.wav"#define EX2_10_BASE "ex2_10.wav"#define EX2_01_BASE "ex2_01.wav"#define EX2_10_WAVPACK_BASE "ex2_10wp.wav"#define EX2_10_SHORTEN_BASE "ex2_10st.wav"#define EX2_01_WAVPACK_BASE "ex2_01wp.wav"#define EX2_01_SHORTEN_BASE "ex2_01st.wav"#define EX4_ULAW_BASE "ex4.wav"#define EX4_ULAW_WAVPACK_BASE "ex4_wp.wav"#define EX4_ULAW_SHORTEN_BASE "ex4_sh.wav"#define EX4_ULAW_10_BASE "ex4_10.wav"#define EX4_ULAW_01_BASE "ex4_01.wav"#define EX5_ULAW_2CHAN_BASE "ex5.wav"#define EX5_ULAW_2CHAN_PCM_BASE "ex5_p.wav"#define EX5_ULAW_2CHAN_PCM_01_BASE "ex5_p01.wav"#define EX5_ULAW_2CHAN_PCM_SHORTEN_BASE "ex5_p_sh.wav"#define EX5_ULAW_2CHAN_PCM_WAVPACK_BASE "ex5_p_wp.wav"#define EX5_ULAW_2CHAN_WAVPACK_BASE "ex5_wp.wav"#define EX5_ULAW_2CHAN_SHORTEN_BASE "ex5_sh.wav"#define EX5_ULAW_CHAN1_BASE "ex5_c1.wav"#define EX5_ULAW_CHAN1_BITREV_BASE "ex5_c1br.wav"#define EX5_ULAW_CHAN1_PCM_BASE "ex5_c1_p.wav"#define EX5_ULAW_CHAN2_BASE "ex5_c2.wav"#define EX5_ULAW_CHAN2_BITREV_BASE "ex5_c2br.wav"#define EX5_ULAW_CHAN2_PCM_BASE "ex5_c2_p.wav"#define EX5_ULAW_CHAN12_BASE "ex5_12.wav"#define EX5_ULAW_CHAN12_PCM_BASE "ex5_12_p.wav"#define EX6_BASE "ex6.wav"#define EX7_PCULAW_BASE "ex7.wav"#define EX7_PCULAW_WAVPACK_BASE "ex7_wp.wav"#define EX7_PCULAW_SHORTEN_BASE "ex7_sh.wav"#define EX7_PCULAW_10_BASE "ex7_10.wav"#define EX7_PCULAW_01_BASE "ex7_01.wav"char EX1_10[256], EX1_10_CORRUPT[256], EX1_01[256], EX1_10_WAVPACK[256];char EX1_10_SHORTEN_CORRUPT[256];char EX1_10_SHORTEN[256], EX1_01_WAVPACK[256], EX1_01_SHORTEN[256];char EX1_10_SHORTPACK[256], EX1_01_SHORTPACK[256];char EX2_10[256], EX2_01[256], EX2_10_WAVPACK[256];char EX2_10_SHORTEN[256], EX2_01_WAVPACK[256], EX2_01_SHORTEN[256];char EX4_ULAW[256], EX4_ULAW_10[256], EX4_ULAW_01[256];char EX4_ULAW_SHORTEN[256], EX4_ULAW_WAVPACK[256];char EX5_2CHAN[256], EX5_2CHAN_SHORTEN[256], EX5_2CHAN_WAVPACK[256];char EX5_CHAN1[256], EX5_CHAN2[256], EX5_CHAN1_BITREV[256];char EX5_CHAN2_BITREV[256];char EX5_2CHAN_PCM[256], EX5_CHAN1_PCM[256], EX5_CHAN2_PCM[256];char EX5_2CHAN_PCM_SHORTEN[256], EX5_2CHAN_PCM_WAVPACK[256];char EX5_CHAN12[256], EX5_CHAN12_PCM[256], EX5_2CHAN_PCM_01[256];char EX6[256];char EX7_PCULAW[256], EX7_PCULAW_10[256], EX7_PCULAW_01[256];char EX7_PCULAW_SHORTEN[256], EX7_PCULAW_WAVPACK[256];/* function prototypes */void header_test(int);void doc_example_2_test(int);void doc_example_4_test(int);void ulaw_test(int);void pculaw_test(int);void converted_read_check(char *, char *, char *);void two_channel_test(int);int perform_2_channel_write_test(char *, char *, char *, char *);int perform_2_channel_read_test(char *, char *, char *, char *);void large_file_test(int);void do_large_file_conversion(char *, char *, char *);void make_test_file(char *, int, char *);void write_required_field_test(int);void write_check_adding_fields(char *, char *);void write_check_adding_fields_test(int);void update_test(int);void header_update(char *, int);void waveform_update(char *, char *, char *);void checksum_pre_post_verification(int);void pre_read_check(char *, int);void post_write_check(char *);void print_usage(void);void build_example_file_names(char *);void mult_channel_raw_data_test(int);void selective_channel_test(int);int do_selective_read_test(char *, char *, char *, char *, int);void array_access_tests(int);int write_with_array_access(char *, char *, char *, int);int verify_checksum_computations(int);void rewind_tests(int);void seek_test(int);void perform_seek_test(char *);void rewind_file_compare(char *, char *, char *, char *, char *);void rewind_file_multi_channel(char *,char *,char *,char *);void sp_compute_checksum_test(int);void do_sp_compute_checksum_test(char *, int, int, int);int do_sp_tell_check(SP_FILE *sp, char *file);#ifndef M_PI#define M_PI 3.14159265358979323846#endif#define COPY "cp"void print_usage(void) { fprintf(spfp, "Usage: tsphere -[vmMeh] [-l iterations] [ -[bf] n ] -d sphere_lib_dir\n");}int warning = 0;int main(int argc, char **argv){ char *sphere_lib_dir="."; int c,l,loop=1,start_test=0,final_test=100; char *prog; prog = strrchr(argv[0],'/'); prog = (prog == CNULL) ? argv[0] : (prog + 1); while (( c=hs_getopt( argc, argv, "d:l:b:f:hvmMe" )) != -1 ) switch ( c ) { case 'd': sphere_lib_dir = hs_optarg; break; case 'v': sp_verbose ++; break; case 'l': loop = atoi(hs_optarg); break; case 'b': start_test = atoi(hs_optarg); break; case 'f': final_test = atoi(hs_optarg); 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': print_usage(); exit(0); default: print_usage(); fprintf(spfp,"Illegal argument: %c\n",c); exit(100); } fprintf(spfp,"%s: %s\n\n",prog,sp_get_version()); if (hs_optind != argc){ print_usage(); exit(100); } hs_resetopt(); build_example_file_names(sphere_lib_dir); for (l=0; l!=loop; l++){ if (loop != 1) fprintf(spfp, "************* Beginning Iteration %d ************\n\n", l+1); if (start_test <= 1 && final_test >= 1) header_test(1); if (start_test <= 2 && final_test >= 2) checksum_pre_post_verification(2); if (start_test <= 3 && final_test >= 3) write_required_field_test(3); if (start_test <= 4 && final_test >= 4) write_check_adding_fields_test(4); if (start_test <= 5 && final_test >= 5) verify_checksum_computations(5); if (start_test <= 6 && final_test >= 6) update_test(6); if (start_test <= 7 && final_test >= 7) large_file_test(7); if (start_test <= 8 && final_test >= 8) ulaw_test(8); if (start_test <= 9 && final_test >= 9) two_channel_test(9); if (start_test <= 10 && final_test >= 10) mult_channel_raw_data_test(10); if (start_test <= 11 && final_test >= 11) doc_example_2_test(11); if (start_test <= 12 && final_test >= 12) doc_example_4_test(12); if (start_test <= 13 && final_test >= 13) selective_channel_test(13); if (start_test <= 14 && final_test >= 14) array_access_tests(14); if (start_test <= 15 && final_test >= 15) rewind_tests(15); if (start_test <= 16 && final_test >= 16) pculaw_test(16); if (start_test <= 17 && final_test >= 17) seek_test(17); if (start_test <= 18 && final_test >= 18) sp_compute_checksum_test(18); #ifdef SUN if (malloc_verify() == 0){ fprintf(spfp,"****** all tests completed BUT THE MEMORY HEAP"); fprintf(spfp,"IS TRASHED ******\n"); }#endif } if (warning > 0) fprintf(spfp, "******* There were %d non-fatal warnings *******\n",warning); fprintf(spfp,"******* ALL TESTS SUCCESSFULLY COMPLETED *******\n"); exit(0);}void sp_compute_checksum_test(int test){ fprintf(spfp,"Test %2d: sp_compute_checksum() testing:\n",test); fprintf(spfp,"-- Test on files w/o previous checksums:\n"); do_sp_compute_checksum_test(EX4_ULAW,0,0,0); do_sp_compute_checksum_test(EX5_CHAN12_PCM,0,0,0); fprintf(spfp,"-- Test on files with previous checksums:\n"); do_sp_compute_checksum_test(EX1_10,1,0,0); do_sp_compute_checksum_test(EX1_10_SHORTEN,1,0,0); fprintf(spfp,"-- Test on corrupt files:\n"); do_sp_compute_checksum_test(EX1_10_SHORTEN_CORRUPT,1,1,0); do_sp_compute_checksum_test(EX1_10_CORRUPT,1,1,0); fprintf(spfp,"-- Test on pre-seek'd files:\n"); do_sp_compute_checksum_test(EX1_10,1,0,1); do_sp_compute_checksum_test(EX1_10_SHORTEN,1,0,1); fprintf(spfp,"\n");}void do_sp_compute_checksum_test(char *file, int with_checksum, int corrupt, int test_seek){ SP_FILE *sp; SP_INTEGER fchksum; SP_CHECKSUM cchksum; int ret; fprintf(spfp,"---- Testing sp_compute_checksum() on file '%s'\n",file); if ((sp=sp_open(file,"r")) == SPNULL) { fprintf(spfp,"Error: sp_open of file '%s' failed.\n",file); goto FATAL_EXIT; } if (sp_h_get_field(sp,"sample_checksum", T_INTEGER,(void *)&fchksum)!=0){ if (with_checksum){ fprintf(spfp,"Error: Can't get checksum from file '%s'\n",file); goto FATAL_EXIT; } } if (test_seek){ if (sp_seek(sp,1034,0) != 0){ fprintf(spfp,"Error: attempt to perform an initial seek failed"); goto FATAL_EXIT; } } ret = sp_compute_checksum(sp, &cchksum); if (!corrupt){ if (cchksum == 0 && ret > 0){ sp_print_return_status(spfp); goto FATAL_EXIT; } if (with_checksum && (fchksum != cchksum)){ fprintf(spfp,"Error: Computed checksum %d != header %d\n", (int)cchksum,(int)fchksum); goto FATAL_EXIT; } } else { if (ret == 0){ fprintf(spfp,"Error: successfull execution on corrupt file\n"); goto FATAL_EXIT; } } if (test_seek){ if (sp_tell(sp) != 1034){ fprintf(spfp,"File was not returned to it's seek'd position of" " %d, but rather %d\n",1034,sp_tell(sp)); goto FATAL_EXIT; } } if (sp_close(sp) != 0){ fprintf(spfp,"Error: sp_close failed\n"); goto FATAL_EXIT; } return; FATAL_EXIT: if (sp != SPNULL) sp_close(sp); exit(1);}void seek_test(int test){ fprintf(spfp,"Test %2d: Waveform sp_seek() and sp_tell() tests:\n",test); perform_seek_test(EX1_01); perform_seek_test(EX1_01_SHORTEN); perform_seek_test(EX6); fprintf(spfp,"\n");}void perform_seek_test(char *file){ SP_FILE *sp; void *orig_data = (void *)0, *seek_data = (void *)0; int origin, offset; SP_INTEGER nsamp, nchan, nsnb; int loc, size, i, n1; /* 1. open and read in a file. 2. close the file 3. re-open the file, randomly seeking through the file verifying data as you go . */ fprintf(spfp,"-- Seek testing on file '%s'.\n",file); if (sp_load_file(file, ""/*SDM*/, &nsamp, &nchan, &nsnb, &orig_data) != 0){ fprintf(spfp," sp_load_file for file '%s' failed\n",file); goto FATAL_QUIT; } if ((sp=sp_open(file,"r")) == SPNULL) { fprintf(spfp," sp_open of file '%s' failed.\n",file); goto FATAL_QUIT; } if ((seek_data = (void *)sp_data_alloc(sp,-1))== (void *)0){ fprintf(spfp," seek data allocation failed\n"); goto FATAL_QUIT; } for (origin = 0; origin < 3; origin ++){ fprintf(spfp,"---- Origin: %d, Seek to: ",origin); /* start seeking through the data, comparing the data */ for (i=0; i<5; i++){ size = (nsamp / 5); if (size < 100) size = 100; loc = ((i+1) * (int)(nsamp * 0.6)) % nsamp; /* check the location, and make sure we don't read after EOF */ if (loc > (nsamp - 100)) loc = 100; if (loc + size > nsamp) size = nsamp - loc; fprintf(spfp,"%d[%d] ",(int)loc,(int)size); switch (origin) { case 0: offset = loc; break; case 1: offset = loc - sp->read_spifr->waveform->samples_read; break; case 2: offset = loc - sp->read_spifr->status->file_sample_count; break; default: goto FATAL_QUIT; } if (sp_seek(sp,offset,origin) != 0){ fprintf(spfp,"\nsp_seek() failed.\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -